Category Archives: Levelled Python

4 – For Loops (Level 5)

For Loop Examples

Print the numbers 1 to 9

for k in range(1,10):
    print(k)

Countdown from 10 to 1

for k in range(10,0,-1):
    print(k)

Print the days of the week

for day in ["Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday"]:
    print(day)

Print the five times table

for k in range(1,11):
    print("5 x {0} = {1}".format(k, 5*k))

For Loop Exercises

Write for loops to output the following sequences of numbers

  1. 0,1,2,3,4,5,6,7,8,9,10
  2. 0,2,4,6,8,10,12,14,16
  3. 1,2,3,4,5, … 97,98,99,100
  4. 7,14,21, … 63,70,77
  5. 20,18,16, … 4,2,0,-2
  6. 2,5,8,11,14,17,20,23,26,29
  7. 99,88,77,66,55,44,33,22,11,0
  8. Numbers 1 to 1000.
  9. Even numbers from 0 to 100.
  10. Odd numbers from -50 to 50
  11. All multiples of 3 up to 500.

Extension

  1. Use a for loop to print the 5 times table up to 12 x 5
  2. Use a for loop to print the 7 times table up to 12 x 7 in the form “3 x 7 = 21”
  3. Use a for loop to print the following sequence: 0.5, 0.4, 0.3, 0.2, 0.1, 0
  4. Use a for loop to print the following sequence: 0.03, 0.02, 0.01, 0, -0.01, -0.02, -0,03
  5. Use a for loop to print five random numbers between 1 and 10
  6. Use a for loop to print the first ten square numbers: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
  7. Use a for loop to print the first ten triangle numbers: 1, 3, 6, 10, 15, 21, 28, 36,45, 55

3 – Python Lists (Level 5)

Sample Code

food = ["Sausage", "eggs", "Bacon", "Beans"]
pupils = ["John", "Jill", "Emily", "Satpal"]
scores = [5,3,6,7,9,1,2]
days = ["Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday"]        

for day in days:
    print(day)

print(food[1])
print(pupils[2:])
print (days[2:4])
print(pupils[:2])
print(days[-2])

print(len(days))
print(max(scores))
print(min(scores))

if "John" in pupils:
    print("Pupil is present")
else:
    print ("Pupil absent")

pupils = pupils + ["Arthur"]
print(pupils)

Exercises

The following questions refer to the sample code. You can type the code into IDLE and run it to help you figure out the answer

  1. Look at the print(food[1]) line. What does the [ 1] do?
  2. How would you print the first item in the list?
  3. If a python list has seven items, what would number would the seventh item be?
  4. Look at the print(pupils[2:]) line. What does [2:] mean?
  5. Look at the print(days[2:4])line. What does [2:4] mean?
  6. Look at the print(days[-2]) line. What does [-2] mean?
  7. What does len do?
  8. What do max and min do?

Now write your own modules to do the following

  1. Create a list called months, containing the months in the year.
  2. Print out all the months, one after the other
  3. Use slicing (e.g. days[2:4}) to print out the spring months: March, April, May
  4. Print out the summer months: June, July, August
  5. Print out the first and last months of the year
  6. Print out the winter months: December, January and February

Research

Use a search engine and online manuals to find out how to get Python to do the following

  1. Reverse the following list: [“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”, “Friday”,”Saturday”] i.e. print out “Saturday”,”Friday”,”Thursday”,… etc
  2. Remove “eggs” from this list food = [“Sausage”, “eggs”, “Bacon”, “Beans”]
  3. Sort the following list into ascending order scores = [5,3,6,7,9,1,2]
  4. Insert “Mushrooms” into this list, just after “eggs”
  5. Count how many times “blue” appears in this list [“red”,”blue”,”blue”,”blue”,”red”,”blue”]

2 – Selection (Level 4)

Sample Code

 Simple Selection

surname = input("Enter your surname\n")
gender = input("Are you M or F?\n")
if gender == "M":
    print("Hello Mr " + surname)
else:
    print("Hello Ms " + surname)

Operators

day = input("Name a day that starts with the letter S\n")
if day == "Saturday" or day == "Sunday":
    print("Correct")
else:
    print("Wrong")
year = int(input("Input your year\n"))
if year>=7 and year <=9:
    print("You are in KS3")
else:
    print("You are not in KS3")

Exercises

  1. Prompt the user as follows: “What’s the capital of France?” Output “Correct” if they enter “Paris”, output “Incorrect” otherwise
  2. Prompt the user as follows: “Name a month that starts with the letter A”: Output “Correct” if they enter “April” or “August”, output “Incorrect” otherwise
  3. Prompt the user as follows: “Name a Beatle”. Output “Correct” if they enter “John”, “Paul”, “George” or “Ringo”, output “Incorrect” otherwise
  4. Prompt the user to enter their age. If they are 18 or over output “You are old enough to vote.” Otherwise, output “You are too young to vote”
  5. Prompt the user to enter their age. If they are aged between 4 and 16 output “You should be at school”
  6. An online whisky shop charges for shipping as follows: One bottle, £5.99; two to five bottles, books £7; more than five bottles, free. Prompt the user to enter the number of bottles bought and output the shipping cost.
  7. An online bookshop charges shipping as follows: Orders less than £10, £2.99; orders £10 and over, free; add on £2.50 for all orders if next day delivery is selected. Prompt the user to enter the cost of the order, and then prompt for next day delivery. Output the shipping cost.

Extension

The modulo operator (%) is very useful. It works out the remainder when one number is divided by another. So

10 % 2 = 0 (Because 10/2 = 5 remainder 0)

10 % 3 = 1 (Because 10/3 = 3 remainder 1)

Use the modulo operator to solve the following problems.

  1. Prompt the user to enter a number. Output if the number is odd or even.
  2. Prompt the user to enter a humber. Output Fizz if the number is divisible by 3, otherwise just output the number
  3. Extend the above problem so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5 and otherwise just output the number.
  4. Now extend the above problem again so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5, output Fizz Buzz if the number is divisible by both 5 and 3 and otherwise just output the number.

1 – Input and Output (Level 3)

Sample Code

Escape Characters

Escape Sequence Character
\n newline
\t tab
\b backspace
\” double quote
\’ single quote
\\ backslash
\uDDDD Unicode character
print("\u0041")

Simple Input

name = input('Enter your name: ')
print('Hello', name)

Format Output

pounds = 2
exchange = 1.38
print("{0} Pounds = {1} Euros".format(pounds, exchange))

Exercise

  1. Use the \t escape character to print out a noughts and crosses grid, as shown below in fig. 1
  2. Prompt the user to enter their (name). Print out “Hello” (name) “I hope you’re well”
  3. Prompt the user to enter their (name) and their (age). Print out (name) ” is ” (age) “years old.”
  4. Prompt the user to enter two numbers. Print out the sum of those numbers.
  5. Prompt the user to enter two numbers. Print out the average of those numbers.
  6. Prompt the user to enter their name and their age. Print out “Hello” (name) “you are” age “years old.”
  7. Get the computer to tell a Knock Knock Joke. “Knock Knock” (wait for input) “Olivia” (wait for input) “No you don’t, I live here”
  8. Print the dandelion shown in fig. 2
Table 1: fig. 1
o x
x o
o x o
      .--.
    .'_\/_'.
    '. /\ .'
      "||"
       || /\
    /\ ||//\)
   (/\\||/
______\||/_______

fig. 2

Extension

  1. Look up the current pounds to dollars exchange rate. Write a program that prompts the user to input a number of pounds and then outputs how many dollars that is.
  2. To work out your BMI, divide your weight in kilograms by your height in metres squared. In other words BMI = w / h*h. Write a program that prompts the user to input their weight and height, and then outputs their BMI.

Python Levels

The Daily Python questions are levelled as follows. It’s my experience that nearly all students are capable of coding at level 3. Some students need help to progress to subsequent levels.

The following are based on my department’s experience in teaching coding. You may disagree with the levels, you may wish to use them as a starting point for further development, either way, I’d be very interested to hear your opinion.

The level numbers start at 3 to fit in with the old KS3 levelling system.

Level 3

  • Output Strings and numbers
  • Concatenate Strings and numbers
  • Use variables
  • Perform simple arithmetic operations
  • Prompt for user input

Level 4

  • Use if statements with Strings
  • Use if statements with numbers
  • Use if else statements
  • Understand difference between addition and concatenation.

Level 5

  • Elif
  • For loops
  • A list
  • Concise comments

Level 6

  • Boolean operators AND OR NOT
  • Counts and iterations while loops
  • Nest if statements

Level 7

  • Nest for, while loops,
  • Functions and parameters
  • 2D Arrays
  • Scope

Level 8

  • Recursion
  • Modular programming
  • Self-documenting code: high level commenting