Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Last Week 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."— Presentation transcript:

1 Last Week 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 Docstrings Use to explain the code Must be a string and the first line in a function or module Include parameter names, mention their types and describe the return value and type

2 This Week More on modules New Python statements if statement print statement details input builtin function for … in …: statement The boolean type

3 Modules Sometimes we want to use some functions frequently Save them to a file, e.g., filename.py Include them with the Python command import filename We call this file a module.

4 Modules Python has builtin functions, e.g., –pow(x,y) returns x y –sqrt(x) returns √x Organized into different modules (stored in different files) pow(x,y) and sqrt(x) belong to the math module

5 More on import import math –Need to say math.sqrt(2,3) to use sqrt() –Prevents ambiguity –But inconvenient to type math.sqrt Solution –Only import specific functions: >>> from math import pow, sqrt –Import all functions when you know there are no conflicts >>> from math import * –Now we can say sqrt(5)

6 The __builtins__ Module Ways to get help with functions: –dir(module): list the functions in a module –help(function): show the docstrings for a function or module –import module: include the functions defined in a module

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

8 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”

9 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”

10 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”

11 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”

12 The “if” Statement EnglishPython If conditionif condition: Otherwise, if conditionelif condition: Otherwiseelse:

13 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”

14 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”

15 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”

16 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”

17 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” Nested “if” Statements >100 >37 and <=100 >0 and <=37

18 Getting User Input When we want to display information to the screen we use print. >>> print(“hello”) >>> print(5+6) When we want get input from the user we use the builtin function input. >>> input() >>> name = input(“Enter name:”)

19 Getting User Input Q. What does input return? A. input() always returns a string. Suppose we need an integer value from the user. >>> age = input(“Enter your age:”) Q. How can we convert input’s return value to be an integer? A. >>> age = int(input(“Enter your age:”))

20 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”“l”

21 More on Strings We can select part of a string by specifying a range. >>>”Hello Class”[1:6] “ello ” Q. What string would “Hello Class”[3:] return? A. >>>”Hello Class”[3:] “lo Class” Q. What string would “Hello Class”[:2] return? A. >>>”Hello Class”[:2] “He”

22 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 called methods because they behave like operators. Q. How would you find all the methods for strings? A. dir(str)

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

24 String Methods len(s): returns the length of s. sub in s: returns true if sub is a substring of s. We know what “anna” + “anna” returns. Q. What about “anna”*4 ? “annaannaannaanna”

25 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

26 for loops Format: for variable in string: statements Example with strings: name = ”Edward” new = “” for letter in name: new = letter + new print(new)

27 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 with our variables? >>>print(‘The average of’, A1, ‘,’, A2, ‘ and ’, A3, ‘ is ’, (A1+A2+A3)/3) Does this work?

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

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


Download ppt "Last Week 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."

Similar presentations


Ads by Google