python jim eng [email protected]. vote on class policy in lecture we discussed several ways in which...
TRANSCRIPT
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"
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 "Good guess" else: print "Bad guess"
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
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"
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
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
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"
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”
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
•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
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
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
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 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
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 thinkthink = "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 123noParamsNoReturn()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 * 5paramsNoReturn(54)paramsReturn(20)
More examples
•Strings, String functions, Concatenation
•Types and Conversion
•Lists, List functions
•Integers and Floats
•Your own functions