Lecture 3: Functions and Modules
"import" command
allows one to access a module (you use module as prefix to a function afterwards, e.g. math.cos(math.pi))
"from" keyword
allows one to use certain functions without having to use module as prefix; however, using import is safer overall
argument
any expression inside a function
module data
(dc definition?) functions that exist within a module
function call vs function definition
1) both are commands to do the function, difference is where they can be placed. function call can be placed anywhere -- in python shell, or inside another module, while function definitions belong inside a module 2) function calls can be called as many times as you want, while you can only define a function once.
module vs script
1) modules are imported (e.g. "import module"), while scripts are run from command line (e.g. python helloApp.py). 2) modules provide functions and variables, while scripts behave like applications. 3) when you run a script, only statements/commands are executed. need to use print() function to display values in scripts; while modules will automatically display values (ask for clarification?) *files look the same but the difference is how you use them*
single line comment
denoted by #, not executed by python
docstring
denoted by triple quotes, acts as multiple-line comment; good for code documentation
print(exp)
displays value of exp (expression) on screen
commands
executed after importing a module; "import" will ignore non-commands
module
file that contains python code; way for python to provide optional functions
interactive shell vs module
for interactive shell, python executes each line separately as you type, while for modules you write them in a code editor (e.g. Atom Editor) and then load into python with "import"
function call
function in an expression
in the example fun(x), fun is the ___ and (x) is the ___
function name; argument
built-in function
function that does not need to be imported in python as it already exists; very few of them in python
help()
help function
other modules
io (read/write from files), random (generate random numbers, can pick any distribution), string (useful string functions), sys (information about OS)
help(module)
prints doctoring and module contents
quit()
quit function
input("Type something ")
will prompt user to type something; value returned is a string, even if it looks like int. other values, such as int(), float(), bool(), etc must be converted in order to be used, not a good way to program
modules must be in ___
working directory; must navigate to folder before running python