lec-9-30 - Announcements Next Next assignment due Tues 10/7...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Announcements Next Next assignment due Tues 10/7 “I “I turned in the wrong file.” New New TA - Kartik Pandit ECS 10 10/1 Last Time Data Data types Integers, Integers, eg. 87 Floating Floating point numbers, eg 6.78 Stings, eg. Call me taxi! Stings, eg. “Call me a taxi!” Integers vs strings 5 5 is an integer. Fiona Fiona is a dog. ‘5’ is a string. ‘Fiona’ is a string. Variables Variables Programs Programs Variables x=5 y=x x=2 Assigns the integer 5 to the variable x The variable is on the left The value going into the variable is on the right. Variable is like a box, holding a value Variables 5 X=5 1 Variables Variables 5 y=x 5 2 x=2 5 Why variables? One One reason – You can assign a value in one place and use it lots of places. So its easy to change the value later. Newline Newline “\n” produces a carriage return Two Two ways to do the same thing: print rose\ print “A rose\nis a rose\nis a rose.” rose\ print rose print “A rose” print “is a rose” print ‘is a rose.’ Concatenation An operator An operator on strings “hyper”+”reality” “hyper”+”reality” is a string expression that ression has the value “hyperreality” hyperreality” “Gillian”+”.” has the value “Gillian.” Gillian has the value Gillian Addition Addition is an operator on numbers that happens to be written with the same symbol. 2+5 2+5 is an arithmetic expression that has the that value 7. Different ways to skin a… Here Here are three ways to do the same thing print print “My cat“,catName print print “My cat “+catName “+catName print “My cat“, print My cat print catName (this (this last one works only in programs, not in the IDLE window. The comma prevents going to a new line). 2 The user Getting data from the user x = raw_input(‘Enter a name: ‘) raw_input(‘Enter Th The user enters a string The The string is assigned to the variable x The person running the program. Getting input Use Use the function raw_input() Example: Example: color = raw_input(“ Favorite color: “) Python Python evaluates the function raw_input… by printing the prompt Favorite color: by printing the prompt “Favorite color: “ the the user types in “red” the the string “red” becomes the value of the raw_input function the the variable color gets the value “red” raw_input() makes a string >>> x = raw_input(“Enter a number: ") enter a number: 5 >>> x '5‘ x is assigned the string ‘5’ The only kind of data you can get is a string! Why variables? Reason Reason 1 – You can assign a value in one place and use it lots of places. So its easy to change the value later. Reason Reason 2 – you can get a data from the user and refer to it in your program. Enter Enter to exit Many Many examples in the book end with: raw_input(“Press enter to exit”) Python Python waits for the user to enter something. As soon as the user hits the enter key, the program is done and it exits. Notice Notice the value produced by raw_input() is not raw_input() put into a variable; it is just thrown away. 3 The empty string The variable name contains a string, but it is a string containing no characters. It is called the empty string. is called the empty string The length of string “Daniel” is 6 The length of the empty string “” is 0 Checking for the empty string Using the if statement: if name == “” : name = “Resident” if condition: do some statements More on this next time! 4 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online