Transcript
Page 1: Programming for GCSE Topic 1.3: Python Variables and Types

Programming for GCSETopic 1.3: Python

Variables and Types

Teaching London Computing

William MarshSchool of Electronic Engineering and Computer Science

Queen Mary University of London

Page 2: Programming for GCSE Topic 1.3: Python Variables and Types

Aims• Understand the idea of a variable• Using metaphors to explain variables• Using variables to breakdown complex expressions• Assignment statements• Input from the user

Page 3: Programming for GCSE Topic 1.3: Python Variables and Types

Program Variable • Name for a value

• Names are really important• Name meaning

• Value can change

Which are the following everyday values are like variables?

• Your height• Your age• The credit on your

oyster

Page 4: Programming for GCSE Topic 1.3: Python Variables and Types

Python Variables• Python variable appear when used:

>>> bill = 21>>> bill21>>> bill = "hello">>> bill'hello'

In Python, a variable can

change between an integer and a

string. Confusing?

Page 5: Programming for GCSE Topic 1.3: Python Variables and Types

Using a Variable• A variable can be used instead of a value:

• The output is:

• The variables are: ‘greeting’ and ‘planet’• Any names

greeting = "Hello"planet = "World"print(greeting, planet)

'Hello World'

Page 6: Programming for GCSE Topic 1.3: Python Variables and Types

Errors• A variable must be given a value before it is used:

• Notice:• The message is complex – read it carefully• Only the first error is mentioned

>>> area = length * widthTraceback (most recent call last): File "<pyshell#4>", line 1, in <module> area = length * widthNameError: name 'length' is not defined

Page 7: Programming for GCSE Topic 1.3: Python Variables and Types

Variable as a Memory Location• A variable is a

location in the computer’s memory

• Python takes care of ‘where’ to put the values

"CS"

4

“FUN”

Name Value

subject

number

activity

Page 8: Programming for GCSE Topic 1.3: Python Variables and Types

Decomposition using Variable • How to break a complex calculation down into simpler

steps?• Recurring question in programming

>>> km_mile = (1760 * 36 * 2.54) / 100 / 1000>>> km_mile1.609344

>>> inch_mile = 1760 * 36>>> cm_mile = inch_mile * 2.54>>> m_mile = cm_mile / 100>>> km_mile = m_mile / 1000>>> km_mile1.609344

Page 9: Programming for GCSE Topic 1.3: Python Variables and Types

Assignment• The statement to change a variable is call ‘assignment’

• … can be read as ‘10 is assigned to x’

• How do you read?

>>> x = 10

>>> x = y

Page 10: Programming for GCSE Topic 1.3: Python Variables and Types

Assignment II• “x is assigned the value of y” or “the value of y is assigned

to variable x”

• How are the variables used?• Variable ‘x’ changes value; the value of ‘y’ stays the same• The value of ‘y’ is used (or read); an error occurs if ‘y’ has

never been given a value.• It does not matter if ‘x’ has no value before the statement; if it

does, it is lost

>>> x = y

Page 11: Programming for GCSE Topic 1.3: Python Variables and Types

Quiz – I • What is the value of ‘x’ after these statements have been

executed?

• You can check the answer using the Python shell

x = 5x = x + 3

Page 12: Programming for GCSE Topic 1.3: Python Variables and Types

Quiz – II • What is the values of ‘x’ and ‘y’ after these statements

have been executed?• Each box is a separate problem

• You can check the answers using the Python shell

x = 3y = 2x = x + y

x = 3y = 2 + xx = x + y

x = 3y = 2x = x + yy = x – 2

Page 13: Programming for GCSE Topic 1.3: Python Variables and Types

Input• A make a flexible greeting program let’s ask the name

first

• The output is (if you type the letter underlined):

#Greeting anyonename = input("What's your name?")print("Hello", name)

What's your name?WilliamHello William

Page 14: Programming for GCSE Topic 1.3: Python Variables and Types

Input a Number• Input always reads a string• Must not confuse string and number

• Consider:

• The result is:

#This program calculates your age next year#... unfortunately it does not workage = input("How old are you? ")print("Next year you will be", age+1)

How old are you? 21Traceback (most recent call last): File “age-wrong.py", line 4, in <module> print("Next year you will be", age+1)TypeError: Can't convert 'int' object to str implicitly

Page 15: Programming for GCSE Topic 1.3: Python Variables and Types

Using the ‘int’ function• Use the ‘int’ function to convert a string (of digits) to a

number• Try the corrected program:

#This program calculates your age next yearage = input("How old are you? ")print("Next year you will be", int(age)+1)

Page 16: Programming for GCSE Topic 1.3: Python Variables and Types

Summary• A variable gives a name to a value• Choose a meaningful name

• Reading and Assignment• A variable can be read in an expression• A variable can be changed in an assignment statement

• Use variables • To replace a complex expression with several simpler ones• To hold an ‘input’ string from the user

• Use ‘int’ to convert a string to an ‘integer’


Top Related