E N D
Python • Jim Eng • jimeng@umich.edu
Vote on class policy • In lecture we discussed several ways in which you might show credits for content in the web pages you submit in this course. Please vote for the option below you most favor as the standard way in which credit for images (your own and from other sources) and other content should be shown in your assignments. • 31 votes (out of 65)
A credit should be shown in the webpage with each image and with any text or other content from other sources. • Credit for each image and for any text or other content from other sources should be shown as comments within the HTML file so they can be seen by viewing the HTML source. • Credit for each image and for any text or other content from other sources should be shown in "alt" or "title" attributes within the HTML file so they can be seen by hovering over the element. • Credit for each image and for any text or other content from other sources should be collected in a text file to be uploaded along with the other files when submitting an assignment.
A credit should be shown in the webpage with each image and with any text or other content from other sources. 13 • Credit for each image and for any text or other content from other sources should be shown as comments within the HTML file so they can be seen by viewing the HTML source. 7 • Credit for each image and for any text or other content from other sources should be shown in "alt" or "title" attributes within the HTML file so they can be seen by hovering over the element. 7 • Credit for each image and for any text or other content from other sources should be collected in a text file to be uploaded along with the other files when submitting an assignment. 4
Patterns in programming - 1 • Sequential steps • Conditional steps • Repeated steps • Stored and reused steps
add 300 grams of flour add 100 ml of milk add an egg if altitude > 2000: add an egg add 30 grams of salt while there are too many lumps: beat mixture with a fork open and add provided flavor packet
add 300 grams of flour • add 100 ml of milk • add an egg • if altitude > 2000: • add an egg • add 30 grams of salt • while there are too many lumps: • beat mixture with a fork • open and add provided flavor packet
Patterns in programming - 2 • Input • Processing • Output usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf
print "Your guess is", guess answer = 42 if guess < answer : print "Your guess is too low" if guess == answer : print "Congratulations!" if guess > answer : print "Your guess is too high"
Logical expressions True or false? print "Your guess is", guess answer = 42 if guess < answer : print "Your guess is too low" if guess == answer : print "Congratulations!" if guess > answer : print "Your guess is too high"
print "Your guess is", guess answer = 42 if guess == answer : print "Good guess" else: print "Bad guess"
answer = 42 true false guess == answer print “Good Guess” print “Bad guess”
print "Your guess is", guess answer = 42 if guess == answer : print "Congratulations!" else: if guess < answer : print "Your guess is too low" else: print "Your guess is too high"
Nested if-else statement print "Your guess is", guess answer = 42 if guess == answer : print "Congratulations!" else: if guess < answer : print "Your guess is too low" else: print "Your guess is too high"
answer = 42 true false guess == answer true false print “Congratulations” guess < answer print “Too low” print “Too High”
print "Your guess is", guess answer = 42 if guess == answer : print "Congratulations!" elif guess < answer : print "Your guess is too low" else: print "Your guess is too high"
answer = 42 true print “Congrats” guess == answer false true print “Too low” guess < answer false print “Too high”
Variables • A way of assigning a name for an area of memory • A way of saving data temporarily • Called a “variable” because contents can change • Value of variable remains the same until it is changed or discarded
Assignment statements • Sets the value of a variable • Assign the value 42 into the variable named ‘answer’ • Assignment operator (‘=’) not equality operator (‘==’) answer = 42 guess == answer
Elevator-floor conversion example usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf • Two variables • Two assignment statements • Input function • Expression
Expressions wf = usf - 1 • (usf - 1) is an expression • (usf - 1) means retrieve the value of usf and subtract one from it • oh, yeah, and save it in the variable wf
Elevator-floor conversion example • an expression can appear anywhere a variable can usf = input("Enter the US Floor Number: ") print "Non-US Floor Number is ",(usf - 1)
Variable names in Python • Must start with a letter or underscore • Consist of letters, underscores, numbers • Case-sensitive (myVariable is not the same as myvariable) • Choose meaningful, mnemonic variable names
Reserved words and del from not whileas elif global or withassert else if pass yieldbreak except import printclass exec in raisecontinue finally is returndef for lambda try • Have special meanings • Cannot be used as variable names, function names, etc
Constants • Values that never change usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf
print "Your guess is", guess answer = 42 msg = "" if guess == answer : msg = "Congratulations!" elif guess < answer : msg = "Your guess is too low" else: msg = "Your guess is too high" print msg
Strings - 1 txt = "guess=25" print txt[0] print txt[5] print txt[2:4] print txt[:5] print txt[6:] print txt[8]
Strings - 2 think = "happy" + "thoughts" print think think = "happy" + " " +"thoughts" print think
Defining functions - 1 def noParamsNoReturn(): print "This function has no parameters and no return" returndef noParamsReturn(): print "This function has no parameters" return 123 noParamsNoReturn() print noParamsReturn()
Defining functions - 2 def paramsNoReturn(num): print "This function received a parameter of ",num returndef paramsReturn(num): print "This function received a parameter of ",num return num * 5 paramsNoReturn(54) paramsReturn(20)
More examples • Strings, String functions, Concatenation • Types and Conversion • Lists, List functions • Integers and Floats • Your own functions