Presentation 03
Presentation 03
function argument
name
• Arguments are
§ Expressions, not values
§ Separated by commas
8/30/22 Functions & Modules 3
Built-In Functions
• Python has several math functions
Arguments can be
§ round(2.34) any expression
§ max(a+3,24)
• You have seen many functions already
§ Type casting functions: int(), float(), bool()
• Documentation of all of these are online
§ https://docs.python.org/3/library/functions.html
§ Most of these are two advanced for us right now
http://docs.python.org/3/library
Function
name
Possible arguments
Module
What the function evaluates to
http://docs.python.org/3/library
Module Contents
# This is a comment
x = 1+2
x = 3*x
x
8/30/22 Functions & Modules 15
Using a Module
Module Contents
x = 1+2
x = 3*x
x
8/30/22 Functions & Modules 16
Using a Module
Module Contents
x = 1+2
x = 3*x
x
8/30/22 Functions & Modules 17
Using a Module
Module Contents
x = 1+2 Commands
Executed on import
x = 3*x
x
8/30/22 Functions & Modules 18
Using a Module
Module Contents
x = 1+2 Commands
Executed on import
x = 3*x
x Not a command.
import ignores this
8/30/22 Functions & Modules 19
Using a Module
# This is a comment
x = 1+2
x = 3*x
x
8/30/22 Functions & Modules 20
Using a Module
Module Script
Module Script
module.py script.py
This file shows how modules work This file shows why we use print
""" """
module.py script.py
This file shows how modules work This file shows why we use print
""" """
module.py script.py
module.py script.py
a s c ri p t,
n We see something this time!
• Looks like nothing n y
happens
e o u r u•
Wh re e x e c u te d
e n ts a
•
only statem • Python did the following:
Python did the following:
§ Executed the assignments § Executed the assignments
§ Skipped the last line § Executed the last line
(‘x’ is not a statement) (Prints the contents of x)
8/30/22 Functions & Modules 31
User Input
>>> input('Type something')
Type somethingabc No space after the prompt.
'abc'
>>> input('Type something: ')
Type something: abc Proper space after prompt.
'abc'
>>> x = input('Type something: ')
Type something: abc
Assign result to variable.
>>> x
'abc'
8/30/22 Functions & Modules 32
Making a Script Interactive
"""
A script showing off input. [wmw2] folder> python script.py
Give me something: Hello
This file shows how to make a script You said: Hello
interactive. [wmw2] folder> python script.py
""" Give me something: Goodbye
You said: Goodbye
x = input("Give me a something: ") [wmw2] folder>
print("You said: "+x)
Not using the
interactive shell