This Week More Types boolean string Modules print statement Writing programs if statement Type...
-
Upload
teresa-grayer -
Category
Documents
-
view
234 -
download
2
Transcript of This Week More Types boolean string Modules print statement Writing programs if statement Type...
![Page 1: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/1.jpg)
This Week• More Types
• boolean • string
• Modules
• print statement
• Writing programs
• if statement
• Type boolean
![Page 2: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/2.jpg)
Review of Functions
defIs a keyword
parameters zero or more, comma-separated
bodyOne or more statementsA return statement only exists in a function body
Exampledef square(x): return x**2
def function_name(parameters):
body
![Page 3: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/3.jpg)
Strings and PrintSingle ‘…’ 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)
![Page 4: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/4.jpg)
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 xy
– sqrt(x) returns √x
• organized into different modules (stored in different files)
![Page 5: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/5.jpg)
Writing Programs
A module is a program when we makefunction calls inside the module.
How?Put the function call inside a “main clause”
which looks like:
if __name__ == '__main__':
cube_volume(5)
![Page 6: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/6.jpg)
ModulesModules • 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
![Page 7: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/7.jpg)
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
![Page 8: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/8.jpg)
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)
![Page 9: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/9.jpg)
The “if” StatementEnglish 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”
![Page 10: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/10.jpg)
The “if” StatementPython 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”
![Page 11: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/11.jpg)
The “if” StatementPython example:
def check_temp(temperature):if temperature > 0:
return “above the freezing point”
Otherwise, if the temperature = 0 thenit is “at the freezing point”
Otherwise it is “below the freezing point”
![Page 12: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/12.jpg)
The “if” StatementPython 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”
![Page 13: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/13.jpg)
The “if” StatementPython 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”
![Page 14: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/14.jpg)
The “if” Statement
English Python
If condition if condition:
Otherwise, if condition elif condition:
Otherwise else:
![Page 15: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/15.jpg)
Nested “if” StatementsEnglish 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”
![Page 16: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/16.jpg)
Nested “if” StatementsEnglish example:
def check_temp(temperature):
if temperature > 0:
if the temperature >100 thenit is “above the boiling point”
Otherwise, if the temperature> 37 thenit 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”
![Page 17: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/17.jpg)
Nested “if” StatementsEnglish example:
def check_temp(temperature):
if temperature > 0:
if temperature > 100:
return “above the boiling point”
Otherwise, if the temperature > 37 thenit 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”
![Page 18: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/18.jpg)
Nested “if” StatementsEnglish 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”
![Page 19: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/19.jpg)
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
![Page 20: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/20.jpg)
Boolean
These objects are either true or false.
In Python, the type is bool.
Examples>>> 3>5
>>> false
>>> 2<10
>>> true
![Page 21: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/21.jpg)
Combining Booleans: and, or, nottrue and true
3<4 and 3>2?
true
true and false3<4 and 2>5?
false
false and false6<2 and 2>5?
false
true or false6<10 or 2>5?
true
not truenot 1<4
false
not falsenot 6<2
true
![Page 22: This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.](https://reader035.fdocuments.in/reader035/viewer/2022062303/551c580e5503467b488b5614/html5/thumbnails/22.jpg)
Relational Operators
<, >, <=, >=, ==, !=
Precedence: How does Python evaluate25 < 12 + 35 ?
25 < 47 ?true
Order: arithmetic relational boolean