Year 7 Programming in Python

Lesson 1: Introduction to Programming

Learning Objectives

  • To state what a programming language and program is.
  • To explain what syntax is and why indentation is so important.
  • To write and execute a simple program using the Print function.
  • To debug a simple program.
  • - What is programming?


    1. Watch the following video for an introduction to programming: Why code? and Brainpop Video.
    2. Take the Review Quiz in Brainpop.


  • Python is a programming language
  • A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks.
  • The term programming language usually refers to high-level languages, such as BASIC, C, C++, Python etc
  • Each language has a unique set of keywords (words that it understands) and a special syntax for organizing program instructions.
  • - What is a program?


  • A program is a sequence of instructions that specifies how to perform a computation.
  • The computation might be something mathematical, such as solving a system of equations
  • It can also be a symbolic computation, such as searching and replacing text in a document or (strangely enough) compiling a program.
  • The details look different in different languages, but a few basic instructions appear in just about every language: Input-Get data from the keyboard, a file, or some other device. Output-Display data on the screen or send data to a file or other device. Math-Perform basic mathematical operations like addition and multiplication. Conditional execution-Check for certain conditions and execute the appropriate sequence of statements. Repetition-Perform some action repeatedly, usually with some variation. •
  • Thus, we can describe programming as the process of breaking a large, complex task into smaller and smaller subtasks.
  • That may be a little vague, but we will come back to this topic later when we talk about algorithms.

    Python is a programming language that is easy to learn. It provides a way to write instructions that are simple for a human to understand and clear for the computer to follow.

    1. Open up the Python IDLE. You should see this:

    This is the Shell, which waits for you to type in instructions at the >>> which is called the prompt.

           2. Type in print (“hello world”)

    Hit Enter and you should see this:

         3. Try out a few more print statements. The computer will print out exactly what you put between the (“ ").

        4. Type in print (3+4). What happens? Type in print (5*7). What happens?

        5. So far you have given Python instructions one line at a time – that’s like you being sent to the shop for one item, then going home again to find out what the next item is! Now we need to learn how to write a list of instructions and give them all to Python at once.

    On the Shell, click on File/New Window. You should see a completely empty window.

    Type your code in here (see picture below). Remember, the new version of Python requires you to put brackets around whatever comes after your Print statement.

    Start each instruction on a new line:

        6. Python colours your code so that you can understand it more easily. Anything in green is a String (text) and will print it exactly as it appears.

    Click on File/Save as and save your code with the filename test.

    Click on Run/Run Module (or press F5). You should see your statements appear in the Shell window:

    Did it work? If so, congrats on creating your first program! If not, go back and check your code carefully!

    - What is syntax?

    Click on the button below. Be ready to tell your partner the answers to the following questions in five minutes' time:

    1. What does syntax mean in relation to programming?
    2. Name 5 key words that Python uses.
    3. Explain what indentation is and its importance in Python.


  • In computer science, the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language.
  • Syntax means that special key words and characters combine to result in a program.
  • In any programming language you have to use the correct syntax.
  • E.g. print(“hello”) works. However these do not work: print hello or print (hello)
  • Python is also case sensitive. e.g print(“hello”) works PRINT(“hello”) does not.
  • Try type print("hello") incorrectly and see what happens when you run the program.
  • Now do the Programming time 1 worksheet (page 4 or workbook).
  • Lesson 2: Input in Python (text)
    Learning Objectives

    • Understanding variables.
    • To understand how to ask a user questions and to store their answers in a variable.
    • To understand how to display the user's answers.


    1. Click File/New Window and type the following:

    print("Please type your name in")

    my_name = input ()

    print("Nice to meet you " + my_name)

         2. What happens when you run the program?

         3.  What is my_name?

        4. What does input() mean?

    - What is a variable?


    • my_name in the example above is a variable.
    • Variables are reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
    • Based on the data type of a variable, the interpreter (what reads and executes your commands) allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters (strings) in these variables.
    • In order to write a useful program, we need to be able to ask the user questions, and store their answers as well.
    • The input function first prints the string you give as a parameter (in this case ("What is your name?")), and then it waits for a line to be typed in, and returns the string of characters you typed.

    Lesson 3: Input in Python (numbers)
    Learning Objectives

    • Understanding variables.
    • To understand how to ask a user questions and to store their answers in a variable.
    • To understand how to display the user's answers.


    1. We can ask someone their age and store their answer as a variable.
    2. Open a new window, and type the following:

    age = input ("how old are you?")

    print ("You are " + age)

       3. Run the program. What happens?


    - Can you write a program which takes input from the user on how old they are in years, but displays how old they are in days?


    • You created and assigned values to two variables in the activities above; one called name and the other called age. An assignment statement associates a variable name on the left of the equal sign with the value of an expression calculated from the right of the equal sign. Once a variable is assigned a value, the variable can be used in place of that value.


    Lesson 4: Commenting your code and creating a maths quiz
    Learning Objectives

    • To know how to comment out code and the purpose of commenting.
    • To know how to use the conditional statements.
    • To create a simple maths quiz program using If statements, the print and input functions, and variables to keep score.


    • Comments in Python start with the hash character, #, and extend to the end of the physical line.
    • A comment may appear at the start of a line or following whitespace or code, but not within a string literal. A hash character within a string literal is just a hash character.
    • Comments are to clarify code and are not interpreted by Python.

    Some examples:

    # this is the first comment

    SPAM = 1                       # and this is the second comment

                                         # ... and now a third!

    STRING = "# This is not a comment."


    1. Open up one of your programs and comment the code, making it clear to your partner what the code in your program does. Look at the example below to help you.

    # This program finds out the user's name

    print("Please type your name in")                  #prints message to user

    my_name = input ()                                      #stores user’s name in a variable

    print("Nice to meet you " + my_name)           #displays message


    Learning Objective: To understand and use conditional statements if, else if (elif) and else.

    You can test something to see if it is true.

    Python will respond with either True or False. Try these commands in the shell:

    print (3==5)

    print (5>4)

    print (2<7)

    print (2!=7)

    THEORY: Making choices in Python

    • We can tell the computer to check something and do something different if it is true or if it is false, by using an IF statement.
    • It will only carry out the indented instructions after the if line if the condition is true.
    • You can also include an else: section to tell it what to do if the answer is false.


    1. Type the code below into a new window.

    answer = input("Do cats bark? ")

    if answer == "no":




    2. Type these commands in and run them:

    mark = int(input("Score: "))

    if mark > 80:


    elif mark > 40:




    • Why do we indent If statements?
    • What is elif for?


    - Add another elif command in the middle so that a score of more than 60 is rated as "Super".


    • Open a new window (do this in the interpreter by selecting File – New Window.)
    1. Create the script below, exactly as it appears. Please pay particular attention to the use of spaces, indents and colons. Some programming languages, Java for example make extensive use of { } brackets to mark blocks or phrases of code. Python uses indents instead to mark out separate blocks. So, it is crucial that the indents are used appropriately.

    answer = int(input ("What is 2 + 2?"))

    if answer == 4:

        print("Well done")


        print("Sorry the answer was 4")

        3. Save this program as mathsquiz. Run it to test if it works.


    - While you wait for others to catch up, create some more questions.

    Success criteria for peer assessing your partner's Maths quiz:

    + 4 marks: My partner's Maths quiz program runs without an error.

    + 4 marks: My partner has used if statements.

    + 2 marks: I found my partner's program straightforward and useful.


    Create a test that will check the user’s knowledge of the 12 times multiplication table, with between 4 and 12 questions. Save it with the name 12timestable and it must include a heading, with description, name of creator and date. The program must feature some other use of comments and must work successfully.

    ACTIVITY: MATHS QUIZ CONTINUED - using variables to keep track of score

    1. Open up the mathsquiz program you created previously.
    2. With just a few lines of extra code, it should be possible to maintain a score throughout a game and then give the user some feedback at the end.
    3. Modify your script as indicated below:

    score = 0 #this defines variable score, and sets it as zero

    print("What is 2 + 2?")

    answer = input ()

    answer = int(answer)

    if answer == 4:

        print("Well done")

        score = score + 1 #this increases score by one


        print("Sorry the answer was 4")


    - Do you know how to display the user's score at the end?

    - Open your 12timestable program and add a scoring feature to each question. Add a line that prints the score at the end. If you want to be mean, you might also deduct a point for every wrong answer and add this to your script.

    - What have I learnt so far?

    Click on the button below to take the quiz on what you have learnt so far about Python. You may want to read one more time through all the theory above to ensure you score high marks!

    Lesson 5: Fun with Turtle!
    Learning Objectives

    • To learn that programs are made up of a sequence of instructions that have to be written exactly according to the syntax rules of the language.
    • To write a Python program that draws a simple picture/geometric shape.
    • To understand what a computer library is.
    • To reinforce understanding of repetition and when to use For loops.

    ACTIVITY 1: Importing the Turtle library and drawing a simple square

    1. Type import turtle into the IDLE.
    2. To create a canvas, you need to type t = turtle.Pen().
    3. Move the turtle forward and backward by typing t.fd(x) and t.bk(x) where x = the number of pixels it moves.
    4. Turn the turtle left or right by typing and t.rt(y) where y = the number of degrees it turns - 0 to 360.
    5. Can you draw a square?
    6. EXTENSION: Draw a rectangle, a triangle and a box without corners.

    ACTIVITY 2: Using For loops

    Instead of repeating lines and lines of code, we can use a For loop:


    for i in range (4):


    1. Change your code for your square so that it uses a For loop.

    On the first line, we tell the Pen object to reset itself, then we start a For loop which will count from 1 to 4 with the code range (1, 5). How many lines of code have you reduced your program by? Does it still do the same thing and draw a square?

        2. Now, with a few simple changes to our For loop, we can create something even more interesting. Change your code to the following and run the program:


    for i in range (1, 9):


        3. Can you make a star with even more points? (Hint: you will need to decrease the angle a bit and make it loop 37 times.)

        4. What other stars can you create by maybe reducing the loops and changing the degree of the turn?

         5. Do some research on the Internet to find other commands you can use in the Turtle module. The following website is a good starting point. Change the pen colour, the pen size, pen shape and the pen speed.


    - Can you find out how to colour in shapes you draw (hint: begin_fill() )

    - Draw an octagon and then fill it with a different colour.