1 / 16

Programming for GCSE Topic 1.3: Python Variables and Types

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

tamar
Download Presentation

Programming for GCSE Topic 1.3: Python Variables and Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TeachingLondon Computing Programming for GCSETopic 1.3: PythonVariables and Types William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

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

  3. 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

  4. 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?

  5. 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'

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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)

  16. 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’

More Related