1 / 23

Last Week

Last Week. More Types bool String Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it. This Week. if statement print statement details input builtin function More about strings.

nasnan
Télécharger la présentation

Last Week

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. Last Week More Types • bool • String Modules • Save functions to a file, e.g., filename.py • The file filename.py is a module • We can use the functions in filename.py by importing it

  2. This Week • if statement • print statement details • input builtin function • More about strings

  3. The “if” Statement English example: Check The Temperature: If the temperature > 0 then it is “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

  4. The “if” Statement Python example: def check_temp(temperature): If the temperature > 0 then it is “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

  5. The “if” Statement Python example: def check_temp(temperature): if temperature > 0: return “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

  6. The “if” Statement Python example: def check_temp(temperature): if temperature > 0: return “above the freezing point” elif temperature == 0: return “at the freezing point” Otherwise it is “below the freezing point”

  7. The “if” Statement Python example: def check_temp(temperature): if temperature > 0: return “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

  8. The “if” Statement

  9. Nested “if” Statements English example: Check The Temperature: If the temperature > 0 then if the temperature >100 then it is “above the boiling point” Otherwise, if the temperature> 37 then it is “above body temperature” Otherwise, it is “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

  10. Nested “if” Statements English example: def check_temp(temperature): if temperature > 0: if the temperature >100 then it is “above the boiling point” Otherwise, if the temperature> 37 then it is “above body temperature” Otherwise, it is “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

  11. Nested “if” Statements English example: def check_temp(temperature): if temperature > 0: if temperature > 100: return “above the boiling point” Otherwise, if the temperature > 37 then it is “above body temperature” Otherwise, it is “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

  12. Nested “if” Statements English example: def check_temp(temperature): if temperature > 0: if temperature > 100: return “above the boiling point” elif temperature > 37: return “above body temperature” Otherwise, it is “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

  13. Nested “if” Statements English example: def check_temp(temperature): if temperature > 0: if temperature > 100: return “above the boiling point” elif temperature > 37: return “above body temperature” else: return “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point” >100 >37 and <=100 >0 and <=37

  14. Getting User Input When we want to display information to the screen we use print. print(“hello”) print(5+6) When we want get inputfrom the user we use the builtin function input. >>>input() >>>input(“Enter name:”) input() always returns a string. If the input is an int then we can convert to int: >>>int(input(“Enter your age: “))

  15. Converting Between Types How do we convert between an int and a string and vice versa? >>>int(“1000”) 1000 >>>str(5+6) ‘11’ >>>int(11.9) 11

  16. Strings Using Conversion Specifiers We sometimes would like to insert values of variables into strings: A1 = 60 A2 = 75 A3 = 88 We would like: ‘The average of 60, 75 and 88 is 74.33.’ How do we print this? >>>print(‘The average of’, A1, ‘,’, A2, ‘ and ’, A3, ‘ is ’, (A1+A2+A3)/3) Does this work?

  17. Strings Using Conversion Specifiers We displayed: ‘The average of 60 , 75 and 88 is 74.33333333333333 .’ Q. What’s wrong? A. Spacing is wrong around commas and periods. We have many more decimal places than wanted. Q. How can we fix it? A. Use conversion specifiers. >>>print(‘The average of %d, %d and %d is %.2f’ %(A1, A2, A3, (A1+A2+A3)/3.0)) The average of 60, 75 and 88 is 74.33.

  18. Common Conversion Specifiers %ddisplay the object as a decimal integer %fdisplay the object as a floating point with 6 decimal places %.2f display the object as a floating point with 2 decimal places %s display the object as a string Q. What else do we use % for? A. Modulus. We say that % is overloaded.

  19. More on Strings Q. What is a string exactly? A. A sequence of characters with position numbers called indices. “Hello World”: “H” is at index 0. Q. How can we select parts of a string? A. We can select a particular character by specifying its index: >>>”Hello World”[2] “l” A. We can select part of a string by specifying a range. >>>”Hello Class”[1:6] “ello C”

  20. More on Strings Q. What else can we do to strings? A. There are many methods that apply to strings: • “Hello World”.replace(“World”,“Class”) • “Hello Class”.count(“l”) • “Hello Class”.find(“as”) The functions replace, count and find are calledmethods because they behave like operators. Q. How would you find all the methods for strings? A.dir(str)

  21. String Methods s = “The Toronto Blue Jays need help!” s.isupper():returns True if s is all upper case, False otherwise. s.islower():returns True if s is all lower case, False otherwise. s.isdigit():returns True if s is a number, False otherwise. s.upper():returns a copy of s in all upper case. s.lower():returns a copy of s in all lower case. len(s):returns the length of s. sub in s:returns true if sub is a substring of s. “anna”*4:returns “annaannaannaanna”.

  22. Visiting the Items in a String S Printing out the characters of the string English: for each char in S print the char Python: for char in S: print(char) Notes: char is a variable name for and in are Python key words

  23. for loops Format: for variable in string: statements Example with Strings: name = “Edward” new= ‘’ for letter in name: new = letter + new print(new)

More Related