160 likes | 295 Views
T eaching L ondon C omputing. Programming for GCSE Topic 1.3: Python Variables and Types. William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London. Aims. Understand the idea of a variable Using metaphors to explain variables
E N D
TeachingLondon Computing Programming for GCSETopic 1.3: PythonVariables and Types William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London
Aims • Understand the idea of a variable • Using metaphors to explain variables • Using variables to breakdown complex expressions • Assignment statements • Input from the user
Program Variable • Which are the following everyday values are like variables? • Your height • Your age • The credit on your oyster • Name for a value • Names are really important • Name meaning • Value can change
Python Variables • Python variable appear when used: >>> bill = 21 >>> bill 21 >>> bill = "hello" >>> bill 'hello' In Python, a variable can change between an integer and a string. Confusing?
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'
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 * width Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> area = length * width NameError: name 'length' is not defined
Variable as a Memory Location • A variable is a location in the computer’s memory • Python takes care of ‘where’ to put the values Name Value subject number activity
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_mile 1.609344 >>> inch_mile = 1760 * 36 >>> cm_mile = inch_mile * 2.54 >>> m_mile = cm_mile / 100 >>> km_mile = m_mile / 1000 >>> km_mile 1.609344
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
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
Quiz – I • What is the value of ‘x’ after these statements have been executed? • You can check the answer using the Python shell x = 5 x = x + 3
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 = 3 y = 2 x = x + y x = 3 y = 2 + x x = x + y x = 3 y = 2 x = x + y y = x – 2
Input • A make a flexible greeting program let’s ask the name first • The output is (if you type the letter underlined): #Greeting anyone name = input("What's your name?") print("Hello", name) What's your name?William Hello William
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 work age = input("How old are you? ") print("Next year you will be", age+1) How old are you? 21 Traceback (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
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 year age = input("How old are you? ") print("Next year you will be", int(age)+1)
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’