160 likes | 244 Views
T eaching L ondon C omputing. Variables and Input. Margaret Derrington KCL Easter 2014. variable, declare, assign, type, value, integer, string, list, array, float. Declaring variables. In some languages VARIABLES (and other elements like functions) have to be DECLARED
E N D
TeachingLondon Computing Variables and Input Margaret Derrington KCL Easter 2014
variable, declare, assign, type, value, integer, string, list, array, float Declaring variables • In some languages VARIABLES (and other elements like functions) have to be DECLARED • You have to start by IDENTIFYING (naming) them and specifying their TYPE, (integer, string, float, list, array…) etc • In python declaration is implicit; when a variable is used, python has a good guess and fixes its type by the value it is ASSIGNED • And actually you can change the type by assigning a value of a different type • If you haven’t yet given a variable a value, you cannot use it, python doesn’t yet know what type it is; you’ll get an error. • We have already met integers (whole numbers) and strings (text) • A float (floating point number) is a decimal, and we will meet lists and arrays later
Check it out… in the shell misconception ALERT!!! In python = does NOT mean equals, it denotes ASSIGNMENT apples = (is given the value) 3 Equals is actually = = !!! >>> apples = 3 >>> apples = apples -1 >>> apples 2 >>> len(apples) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> len(apples) TypeError: object of type 'int' has no len() >>> The function lenonly works on strings, and apples is obviously a number, an integer (whole number) in fact. I ate one! apples = apples - 1 is NOT an equation!!!!! You can do arithmetic with apples but you cannot find its length.
More stuff to check in the shell Type in the lines that begin >>> And see if you can explain what happens >>> apples = 3 >>> apples = = 3 True >>> apples = apples - 0.3 >>> apples 2.7 >>> len (apples) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> len (apples) TypeError: object of type 'float' has no len() >>> >>> apples = "Pink Lady" >>> len (apples) 9 >>> apples= apples-1 Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> apples= apples-1 TypeError: unsupported operand type(s) for -: 'str' and 'int'
More stuff to check in the shell a boolean the statement appes is equal to 3 is either True or False >>> apples = 3 >>> apples = = 3 True >>> apples = apples - 0.3 >>> apples 2.7 >>> len (apples) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> len (apples) TypeError: object of type 'float' has no len() >>> >>> apples = "Pink Lady" >>> len (apples) 9 >>> apples= apples-1 Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> apples= apples-1 TypeError: unsupported operand type(s) for -: 'str' and 'int' I took a big bite out of the apple! apples is not an integer now. We have given it the value 2.7 by subtracting 0.3, so it is a float (floating point number or decimal) apples is now a string So len (apples) works fine! But we cannot do arithmetic with it
Fruit Salad in IDLE • Write these progams into IDLE and run them… see if you can explain what happens • Add the line print ("I have got", fruit, "pieces of fruit") If you change this line to Print (“fruit”) What will happen? Why? The colours should give you a clue! Print (“fruit”) #FruitSalad1 apples=3 pears=5 bananas=7 oranges=4 fruit=apples+pears+oranges+bananas print(fruit) Take a note of this construction; it’s important
Fruit Salad continued • Change your program, and resave it with a new name What happens? Does Python know grapes are fruit? DOH! #FruitSalad2 apples=3 pears=5 bananas=7 oranges=4 grapes= 27 fruit=apples+pears+oranges+bananas print(fruit) print ("I have got", fruit, "pieces of fruit") print ("I have got", fruit, "pieces of fruit and a bunch of grapes!")
Even More Fruit Salad • Change your program, and resave it with a new name What happened? Why? #FruitSalad3 apples=3 pears=5 bananas=7 oranges=4 grapes= 27 bananas= bananas-1 # one got eaten fruit=apples+pears+oranges+bananas print(fruit) print ("I have got", fruit, "pieces of fruit")
Even More Fruit Salad • Change your program, and re-save it with a new name Is this what you expected? What happened? Why? #FruitSalad4 apples = 3 pears = 5 bananas = 7 oranges = 4 grapes = 27 bananas = bananas - 1 # one got eaten fruit = apples+pears+oranges+bananas pears = pears - 2 #over-ripe; threw them away print (fruit) print ("I have got", fruit, "pieces of fruit") Misconception Alert!!!!! Compare this to how a SPREADSHEET works
Values are assigned only WHEN PROGRAMMED TO DO SO Misconception Alert!!!!! #FruitSalad3 apples = 3 pears = 5 bananas = 7 oranges = 4 grapes = 27 bananas = bananas - 1 # one got eaten fruit = apples+pears+oranges+bananas pears = pears - 2 #over-ripe; threw them away print (fruit) print ("I have got", fruit, "pieces of fruit") fruit gets assigned its value here When pears still has the value 5 Compare this to how a SPREADSHEET works
Values are assigned WHEN PROGRAMMED TO DO SO #FruitSalad3 apples = 3 pears = 5 bananas = 7 oranges = 4 grapes = 27 bananas = bananas - 1 # one got eaten fruit = apples+pears+oranges+bananas pears = pears - 2 #over-ripe; threw them away print (fruit) fruit = apples+pears+oranges+bananas print (fruit) print ("I have got", fruit, "pieces of fruit") Misconception Alert!!!!! fruit gets assigned its value here When pears still has the value 5 It will be assigned a new value only if there is an instruction to make it do so Compare this to how a SPREADSHEET works
LEFT AND RIGHT MATTERS!!!! And you cannot just swop them over! • apples = 6 • pears = 7 • apples = = pears will get False • apples = pears # check it to see • print (apples) • print (pears) • HOW do you swop the values of two variables???? will assign the value of pears to apples… check it out Misconception Alert!!!
User input >>> name = input ("Please write your name ") Please write your name Margaret >>> print (name) Margaret >>> • Another way of assigning a value to a variable is to get the user to give it • Eg name = input (“please write your name ”) • You can test this in the shell or include it in a program, so when it gets to this point it will ask for user input and assign the input value to the variable • So if you tell it to print that variable, it will print the value that the user has input • Try it…. in the shell… then write a program to get a user’s name and tell them how many letters it has… Leave a space to make a gap before the input
BUT… anything input from a keyboard is actually a STRING, so… • Keyboard input is text, and if necessary must be converted into integers • You can do this in two stages, or all-in-one >>> age = input ("How old were you last birthday? ") How old were you last birthday? 21 >>> age = int(age) >>> #alternatively >>> age= int(input ("How old were you last birthday? ")) How old were you last birthday? 21 >>> Misconception Alert!!! Check this works by using age = age+1 Which will give an error if the value of age is a string
Variables and assignment : Summary • A variable is a name for a value that can be changed, a bit like a cell in a spreadsheet • The type can be an integer, float, string boolean, list ….etc etc • The type can be changed, • If you forget what type a variable is and try to do inappropriate things to it, you will get errors • The user can give a value to a variable • Keyboard input is always a “string”but you can change it • A variable value does NOT update itself (so its NOT like a spreadsheet) • It only changes when a new value is assigned to it. • = means assignment and variable = value is NOT an equation • Left and right matters • = = means equals and is a boolean it returns True or False • There are also • <= , < • >= , > • != (is NOT equal to) Misconception Alert!!! There are MANY misconceptions possible with variables
ERRORS these are some you will probably come across today • We have looked at errors of type • Which you get it you use a function on the wrong type of variable • There are other errors… • You will gradually learn to make sense of them >>> pring("Hello") Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> pring("Hello") NameError: name 'pring' is not defined >>> • This is another common error, when you mistype something, it tells you that you are using a function or a variable that has not been defined • >>> print ("hello world) • SyntaxError: EOL while scanning string literal • >>> • Another common error… I didn’t finish the string, it got to the EndOfLine without finding the ” Python is also very fussy about indentation… As you will find out! When you fix one error, it may tell you about another… it only bothers with the first one it sees.