Practice Exercises
Test your understanding with hands-on exercises. Reveal answers when you're ready.
1 1.1 What is Python
Which of the following is Python commonly used for?
Why is Python considered beginner-friendly?
1 1.2 First Line of Code
Complete the code to print "Hello, World!"
______("Hello, World!")
Write a program that prints your name on one line and your favorite hobby on the next line.
1 1.3 Comments
Which symbol is used for a single-line comment in Python?
Add a multi-line comment to describe the program below.
______
This program calculates
the area of a rectangle
______
length = 10
width = 5
area = length * width
print(area)
1 1.4 Variables
Write code to create three variables: name (string), age (integer), and height (float). Then print all three.
Which of the following is NOT a valid variable name in Python?
1 1.5 Data Types
What is the data type of 3.14 in Python?
Create one variable of each type: int, float, str, and bool. Then print each with its type.
1 1.6 Type Conversion
Fill in the blank to convert the string to an integer:
age_str = "25"
age_int = ______(age_str)
print(age_int + 5) # Should print 30
Write code that adds an integer and a float together, then prints the result and its type.
1 1.7 Input
Fill in the blank to get the user's favorite color:
color = ______("What is your favorite color? ")
print("You like " + color + "!")
Write a program that asks the user for their name and age, then prints a greeting like: "Hello Alice, you are 25 years old!"
1 1.8 Operators
What does 2 + 3 * 4 evaluate to in Python?
Fill in the blank using the modulo operator to get the remainder of 17 divided by 5:
remainder = 17 ______ 5
print(remainder) # Should print 2
Write a program that asks the user for a number and tells them whether it is even or odd using the % operator.
2 2.1 if Statement
Write a program that checks if a number is positive and prints "Positive number!" if it is.
What is wrong with this Python code?
if x > 10
print("Big")
2 2.2 if…else
Write a program that checks if a number is even or odd and prints the result.
Complete the pass/fail checker. A score of 60 or above is a pass:
score = int(input("Enter your score: "))
______ score >= 60:
print("Pass!")
______:
print("Fail!")
2 2.3 if…elif…else
Write a grading program: 90+ = A, 80–89 = B, 70–79 = C, 60–69 = D, below 60 = F.
Write a season detector: month 3–5 = Spring, 6–8 = Summer, 9–11 = Autumn, 12/1/2 = Winter.
2 2.4 Nested if
Write a program that checks if a number is positive, and if so, also checks whether it is even or odd.
Write a login checker: first check the username, then (if correct) check the password. Print "Access granted" only if both are correct.
2 2.5 Branching Practice
Write a leap year checker. A year is a leap year if: divisible by 4 AND (not divisible by 100 OR divisible by 400).
Write a discount calculator: under $50 = no discount, $50–$99 = 10% off, $100–$199 = 20% off, $200+ = 30% off. Print the final price.
Write a simple calculator: ask for two numbers and an operator (+, -, *, /), then print the result. Handle division by zero.
3 3.1 for Loop
Write a for loop that prints the numbers 1 to 10, each on a new line.
Write a for loop that prints each character of the string "Python" on a separate line.
3 3.2 while Loop
Write a countdown from 10 to 1 using a while loop. Print "Liftoff!" at the end.
Write a program that keeps asking the user for input until they type "quit". Print each input they give.
3 3.3 break / continue
Write a loop that prints numbers 1 to 10, but stops (using break) when it finds the number 7.
Write a loop that prints numbers 1 to 10, but skips (using continue) the even numbers — only odd numbers should print.
3 3.4 Nested for Loops
Print a 5×5 multiplication table using nested for loops.
Print a right triangle pattern of stars with 5 rows:
*
**
***
****
*****
3 3.5 Nested while Loops
Write a number pyramid pattern using nested while loops:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Write a countdown with an inner countdown: outer loop goes from 3 to 1, inner loop counts from 3 down to the current outer value. Print each number.
3 3.6 Mixed Branch + Loop
Find and print all even numbers between 1 and 20 using a loop and an if statement.
Write FizzBuzz for numbers 1 to 20: print "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples of both, or the number itself.
4 4.1 Built-in Functions
What does len("Python") return?
Fill in the blanks to print the type and value of the variable:
x = 42
______(type(x)) # Should print <class 'int'>
______(x) # Should print 42
4 4.2 Custom Functions with def
Write a function called greet that takes a name as a parameter and prints "Hello, [name]!". Call it with your name.
Write a function called separator that prints a line of dashes: "————————————". Call it twice.
4 4.3 Parameters
Write a function add that takes two numbers and prints their sum. Call it with 5 and 3.
Write a function introduce that takes a name and age, then prints "My name is [name] and I am [age] years old."
4 4.4 Return Values
Write a function square that takes a number and returns its square. Print the result of calling square(7).
Write a function max_of_two that takes two numbers and returns the larger one. Do not use the built-in max() function.
4 4.5 Nested Function Calls
Write two functions: double(n) that returns n*2, and add_ten(n) that returns n+10. Then compose them: print the result of double(add_ten(5)).
Write a function circle_area that calculates a circle's area, and another function circle_info that calls circle_area internally and prints "Area: [result]".