I. Introduction to Python (5 minutes)
Python was created by Guido van Rossum and released in 1991. It’s known for its simplicity and versatility, making it a popular choice in various fields such as web development, data analysis, artificial intelligence, and scientific computing.
II. Setting up the Environment (5 minutes)
We will be using Google Colab, a free cloud service hosted by Google, for coding in Python. It requires no setup, provides access to powerful computing resources, and allows you to share your work easily. Simply go to Google Colab, sign in with your Google account, and you can start writing and running Python code.
III. Basic Syntax and Operations (20 minutes)
Variables and Data Types:
Variables are used to store data. In Python, you don’t need to declare the type of data a variable will hold – it’s decided dynamically.
age = 16
(int)
name = "John"
(str)
height = 5.9
(float)
is_student = True
(bool)
Arithmetic Operations:
Python supports basic arithmetic operations:
Addition: sum_result = 5 + 3
Subtraction: diff_result = 10 - 4
Multiplication: product_result = 6 * 7
Division: div_result = 8 / 2
Modulus: mod_result = 10 % 3
(remainder of the division)
Exponentiation: exp_result = 2 ** 3
(2 raised to the power of 3)
Floor Division: floor_div_result = 7 // 2
(division that rounds down)
String Manipulation:
Strings are sequences of characters. You can concatenate them, repeat them, index them, and slice them:
Concatenation: greeting = "Hello" + " World"
Repetition: name_repeated = "John " * 3
Indexing: char_at_index = "Hello"[1] # e
Slicing: substring = "Hello"[1:4] # ell
IV. Control Structures (5 minutes)
Conditional Statements:
Use if
, elif
, and else
to control the flow of your program based on conditions.
Loops:
Use for
and while
loops to repeat a block of code multiple times.
V. Functions (5 minutes)
Defining and Calling Functions:
Functions are blocks of code that are designed to do one specific job. You can define your own functions and use them in your program.
VI. Exercises (20 minutes)
-
Create variables to store your name, age, and favorite color. Then print these variables.
Solution
name = "John" age = 25 favorite_color = "blue" print(name, age, favorite_color)
-
Create a variable to store the price of a shirt, then create another variable to store the number of shirts bought. Calculate and print the total cost.
Solution
shirt_price = 20 shirts_bought = 5 total_cost = shirt_price * shirts_bought print(total_cost)
-
Create a variable to store the number of pages in a book, and another to store the number of books. Calculate and print the total number of pages in all books.
Solution
pages_per_book = 300 number_of_books = 10 total_pages = pages_per_book * number_of_books print(total_pages)
-
Create a variable to store the number of miles you run each day, and another to store the number of days. Calculate and print the total miles run.
Solution
miles_per_day = 5 days = 30 total_miles = miles_per_day * days print(total_miles)
-
Create a variable to store the average price of a meal, and another to store the number of meals you eat in a day. Calculate and print the total cost of meals in a month.
Solution
average_meal_price = 10 meals_per_day = 3 total_cost = average_meal_price * meals_per_day * 30 print(total_cost)
-
Create two variables to store the width and height of a rectangle. Calculate and print the perimeter of the rectangle.
Solution
width = 10 height = 5 perimeter = 2 * (width + height) print(perimeter)
-
Create a variable to store the radius of a circle. Calculate and print the area of the circle (use 3.14 for π).
Solution
radius = 7 area = 3.14 * radius ** 2 print(area)
-
Create a variable to store the side of a square. Calculate and print the area of the square.
Solution
side = 4 area = side ** 2 print(area)
-
Create a variable to store the base and another to store the height of a triangle. Calculate and print the area of the triangle.
Solution
base = 10 height = 6 area = 0.5 * base * height print(area)
-
Create a variable to store the amount of money you have and another to store the price of a candy bar. Calculate and print the number of candy bars you can buy and the remaining change.
Solution
money = 20 candy_price = 1.5 candy_bars = money // candy_price remaining_change = money % candy_price print(candy_bars, remaining_change)