230 likes | 382 Views
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.
E N D
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
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”
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”
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”
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”
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”
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”
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”
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”
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”
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
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: “))
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
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?
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.
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.
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”
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)
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”.
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
for loops Format: for variable in string: statements Example with Strings: name = “Edward” new= ‘’ for letter in name: new = letter + new print(new)