Python Programming for S3 – Working with Variables

Part 2 of 5 of Mr Brown's in-depth primer on Python Programming for S3.

  1. Python Programming for S3 – Getting Started
  2. Python Programming for S3: Output, Variables & Input
  3. Python Programming for S3 – Working with Variables

A Teaching Tutorial by Mr Brown, Computer Science teacher at Inverness High School.


The second part of Mr Brown’s primer on Python moves on to explaining how to use Variables.

Task 3

3(a) The About Me program worked for you. We need to be able to write flexible programs that can work for many users in many situations. Implement by typing the EXACT program below using variables in a new repl called “About Anyone”, run, fix and screenshot the output below.

name = "David Benson"
TV_show = "Stranger Things"
Animal = "Tigers"

print ("My name is ",name,".")
print (TV_show," is my favourite TV show.")
print ("My favourite animals are ",Animal," as they are sooooo cute!")

3(b) Change the code so it works for your information then screenshot the changed program and the working output test run below.

3(c) Explain what a Variable is, describe how they are used in programs and why they are an advantage to programmers.

What is a variable?…

Describe how they are used in a program (mention program below)…

The advantages of using variables are…

Task 4

4(a) The About Anyone program could be changed to work for any person in the world using variables but we as the programmer would have to change it each time. Implement the code below using input questions in a repl called “About Anyone Automatic”, test run answer the questions as you and screenshot the resulting program output for your information below.

name = input("Enter your name :  ")
TV_show = input("What is your favourite TV show?  ")
Animal = input("What is your favourite animal?  ")

print ("My name is ",name,".")
print (TV_show," is my favourite TV show.")
print ("My favourite animals are ",Animal," as they are soooo cute!")

4(b) Run your program for 2 friends and answer the questions. When each is done take a snipped screenshot of each of the 2 test runs to paste below.

4(c) Explain using the work above how you have created one program that will work for any person in the world without any more coding from you.

Explain how the program will work for anyone with input and variables…

4(d) Explain the working of an input line in the 3 parts of it that go backwards as shown:

  1. Displayed on screen…
  2. Input from keyboard…
  3. Variable assignment =…

Task 5

5(a) Everything in computer systems is stored and worked with as binary 1s and 0s. Like with Databases when you create data variables in a program you need to set their data types so the device knows how to work with them. Enter the code below in a new repl “School Pupil Number” and screenshot a working test run below.

school_name = input("Enter the school's name -  ")
BGE_pupil_numbers = int(input("Enter the number of pupils in S1-3 in the school -"))
Seniors_pupil_numbers = int(input("Enter the number of pupils in senior phase S4-6 -"))

print ()
print ("SCHOOL NAME:", school_name)

Total_Pupil_Numbers = BGE_pupil_numbers + Seniors_pupil_numbers

print ("The number of pupils in",school_name,"is", Total_Pupil_Numbers,".")

5(b) Answer the questions about data types and the working of the previous program below.

  1. What does int stand for?… What does it do in this program once the user has entered the number?…
  2. Why does school name question not do something similar?…
  3. What is the line that just says print for?…
  4. What happens to anything in “speech marks” in a question or print line?
  5. What happens when a variable like school_name is in a print line?…
  6. What type of arithmetic is used in the program? What does it do the arithmetic with?… Why is a new variable and assignment needed at the start of the maths?…
  7. Explain how you output a mix of sentence text and variables in a print line…
  8. Describe what the program does in plain english…

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button