Presentation is loading. Please wait.

Presentation is loading. Please wait.

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.

Similar presentations


Presentation on theme: "This Week More Types boolean string Modules print statement Writing programs if statement Type boolean."— Presentation transcript:

1 This Week More Types boolean string Modules print statement Writing programs if statement Type boolean

2 Review of Functions def Is a keyword parameters zero or more, comma-separated body One or more statements A return statement only exists in a function body Example def square(x): return x**2 def function_name(parameters): body

3 Strings and Print Single ‘…’ and double “…” quotes: ‘This is a string’ and “this is a string.” Triple ```…’’’ quote: ```This is a really long string that spans more than one line.’’’ Concatenation “Good ” + “Morning”  “Good Morning” When we want to output information to the screen we use print(). print(“hello”) print(“5+6=”, 5+6)

4 Modules Want to use some functions often Save them to a file, e.g., filename.py Include them with the Python command import filename Python has builtin functions, e.g., –pow(x,y) returns x y –sqrt(x) returns √x organized into different modules (stored in different files)

5 Writing Programs A module is a program when we make function calls inside the module. How? Put the function call inside a “main clause” which looks like: if __name__ == '__main__': cube_volume(5)

6 Modules Functions stored in a file filename.py import filename or Click Run (with curser in the edit pane) Documentation Strings (docstrings) Use to explain the code Must be a string and the first line in a function or module Use complete, grammatically correct sentences Use parameter names, mention their types and describe the return value and type

7 The __builtins__ Module Functions that belong to Python Some useful 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

8 More on import import math –Need to say math.pow(2,3) to use pow(,) –Prevents ambiguity –But inconvenient to type math.pow 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 pow(2,3)

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

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

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

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

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

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

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

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

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

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

19 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

20 Boolean These objects are either true or false. In Python, the type is bool. Examples >>> 3>5 >>> false >>> 2<10 >>> true

21 Combining Booleans: and, or, not true and true 3 2? true true and false 3 5? false false and false 6 5? false true or false 6 5? true not true not 1<4 false not false not 6<2 true

22 Relational Operators, =, ==, != Precedence: How does Python evaluate 25 < 12 + 35 ? 25 < 47 ? true Order: arithmetic  relational  boolean


Download ppt "This Week More Types boolean string Modules print statement Writing programs if statement Type boolean."

Similar presentations


Ads by Google