programming for gcse topic 1.3: python variables and types

Download Programming for GCSE Topic 1.3: Python Variables and Types

Post on 24-Feb-2016




0 download

Embed Size (px)


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 - PowerPoint PPT Presentation


Programming for GCSE

Programming for GCSETopic 1.3: PythonVariables and TypesTeaching London Computing

William MarshSchool of Electronic Engineering and Computer ScienceQueen Mary University of London

AimsUnderstand the idea of a variableUsing metaphors to explain variablesUsing variables to breakdown complex expressionsAssignment statementsInput from the userProgram Variable Name for a value

Names are really importantName meaning

Value can changeWhich are the following everyday values are like variables?

Your heightYour ageThe credit on your oyster

Python VariablesPython variable appear when used:>>> bill = 21>>> bill21>>> bill = "hello">>> bill'hello'In Python, a variable can change between an integer and a string. Confusing? Using a VariableA variable can be used instead of a value:

The output is:

The variables are: greeting and planetAny namesgreeting = "Hello"planet = "World"print(greeting, planet)'Hello World'ErrorsA variable must be given a value before it is used:

Notice:The message is complex read it carefullyOnly the first error is mentioned

>>> area = length * widthTraceback (most recent call last): File "", line 1, in area = length * widthNameError: name 'length' is not defined Variable as a Memory LocationA variable is a location in the computers memory

Python takes care of where to put the values"CS"4FUNName Valuesubject


activityDecomposition 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.609344AssignmentThe statement to change a variable is call assignment

can be read as 10 is assigned to x

How do you read?>>> x = 10>>> x = yAssignment IIx 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 sameThe 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 = yQuiz I What is the value of x after these statements have been executed?

You can check the answer using the Python shellx = 5x = x + 3Quiz 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 shellx = 3y = 2x = x + yx = 3y = 2 + xx = x + y x = 3y = 2x = x + yy = x 2 InputA make a flexible greeting program lets 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 WilliamInput a NumberInput always reads a stringMust not confuse string and numberConsider:

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", line 4, in print("Next year you will be", age+1)TypeError: Can't convert 'int' object to str implicitlyUsing the int functionUse the int function to convert a string (of digits) to a numberTry the corrected program:#This program calculates your age next yearage = input("How old are you? ")print("Next year you will be", int(age)+1)SummaryA variable gives a name to a valueChoose a meaningful nameReading and AssignmentA variable can be read in an expressionA variable can be changed in an assignment statementUse variables To replace a complex expression with several simpler onesTo hold an input string from the userUse int to convert a string to an integer


View more >