A201 lab on Wed May 13, 2009 in BH308. We know simple expressions like 1 + 2 and can type them in Python. So far we looked at numbers and operators associated with them. Let's look at strings. What are strings? Strings are sequences of characters between delimiters. Delimiters could be: " (double quote) and ' (single quote). Use them consistently, that is: "what" is a good string and so is 'what' but "what' or 'what" are not. So you have to use the same delimiter. Characters: on the keyboard. Spaces etc. not just letters. >>> 1 1 >>> 1 + 2 3 >>> "what" 'what' >>> 'what' 'what' >>> "what' SyntaxError: EOL while scanning single-quoted string >>> " " ' ' >>> "I can use spaces and lowercase and uppercase and I am happy." 'I can use spaces and lowercase and uppercase and I am happy.' >>> "123" '123' >>> "45" '45' >>> "123" + "45" '12345' >>> To convert strings of digits to integers we use int(...) so >>> "123" + "45" '12345' >>> "1" + "2" '12' >>> int("1") 1 >>> "123" + "45" '12345' >>> int("123") + int("45") 168 >>> int("1") + int("2") 3 >>> Like int(...) we have many useful functions in Python. Each one its own purpose. Functions are also known as: methods, subroutines, procedures, recipe. Arguments to functions are like ingredients to recipes. raw_input is such a useful function. Its name is raw_input. It takes one argument, like int. In the case of int the argument is the string we want converted. raw_input takes a string which it will use to prompt the user with. It will start by printing that string and then it will wait. The user can then type characters. When the user hits enter for the first time raw_input collects that line of characters and gives it to the program where it is invoked. Here are some errors when you invoke int(...) incorrectly. >>> int("34a") Traceback (most recent call last): File "", line 1, in int("34a") ValueError: invalid literal for int() with base 10: '34a' >>> int("2.1") Traceback (most recent call last): File "", line 1, in int("2.1") ValueError: invalid literal for int() with base 10: '2.1' Expressions are evaluated and they return values. You can store values in variables (named locations). The names of variables are made out of letters and digits. They need to start with a letter. So 3 + 4 - 2 evaluates to 7 - 2 which evaluates to 5. We can do that in stages by using two variables: >>> 3 + 4 - 2 5 >>> a = 3 + 4 >>> a 7 >>> a - 2 5 >>> b = a - 2 >>> a 7 >>> b 5 >>> We have two variables, a and b, a has the intermediate result 7, b has the final answer. One assigns a value to a variable via an assignment statement. An assignment statement has two parts separated by a = sign. the = sign acts like an arrow <-- actually. In an assignment statement there is an expression on the right side of the = sign and a variable on the left side. The statement evaluates the expression on the right side and stores the value in the variable that appears on the left side. Now I can tell you that we usually store what raw_input reads in a variable. Like this: name = raw_input("What's your name? ") print "You have typed", name Next we do this in two stages: name = raw_input("What's your name? ") age = raw_input("How old are you? ") print "Well,", name, "next year you will be", age + 1 When we run it it says: >>> ================================ RESTART ================================ >>> What's your name? Larry Bird How old are you? 52 Well, Larry Bird next year you will be Traceback (most recent call last): File "C:/Users/dgerman/Desktop/one.py", line 6, in print "Well,", name, "next year you will be", age + 1 TypeError: cannot concatenate 'str' and 'int' objects >>> So we need to make this change: name = raw_input("What's your name? ") age = raw_input("How old are you? ") print "Well,", name, "next year you will be", int(age) + 1 Then the program runs well: >>> What's your name? Michael Jordan How old are you? 46 Well, Michael Jordan next year you will be 47 >>> ================================ RESTART ================================ >>> What's your name? Larry Bird How old are you? 52 Well, Larry Bird next year you will be 53 >>> Now you can start working on problems 1, 2, 3, 4, 5, 6. Before you leave e-mail me the programs you worked on: dgerman@indiana.edu