This Week
The string type
Modules
print statement
Writing programs
if statements (time permitting)
The boolean type (time permitting)
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
The Type StringSingle ‘…’ 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”
The print Statement
When we want to output information to the screen we use print().
>>>print(“hello”)hello
>>>print(“5+6=”, 5+6)5+6=11
4
Writing Programs
A file is a program when we makefunction calls inside the file.
Q. How?
Put the function call inside a “main clause” at the end of the file:
if __name__ == '__main__':
cube_volume(5)
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.
Modules
• Python has builtin functions, e.g., – pow(x,y) returns xy
– sqrt(x) returns √x
• Organized into different modules (stored in different files)
• pow(x,y) and sqrt(x) belong to the math module
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)
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
Documentation Strings
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
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”
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”
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”
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”
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”
The “if” Statement
English Python
If condition if condition:
Otherwise, if condition elif condition:
Otherwise else:
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 thenit is “at the freezing point”
Otherwise it is “below the freezing point”
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”
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”
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”
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
Boolean
These objects are either true or false.
In Python, the type is bool.
Examples>>> 3>5
>>> False
>>> 2<10
>>> True
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
Relational Operators
<, >, <=, >=, ==, !=
Precedence: How does Python evaluate25 < 12 + 35 ?
25 < 47 ?True
Order: arithmetic relational boolean