COMPUTING APPLICATIONS 2A Practical Guide Introduction to Python Programming ▪ Python is a versatile, high-level programming language known for its readability and ease of use. It is widely used in web development, data analysis, artificial intelligence, scientific computing, and more. This guide will help you set up your environment and provide some foundational concepts and practical examples to kickstart your Python programming journey. Setting Up Your Environment ▪ Installing Python 1. Download: • Visit the official Python website and download the latest version for your operating system (Windows, macOS, or Linux). 2. Install: • Run the installer. • Make sure to check the box that says "Add Python to PATH" during installation. • Follow the installation prompts to complete the process. Setting Up Your Environment ▪ Installing Visual Studio Code (VS Code) 1. Download: • Go to the official VS Code website and download the installer for your operating system. 2. Install: • Run the installer and follow the prompts to install VS Code. • During installation, you can choose to add VS Code to your PATH for easier access from the terminal. Setting Up Your Environment 1. Install the Python Extension: • Open VS Code. • Click on the Extensions view icon on the Sidebar (or press Ctrl+Shift+X). • In the search bar, type "Python" and select the extension provided by Microsoft. • Click the Install button. 2. Verify the Installation: • Open a new terminal in VS Code by going to Terminal > New Terminal. • Type python --version and press Enter to ensure that VS Code recognizes your Python installation. Now you have your Python environment set up and ready for programming! Chapter 1 – Introduction to computer science ▪ Exercise 1 o Create a flowchart and write an algorithm to find the sum of two numbers provided by the user. ▪ Exercise 2 o Create a flowchart and write an algorithm to check if a number is even or odd. ▪ Exercise 3 o Create a flowchart and write an algorithm to find the sum of the first N natural numbers. ▪ Exercise 4 ▪ Create a flowchart and write an algorithm to find the largest number in a list of numbers. Chapter 1 – Introduction to computer science ▪ Exercise 5 o Draw a flow chart and write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. ▪ Exercise 6 o Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. ▪ Exercise 7 o Write an algorithm that determine the net payable amount on a sale. The net payable amount consists of the sale price plus sales tax. The sales tax is decided as a) 8% of the sale price for national items b) 18% of the sale price for foreign items Construct a flowchart to show how the net payable amount is determined. Chapter 2 – Python Data types, Variables and Assignments, String operators, indexing operator ▪ Exercise 1 o Write Python expressions to perform the following basic arithmetic operations. • Calculate the sum of 15 and 27. • Calculate the difference between 100 and 45. • Multiply 12 by 8. • Divide 144 by 12. • Find the remainder when 35 is divided by 6. ▪ Exercise 2 o Write Python expressions to calculate the following compound expressions. o Calculate the expression (5 + 3) × 2. 5 5 o Calculate the expression 20 − . o Calculate the expression (8 − 3)2 . Chapter 2 – Python Data types, Variables and Assignments, String operators, indexing operator ▪ Exercise 3 o Write Python expressions using built-in functions. o Find the absolute value of -45. o Calculate the square root of 64. o Round the number 5.678 to 2 decimal places. ▪ Exercise 4 o Write Python expressions to perform operations on strings. o Concatenate the strings "Hello" and "World" with a space in between. o Repeat the string “VUT" 3 times. o Find the length of the string "Expressions". Chapter 2 – Lists and Tuples ▪ Exercise 1 o Write Python expressions using list o Perform basic operations on a list. o Create a list of five integers: [1, 2, 3, 4, 5]. o Add the number 6 to the end of the lists; Insert the number 0 at index 0. o Remove the number 3 from the list; Find the index of the number 4. o Print the list in reverse order. ▪ Exercise 2 o Write Python expressions using tuple o Create a tuple with the elements 'apple', 'banana', 'cherry’. o Access the second element of the tuple. o Try to change the first element of the tuple to 'orange' and observe the result.. Chapter 2 – Lists and Tuples ▪ Exercise 3 o Write Python expressions using tuple o Create a tuple with elements (5, 10, 15, 20). o Access the third element of the tuple; Create a new tuple by concatenating the original tuple with (25, 30); Repeat the tuple three times to create a new tuple. ▪ Exercise 4 o Write Python expressions using tuple o Convert between lists and tuples; Create a list with the elements 'x', 'y', 'z’. o Convert the list to a tuple; Convert the tuple back to a list. ▪ Exercise 5 o Write Python expressions using tuple o Use tuple unpacking to extract values. o Create a tuple person = ("Alice", 30, "Engineer"). o Unpack the tuple into three variables: name, age, occupation; Print the variables. Chapter 2 – Lists and Tuples ▪ Exercise 6 o Write Python expressions using tuple o Create a tuple with elements (5, 10, 15, 20). o Access the third element of the tuple; Create a new tuple by concatenating the original tuple with (25, 30); Repeat the tuple three times to create a new tuple. ▪ Exercise 7 o Write Python expressions using tuple o Convert between lists and tuples; Create a list with the elements 'x', 'y', 'z’. o Convert the list to a tuple; Convert the tuple back to a list. ▪ Exercise 8 o Write Python expressions using tuple o Use tuple unpacking to extract values. o Create a tuple person = ("Alice", 30, "Engineer"). o Unpack the tuple into three variables: name, age, occupation; Print the variables. Chapter 3 – Built-in functions ▪ Exercise 1 o Use the max() and min() functions to find the maximum and minimum values. o Find the maximum and minimum in a list of numbers: [3, 1, 4, 1, 5, 9]. o Find the maximum and minimum in a tuple: (10, 20, 30, 5, 15). ▪ Exercise 2 o Use the sum() function to calculate the total. o Calculate the sum of the numbers in the list [1, 2, 3, 4, 5]. o Calculate the sum of the numbers in a tuple (10, 20, 30, 40). ▪ Exercise 3 o Use input() to gather user input and print() to display messages. o Ask the user for their name and greet them. o Ask the user for two numbers and print their sum. Chapter 3 – Interactive input ▪ Exercise 1 o Use Input() function, ask the user for their name and age, then print a greeting message. o Prompt the user to enter their name; Prompt the user to enter their age. o Print a message that includes both their name and age. ▪ Exercise 2 o Prompt the user to enter a sentence and count the number of vowels in it. o Ask the user to input a sentence; Count and display the number of vowels (a, e, i, o, u). ▪ Exercise 3 o Allow the user to input their favorite fruits and display them in a list. o Ask the user to enter their favorite fruits, separated by commas. o Split the input into a list and print the list of fruits. Chapter 3 – Execution control structures ▪ Exercise 1 o Ask the user for a number and determine if it is even or odd. o Prompt the user to enter a number. o Use an if statement to check if the number is even or odd; Print the result. ▪ Exercise 2 o Prompt the user for their score and determine their grade. o Ask the user to enter a score (0-100). o Use if, elif, and else statements to determine the grade: o Pass with distinction: 75 – 100 o Pass: 50 – 74 o Fail: 0 – 49 o Print the grade status. Chapter 3 – Execution control structures ▪ Exercise 3 o Use a for loop to count from 1 to 10 and print each number. ▪ Exercise 4 o Calculate the sum of the first n natural numbers using a while loop. o Prompt the user for a number n; Use a while loop to calculate the sum of numbers from 1 to n; Print the sum. ▪ Exercise 5 o Generate a multiplication table for a given number; Ask the user for a number. o Use a for loop to print the multiplication table for that number from 1 to 10. o Exercise 6 o Use a loop with continue to skip printing even numbers from 1 to 20. Chapter 3 – Execution control structures ▪ Exercise 7 o Create a program that converts temperatures between Celsius and Fahrenheit. o Ask the user for a temperature and whether it's in Celsius or Fahrenheit. o Convert the temperature to the other scale and display the result. ▪ Exercise 8 o Create a simple game where the user has to guess a number between 1 and 10. o Generate a random number between 1 and 10. o Use a while loop to allow the user to guess the number. o Provide feedback on whether the guess is too high or too low. o End the loop when the user guesses correctly. Chapter 4 – Text data, files, and Exceptions ▪ Exercise 1 o Create a formatted greeting message. o Ask the user for their name and age; Print a greeting using formatted strings. ▪ Exercise 2 o Write user input to a text file. o Ask the user for a line of text; Write the text to a file called output.txt. o Exercise 3 o Read and display the contents of a file. o Read the contents of output.txt; Print the contents to the console. o Exercise 4 o Perform division and handle division by zero errors; Ask the user for two numbers; Try to divide the first number by the second, and handle any exceptions. Chapter 5 – Execution control structures ▪ Exercise 1 o Write a Python program to determine if a person is eligible to vote. The basic requirement to vote is that a person must be 18 years or older. However, this program must also include additional conditions such as citizenship and voter registration status. ▪ Exercise 2 o Write a Python program that asks for the total purchase amount and applies a discount based on the following conditions: o If the amount is greater than R400, apply a 20% discount. o If the amount is between R300 and R500, apply a 10% discount. o If the amount is less than R300, no discount. o Exercise 3 o Write a Python program that classifies a person into different age groups: Child: 0 - 12 years; Teenager: 13 - 17 years; Adult: 18 - 64 years; Senior: 65+ years Chapter 5 – Execution control structures ▪ Exercise 4 o Write a Python program that calculates the sum of all numbers from 1 to 100 using a for loop. ▪ Exercise 5 o Write a Python program that counts (using for loop) how many even numbers there are between 1 and 100. ▪ Exercise 6 o Write a Python program that prints a multiplication table (from 1 to 10) using nested for loops. ▪ Exercise 7 ▪ Write a Python program that calculates the sum of odd numbers and sum of even numbers separately from a list of integers. Chapter 5 – Execution control structures ▪ Exercise 6 o Write a Python program that asks the user to input a number N and then prints all numbers from 1 to N using a while loop. ▪ Exercise 7 o Write a Python program that keeps asking the user for a message and repeats it back. The program should only stop if the user enters the word "stop”. ▪ Exercise 8 o Write a Python program that keeps asking the user for a password and only stops when the user enters a predefined password, "secret". This loop will run indefinitely until the condition is met. ▪ Exercise 9 ▪ Write a Python program that asks for user input and echoes it back. The loop will run indefinitely unless the user types "quit" to break out of the loop. Chapter 5 – Execution control structures ▪ Exercise 10 o Write a Python program that searches for a specific number in a list. If the number is found, print "Found!" and break out of the loop. ▪ Exercise 11 o Write a Python program that prints numbers from 1 to N. If a number is divisible by 3, print "Fizz", if divisible by 5, print "Buzz", and if divisible by both, print "FizzBuzz". ▪ Exercise 8 o Write a Python program that demonstrates the use of break, continue, and pass within a single for loop. The program should iterate through a list of numbers and: o Use continue to skip even numbers. o Use pass to do nothing in certain conditions. o Use break to exit the loop when a number greater than 10 is encountered. Chapter 6 – Containers and Randomness ▪ Exercise 1 o Create a phonebook where users can look up contacts by name. o Use names as keys (user-defined indexes) and phone numbers as values in a dictionary. o Create user – define function for Add, delete, and update contacts. ▪ Exercise 2 o Build a system where student names are the keys and their grades are the values. o Demonstrate how dictionaries allow fast lookup, adding, or updating students’ grades. o Calculate the average grade, display a specific student's grade, and print the top student ▪ Exercise 3 o Write a function that takes a string and counts how often each word appears. o Use words as dictionary keys and their frequencies as values. o Handle punctuation and case sensitivity. Chapter 7 – Namespaces ▪ Exercise 1 o Create a function calculate_rectangle_area to calculate the area of a rectangle, and then reuse it to calculate the area of different rectangles by passing different values. ▪ Exercise 2 o Write a program that calculates the total price of a meal including tax and tip. Use separate functions to calculate the tax, the tip, and the final total price. ▪ Exercise 3 o Write a function that takes a string and counts how often each word appears. o Use words as dictionary keys and their frequencies as values. o Handle punctuation and case sensitivity. ▪ Exercise 4 o Write two functions with the same local variable name but performing different operations. Show that the variables in the two functions don't interfere with each other. Chapter 7 – Namespaces ▪ Exercise 5 o Write a program that calculates the Body Mass Index (BMI) using encapsulated, modular weight (kg) functions. The BMI formula is 𝐵𝑀𝐼 = . Make sure to hide the internal logic height (m)2 and variables within the functions. ▪ Exercise 6 o Write a program that demonstrates how a local variable with the same name as a global variable does not affect the global variable. ▪ Exercise 7 o Write a program that modifies a global variable inside a function using the global keyword. ▪ Exercise 8 o Write a program that uses both local and global variables within a function, modifying the global variable but leaving the local variable unchanged. Chapter 7 – Namespaces ▪ Exercise 9 o Write a function that divides two numbers, but handle the case where the denominator is zero using a try-except block. ▪ Exercise 10 o Write a program that reads two numbers from the user and divides them, handling both invalid input (e.g., non-numeric values) and division by zero errors. ▪ Exercise 11 o Create a class Car with a class method display_info that prints out the car's brand and model. o Use the class method to access and print this information. o Define the Car class with: o Class attributes for brand and model. o A class method display_info that prints the brand and model. o Create an instance of the class and call the display_info method. Chapter 7 – Namespaces ▪ Exercise 12 o Define a class BankAccount with the following features: o Instance Attributes: o account_number and balance. o Instance Methods: o deposit(amount): adds the amount to the balance. o withdraw(amount): subtracts the amount from the balance. o Class Method: o create_account(cls, initial_balance): a class method that creates a new BankAccount instance with a specified initial balance. Chapter 8 – Object-Oriented Programming ▪ Exercise 1 o Create a Vehicle class and a Car subclass that overrides the move method. o The Vehicle class has a method move that prints "Moving". o The Car class overrides this method to print "Car is moving" ▪ Exercise 2 o Create a Rectangle class with overloaded constructors. o If no arguments are passed, create a rectangle of size 1x1. o If one argument is passed, create a square. o If two arguments are passed, create a rectangle with specified dimensions. Chapter 8 – Object-Oriented Programming ▪ Exercise 3 o Define a class Point that has two methods: o A method move that moves the point to a new position (x, y). o A method distance that calculates the distance from another point. o Instructions: o Implement the class with instance variables x and y. o Define methods to move the point and compute distance from another point ▪ Exercise 4 o Overload the +, -, and __str__ operators for a Vector class. o The Vector class has x and y attributes. o Overload the + operator to add two vectors. o Overload the - operator to subtract two vectors. o Overload the __str__ method to print the vector in the form "(x, y)". PRINT("THANK YOU") PC Lando
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )