Python Club: Review Exercises

1. Exercises

Exercise 1: Introduction, Syntax and Indentation

Topics covered: What is Python, syntax, indentation, comments, pip

(a)  Write a program that prints the message: Welcome to Python Club!

(b)  Add a comment above your line explaining what it does.

(c)  The code below has an indentation error. Rewrite it correctly so that it runs.

age = 15
if age >= 13:
print("You are a teenager")

Exercise 2: Variables and Data Types

Topics covered: Variables, data types, type()

(a)  Create four variables: name (your name), age (a whole number), height (a decimal number such as 1.6) and is_student (set to True).

(b)  Use the type() function to print the data type of each variable.

(c)  Change age so that it becomes one more than it was, then print it.

Exercise 3: Strings and Formatting

Topics covered: Strings, string formatting, len()

(a)  Create two variables: student (a name, e.g. "Amaka") and score (a number, e.g. 85). Use an f-string to print a sentence that says the student's name and score.

(b)  Use len() to print how many characters are in the student's name.

(c)  Create the variables first_name and last_name. Join them with a space between and store the result in full_name, then print it.

Exercise 4: Arithmetic Operators

Topics covered: +  -  *  /  //  %  **

A shop sells pens at 150 naira each. A student buys 7 pens.

(a)  Store the price and the quantity in variables. Work out the total cost and print it.

(b)  The student pays with 2000 naira. Work out the change and print it.

(c)  17 sweets are shared equally among 5 children. Use // to find how many each child gets, and % to find how many are left over.

(d)  Use the ** operator to calculate 4 to the power of 3.

Exercise 5: Comparison and Logical Operators

Topics covered: ==  !=  <  >  <=  >=,  and, or, not

Use these values:

x = 12
y = 5
age = 16
has_ticket = True

Predict whether each expression is True or False. Write your prediction on paper first, then check it by printing it in Python.

(a) x > y
(b) x == 10
(c) x != y
(d) x < 20 and y > 10
(e) x < 20 or y > 10
(f) not (x == 12)
(g) age >= 13 and age <= 18
(h) has_ticket and age < 15

Exercise 6: Conditional Statements

Topics covered: if, elif, else

(a)  Create a variable num = 14. Write a program that prints "Even" if the number is even and "Odd" if it is odd. (Hint: use the % operator.)

Exercise 7: Loops

Topics covered: for loops, while loops

(a)  Use a for loop to print every fruit in this list, one per line:

fruits = ["mango", "orange", "banana"]

(b)  Use a while loop to count down from 5 to 1, then print "Go!" when the loop is finished.

(c)  Use a while loop to add up the numbers from 1 to 10 and print the total.

Exercise 8: Functions, Docstrings, Built-in Functions and Modules

Topics covered: Functions, parameters, return values, docstrings, built-in functions, modules

(a)  Write a function called greet that takes one parameter, name, and prints a greeting such as: Hello, Amaka! Call the function with your own name.

(b)  Write a function called multiply that takes two numbers and returns their product. Add a docstring that explains what the function does. Call it with 6 and 7 and print the result.

(c)  Use built-in functions on this list to print the largest number, the smallest number, and how many items are in the list:

numbers = [4, 9, 2, 15, 7]

(d)  Import the math module and use math.sqrt() to print the square root of 81.

Exercise 9: Lists

Topics covered: Lists, indexing, changing items, length, mixed data types, type(), list()

Use this list for parts (a) to (c):

fruits = ["apple", "banana", "cherry", "mango"]

(a)  Print the first item and the last item in the list.

(b)  Print how many items are in the list.

(c)  Change "banana" to "orange", then print the whole list.

(d)  Create a list called mixed that contains a string, a whole number, a decimal number and a boolean. Print the list, then print its data type using type().

(e)  Use the list() constructor to create a list of three names, then print it.

Exercise 10: The range() Function

Topics covered: range() with two and three arguments, range() in a for loop, displaying, indexing and slicing a range, membership, length

(a)  Use a for loop with range() to print the numbers 1 to 5.

(b)  Use range() with three arguments to print the even numbers from 2 to 20.

(c)  Display range(5, 10) as a list.

(d)  Create r = range(10, 100, 10). Print the first value, the last value, and the values at positions 1 to 3 (as a list) using slicing.

(e)  Using the same range r, check whether 50 is in the range, and print the length of the range.