python jim eng [email protected]. vote on class policy in lecture we discussed several ways in which...

32
Python Jim Eng [email protected]

Upload: allan-blake

Post on 02-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: 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

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)

Page 3: 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

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

Page 4: 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

•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

Page 5: 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

Patterns in programming - 1

•Sequential steps

•Conditional steps

•Repeated steps

•Stored and reused steps

Page 6: 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

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

Page 7: 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

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

Page 8: 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

Patterns in programming - 2

•Input

•Processing

•Output

usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf

Page 9: 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
Page 10: 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

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"

Page 11: 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

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?

Page 12: 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

print "Your guess is", guess answer = 42 if guess == answer : print "Good guess" else: print "Bad guess"

Page 13: 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

print “Bad print “Bad guess”guess”

print “Bad print “Bad guess”guess”

truetrue falsefalse

answer = 42answer = 42answer = 42answer = 42

print “Good print “Good Guess”Guess”

print “Good print “Good Guess”Guess”

guess == answer

Page 14: 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

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"

Page 15: 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

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

Page 16: 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

truetrue falsefalse

answer = 42answer = 42answer = 42answer = 42

print print “Congratulations“Congratulations

””

print print “Congratulations“Congratulations

””

guess == answer

print “Too High”print “Too High”print “Too High”print “Too High”

truetrue falsefalse

print “Too low”print “Too low”print “Too low”print “Too low”

guess < answer

Page 17: 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

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"

Page 18: 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

print “Congrats”print “Congrats”print “Congrats”print “Congrats”

truetrue

falsefalse

answer = 42answer = 42answer = 42answer = 42

guess == answer

truetrue

falsefalse

guess < answer print “Too low”print “Too low”print “Too low”print “Too low”

print “Too high”print “Too high”print “Too high”print “Too high”

Page 19: 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

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

Page 20: 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

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

Page 21: 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

Elevator-floor conversion example

•Two variables

•Two assignment statements

•Input function

•Expression

usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf

Page 22: 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

Expressions

•(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

wf = usf - 1

Page 23: 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

Elevator-floor conversion example

usf = input("Enter the US Floor Number: ") print "Non-US Floor Number is ",(usf - 1)

•an expression can appear anywhere a variable can

Page 24: 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

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

Page 25: 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

Reserved wordsand 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

Page 26: 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

Constants

•Values that never change

usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf

Page 27: 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

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

Page 28: 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

Strings - 1

txt = "guess=25"print txt[0]print txt[5]print txt[2:4]print txt[:5]print txt[6:]print txt[8]

Page 29: 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

Strings - 2

think = "happy" + "thoughts"print thinkthink = "happy" + " " +"thoughts"print think

Page 30: 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

Defining functions - 1

def noParamsNoReturn(): print "This function has no parameters and no return" returndef noParamsReturn():print "This function has no parameters" return 123noParamsNoReturn()print noParamsReturn()

Page 31: 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

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 * 5paramsNoReturn(54)paramsReturn(20)

Page 32: 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

More examples

•Strings, String functions, Concatenation

•Types and Conversion

•Lists, List functions

•Integers and Floats

•Your own functions