Module 1: Practice Problems
💪 Put Your Knowledge to Work!
These 10 problems cover everything from Module 1: what Python is, using print(), comments, running scripts, and the interactive interpreter. Try to solve each problem on your own before looking at the hints or solutions.
Difficulty levels: Easy Medium Challenge
1 Hello, Your Name! Easy
Write a Python program that prints a personalized greeting. Your output should look like this (replace "Maria" with your own name):
print() statements, one for each line of output.
print("Hello, Maria!") print("Welcome to Python programming.") print("Let's get started!")
2 Python Calculator Easy
Use the Python interpreter (or a script) to calculate and display the following:
- The number of hours in a year (365 days)
- The result of 2 raised to the power of 20
- The remainder when 100 is divided by 7
* for multiplication, ** for exponentiation, and % for remainder (modulo).
print("Hours in a year:", 365 * 24) print("2 to the power of 20:", 2 ** 20) print("100 divided by 7, remainder:", 100 % 7)
3 Spot the Errors Easy
Each line below has an error. Identify each error and write the corrected code:
Print("Hello")
print("Good morning')
print "Welcome to Python"
print(Hello, World!)
# Line 1: 'Print' should be lowercase 'print' print("Hello") # Line 2: Mismatched quotes (double open, single close) print("Good morning") # Line 3: Missing parentheses around the text print("Welcome to Python") # Line 4: Text needs to be in quotes print("Hello, World!")
4 Comment Detective Easy
Look at the following code. What will be printed? (Some lines are comments!)
print("Line A") # print("Line B") print("Line C") # This prints Line C # print("Line D") print("Line E")
# are comments and are completely ignored by Python. Anything after # on the same line is also ignored.
Lines B and D are commented out (they start with #) so they are not executed. The comment after "Line C" on the same line doesn't affect the print statement before it.
5 Type Detective Medium
Without running the code, predict what type() will return for each of the following values. Then check your answers in the interpreter.
type(42) type(3.0) type("42") type(True) type(7 + 3) type(7 / 2)
/) always returns a float, and expressions are evaluated before type() checks them.
type(42) # <class 'int'> - whole number type(3.0) # <class 'float'> - decimal number type("42") # <class 'str'> - it's in quotes, so it's text! type(True) # <class 'bool'> - boolean value type(7 + 3) # <class 'int'> - 7+3=10, which is an int type(7 / 2) # <class 'float'> - division ALWAYS returns float
6 Receipt Printer Medium
Write a Python program that prints a simple receipt. Include at least 3 items with prices and a total. Use comments to explain your code. Your output should look something like:
print() statements. You can use spaces to align the prices. Remember to add a comment explaining what the program does!
# Receipt Printer Program # Displays a formatted store receipt print("============================") print(" PYTHON STORE") print("============================") print("Coffee $4.50") print("Sandwich $8.99") print("Cookie $2.75") print("----------------------------") print("Total: $", 4.50 + 8.99 + 2.75) print("============================") print("Thank you for shopping!")
7 Math Expressions Medium
Predict the result of each expression, then verify in the Python interpreter:
3 + 4 * 2 (3 + 4) * 2 10 / 3 10 // 3 2 ** 3 ** 2 100 - 25 * 3 + 10
**) is right-associative, meaning 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2).
3 + 4 * 2 # = 3 + 8 = 11 (3 + 4) * 2 # = 7 * 2 = 14 10 / 3 # = 3.3333333333333335 (float division) 10 // 3 # = 3 (integer division, rounds down) 2 ** 3 ** 2 # = 2 ** 9 = 512 (right-associative!) 100 - 25 * 3 + 10 # = 100 - 75 + 10 = 35
8 Script vs. Interpreter Medium
For each scenario, decide whether you would use the interactive interpreter or a script file (.py), and explain why:
- Checking what
type("hello")returns - Writing a program that displays a multiplication table
- Testing whether
10 / 3gives a whole number or decimal - Creating a homework submission for your Python class
- Quickly calculating a tip at a restaurant
- Interpreter — Quick one-line test to explore a function
- Script file — Multi-line program you'd want to save and reuse
- Interpreter — Quick test of a single expression
- Script file — A program you need to save, submit, and possibly edit later
- Interpreter — Quick calculation (e.g.,
45.80 * 0.20)
9 Concept Quiz Challenge
Answer the following questions about Python concepts:
- What does "dynamically typed" mean in Python?
- What is the difference between
/and//? - Why is Python called an "interpreted" language?
- Name three features that make Python beginner-friendly.
- What happens if you try to run a file named
python.py?
- Dynamically typed means you don't have to declare variable types. Python figures out the type automatically. You write
x = 5instead ofint x = 5. /performs regular division and always returns a float (e.g.,10 / 3 = 3.333...).//performs integer division and rounds down to the nearest whole number (e.g.,10 // 3 = 3).- Python is "interpreted" because it executes code line by line at runtime, rather than compiling the entire program into machine code before running it.
- Any three of: readable syntax (reads like English), indentation-based structure, dynamically typed, rich standard library, huge supportive community, error messages are descriptive.
- Naming a file
python.pycan cause Python to try to import your file instead of the actual Python module, leading to confusing errors. Always avoid naming files after Python itself or standard library modules.
10 Build a Business Card Challenge
Create a complete Python script (business_card.py) that displays a nicely formatted business card. Requirements:
- Include at least 2 comments explaining your program
- Use a border made of special characters (like
=,*, or+) - Display a name, title, email, and phone number
- Use at least one empty
print()for spacing - Include at least one
print()that combines text and a number using commas
print("+" + "-" * 30 + "+") to create borders. You can combine text and numbers with commas in print().
# Business Card Generator # Creates a formatted digital business card print("+------------------------------+") print("| |") print("| ALEX JOHNSON |") print("| Software Developer |") print("| |") print("| alex@example.com |") print("| (555) 123-4567 |") print("| |") print("+------------------------------+") print() print("Years of experience:", 5)