1 / 32

Python

Python. Jim Eng jimeng@umich.edu. Vote on class policy.

kelvin
Download Presentation

Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python • Jim Eng • jimeng@umich.edu

  2. 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)

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

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

  5. Patterns in programming - 1 • Sequential steps • Conditional steps • Repeated steps • Stored and reused steps

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

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

  8. Patterns in programming - 2 • Input • Processing • Output usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf

  9. 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"

  10. 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"

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

  12. answer = 42 true false guess == answer print “Good Guess” print “Bad guess”

  13. 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"

  14. 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"

  15. answer = 42 true false guess == answer true false print “Congratulations” guess < answer print “Too low” print “Too High”

  16. 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"

  17. answer = 42 true print “Congrats” guess == answer false true print “Too low” guess < answer false print “Too high”

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

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

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

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

  22. 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)

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

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

  25. Constants • Values that never change usf = input("Enter the US Floor Number: ") wf = usf - 1 print "Non-US Floor Number is ",wf

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

  27. Strings - 1 txt = "guess=25" print txt[0] print txt[5] print txt[2:4] print txt[:5] print txt[6:] print txt[8]

  28. Strings - 2 think = "happy" + "thoughts" print think think = "happy" + " " +"thoughts" print think

  29. 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()

  30. 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)

  31. More examples • Strings, String functions, Concatenation • Types and Conversion • Lists, List functions • Integers and Floats • Your own functions

More Related