Practice Lab: Python Programming Level 1 โ Hands-On Exercises
Welcome, young coder! You have completed all the lessons in Level 1 of Python Programming. Now it's time to practice what you have learned. This lab is full of hands-on exercises that will help you become a better programmer. Each exercise is designed to test a specific skill you learned in the course.
Don't worry if you make mistakes โ that's how we learn! Try each exercise on your own first. If you get stuck, look back at your notes or ask for help. The goal is to practice, not to be perfect. Let's get coding!
After completing this lab, you will be able to:
Chidi's Coding Practice
Chidi had learned so much about Python. He could write variables, loops, functions, and even build a quiz game. But his teacher said, "Chidi, the best way to get better at coding is to practice every day. Just like playing football or learning an instrument โ practice makes perfect!"
Chidi decided to practice coding every day. He started with simple exercises โ printing messages, calculating numbers, and asking for user input. Then he moved to harder exercises โ writing functions, working with lists, and building small programs. Each day, he got a little better. Soon, he could solve problems that were difficult before.
Now, it's your turn to practice! This lab has many exercises to help you become a confident Python programmer.
Task: Write a Python program that prints the following messages:
Expected Output:
Hello, World! Welcome to Python Programming! I am learning to code!
Hint: Use the print() function.
Task: Create a program that does the following:
name and store your name.age and store your age.height and store your height in meters (e.g., 1.55).is_student and store True.Example Output:
Name: Ada Age: 10 Height: 1.55 Is student: True
Hint: Use print("Name:", name) or f-strings.
Task: Write a program that does the following:
Example Interaction:
What is your name? Ada What is your favourite food? Jollof Rice Hello Ada! Jollof Rice sounds delicious!
Hint: Use input() to get user input and print() to display the result.
Task: Write a program that asks the user for their age. If the age is 18 or above, print "You are an adult." Otherwise, print "You are a child."
Example Interaction 1:
How old are you? 20 You are an adult.
Example Interaction 2:
How old are you? 10 You are a child.
Hint: Use int() to convert user input to an integer.
Task: Write a program that asks the user for a number and does the following:
Example Interactions:
Enter a number: 5 Positive Enter a number: -3 Negative Enter a number: 0 Zero
Hint: Use if, elif, and else.
Task: Write a program that uses a while loop to print numbers from 1 to 10.
Expected Output:
1 2 3 4 5 6 7 8 9 10
Hint: Start a counter at 1 and increment it until it reaches 10.
Task: Write a program that uses a for loop to print the numbers 1 to 10.
Expected Output:
1 2 3 4 5 6 7 8 9 10
Hint: Use range(1, 11).
Task: Write a program that does the following:
Example Output:
First fruit: Mango Last fruit: Orange Final list: ['Mango', 'Banana', 'Orange', 'Pineapple']
Hint: Use list methods like append(), remove(), and indexing.
Task: Write a program that loops through a list of numbers and prints each number multiplied by 2.
Example Input:
numbers = [1, 2, 3, 4, 5]
Expected Output:
2 4 6 8 10
Hint: Use a for loop to go through the list.
Task: Write a program that does the following:
Example Output:
Ada's age: 10
Final dictionary: {'Ada': 10, 'Chidi': 12, 'Bola': 11, 'Tunde': 9}
Hint: Use curly braces {} to create a dictionary.
Task: Write a function called greet that takes a name as a parameter and prints "Hello, [name]!". Then call the function with three different names.
Expected Output:
Hello, Ada! Hello, Chidi! Hello, Bola!
Hint: Use def greet(name):.
Task: Write a function called add that takes two numbers as parameters and returns their sum. Then call the function with different numbers and print the results.
Example Output:
5 + 3 = 8 10 + 20 = 30 7 + 2 = 9
Hint: Use return to send the value back.
Task: Write a program that does the following:
Hint: Use open("user_info.txt", "w") and write().
Task: Write a program that reads and prints the contents of a file called "user_info.txt". If the file doesn't exist, handle the error with try/except.
Hint: Use open("user_info.txt", "r") and read(). Use try/except to handle the error.
Task: Write a program that asks the user for a number. If the user enters something that is not a number, print "That is not a valid number!" and ask again.
Hint: Use a while loop and try/except.
Task: Create a tuple with 5 of your favourite things (movies, foods, games, etc.). Print the tuple. Then, try to change an item (it should cause an error).
Hint: Use parentheses () for tuples.
Task: Create a tuple with three values (name, age, city). Unpack the tuple into three variables and print them.
Example Output:
Name: Ada Age: 10 City: Lagos
Hint: Use name, age, city = my_tuple.
Task: Create a set of 5 favourite colours. Add a new colour. Try to add a duplicate โ notice it doesn't change. Print the set.
Hint: Use curly braces {} for sets.
Task: Use a list comprehension to create a list of squares of numbers from 1 to 10.
Expected Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Hint: Use [i * i for i in range(1, 11)].
Task: Build a simple calculator program that:
Example Interaction:
Enter first number: 10 Enter second number: 5 Enter operation (+, -, *, /): + 10 + 5 = 15
Hint: Use functions, if/elif/else, and try/except.
How to complete a lab exercise:
This lab is designed to be self-paced. Students can work at their own speed. Encourage them to help each other. Circulate and provide guidance. Celebrate their achievements, no matter how small. Remind them that making mistakes is part of learning.
Help your child set up their coding environment. Sit with them as they work on the exercises. Ask questions like "What do you think this code does?" and "What happens if you change this value?" Celebrate their progress and encourage them to keep going.
: after if, else, while, for, def.= instead of == for comparison.
+-------------+ +-------------+ +-------------+ +-------------+
| READ | --> | UNDERSTAND | --> | WRITE | --> | TEST |
| EXERCISE | | THE TASK | | THE CODE | | THE CODE |
+-------------+ +-------------+ +-------------+ +-------------+
| |
| V
| +-------------+
+--------------------------------------------------| DEBUG |
| IF NEEDED |
+-------------+
|
V
+-------------+
| MOVE TO |
| NEXT |
| EXERCISE |
+-------------+
| Data Structure | Mutable | Ordered | Duplicates | Example |
|---|---|---|---|---|
| List | Yes | Yes | Yes | [1, 2, 3] |
| Tuple | No | Yes | Yes | (1, 2, 3) |
| Dictionary | Yes | Yes (3.7+) | Keys: No, Values: Yes | {"a": 1, "b": 2} |
| Set | Yes | No | No | {1, 2, 3} |
Great job completing this practice lab! You have written many programs using all the skills you learned in Level 1. You used variables, data types, input/output, conditional statements, loops, lists, dictionaries, functions, files, error handling, tuples, sets, and list comprehensions.
Remember, becoming a great programmer takes time and practice. Keep coding every day, even if it's just for 15 minutes. Try building your own projects โ a game, a calculator, or a personal diary. The more you code, the better you will become. You are on the right path!
= and ==?if/else do?return do?_____ prints a message to the screen. (print)_____ gets user input. (input)_____ makes decisions in a program. (if)print("Hello") prints Hello. (True)input() gets user input. (True)= is used for comparison. (False โ it's used for assignment)== is used for comparison. (True)echo("Hello") B) print("Hello") C) printf("Hello") D) output("Hello") Answer: Binput() B) get() C) read() D) scan() Answer: Aif do?def function_name(): B) function function_name(): C) fun function_name(): D) define function_name(): Answer: Areturn do?| Term | Definition |
|---|---|
| print() | A) Gets user input |
| input() | B) Displays output |
| if | C) Repeats code |
| while | D) Makes a decision |
| for | E) Repeats a set number of times |
Answers: print()-B, input()-A, if-D, while-C, for-E
return do?Scenario 1: You want to store the names of 5 of your friends in a list. Then, you want to print each name one by one.
Task: Write the code to do this.
Answer:
friends = ["Ada", "Chidi", "Bola", "Tunde", "Kemi"]
for friend in friends:
print(friend)
Scenario 2: You want to create a program that asks the user for their age and tells them if they are a child (under 18) or an adult (18 and above).
Task: Write the code to do this.
Answer:
age = int(input("How old are you? "))
if age >= 18:
print("You are an adult.")
else:
print("You are a child.")
In groups of three, work together to complete all the lab exercises. Each person can take turns being the "driver" (writing code) and the "navigator" (reading and suggesting ideas). Help each other debug and learn.
Choose 3 exercises from this lab that you found most challenging. Redo them from scratch without looking at your previous answers. This will help you understand the concepts better.
Build a program that acts as a Personal Diary. The program should:
Complete all 20 exercises in this lab. For each exercise, write the code, test it, and save it in a file named lab_exercise_X.py (where X is the exercise number). Submit all files to your teacher.
Build a program that allows the user to create a To-Do List. The program should:
Fill-in-the-blank: 1. print, 2. input, 3. if, 4. while, 5. for, 6. list, 7. dictionary, 8. function, 9. return, 10. set
True/False: 1T, 2T, 3F, 4T, 5F, 6T, 7F, 8T, 9T, 10F
Multiple Choice: 1B, 2A, 3A, 4A, 5A, 6A, 7A, 8A, 9A, 10A, 11A, 12A, 13A, 14A, 15A
Matching: print()-B, input()-A, if-D, while-C, for-E
Congratulations on completing the practice lab! You have now practiced all the skills from Level 1. You are ready to move on to Level 2 โ Advanced Python Concepts.
In Level 2, you will learn:
Keep coding, keep exploring, and never stop learning. You have built a strong foundation โ now go and build amazing things!
๐ Congratulations! You have completed the Python Programming Level 1 Practice Lab! ๐
You are now ready for Level 2 โ Advanced Python Concepts!