Function Calls Built-in Functions vsModules
Function Calls
Built-in Functions vs Modules
? Python supports expressions with math-like functions
? The number of built-in functions is small
¡́ A function in an expression is a function call
¡́
? Function calls have the form
? Missing a lot of functions you would expect
name(x,y,¡)
function
name
¡́ Example: cos(), sqrt()
? Module: file that contains Python code
argument
? Arguments are
¡́ A way for Python to provide optional functions
? Examples:
¡́ Expressions, not values
¡́ round(2.34)
¡́ To access a module, the import command
¡́ Separated by commas
¡́ max(a+3,24)
¡́ Access the functions using module as a prefix
1
2
Example: Module math
>>> import math
>>> math.cos(0)
To access math
functions
Functions
require math
prefix!
1.0
>>> cos(0)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'cos' is not defined
>>> math.pi
3.141592653589793
Module has
variables too!
>>> math.cos(math.pi)
Reading the Python Documentation
Other Modules
? os
Function
name
¡́ Information about your OS
¡́ Cross-platform features
Possible arguments
? random
¡́ Generate random numbers
¡́ Can pick any distribution
Module
? introcs
What the function evaluates to
¡́ Custom module for the course
¡́ Will be used a lot at start
-1.0
3
4
Interactive Shell vs. Modules
Using a Module
Module Contents
""" A simple module.
This file shows how modules work
"""
Docstring (note the Triple Quotes)
Acts as a multiple-line comment
Useful for code documentation
Single line comment
? Launch in command line
? Type each line separately
? Python executes as you type
? Write in a code editor
(not executed)
# This is a comment
Commands
x = 1+2
Executed on import
x = 3*x
Not
a command.
x
¡́ We use Atom Editor
¡́ But anything will work
? Load module with import
import ignores this
5
6
1
Using a Module
Module Contents
""" A simple module.
This file shows how modules work
"""
# This is a comment
¡°Module data¡± must be
x = 1+2 prefixed by module name
x = 3*x
Prints docstring and
x
module contents
Modules Must be in Working Directory!
Python Shell
>>> import module
>>> x
Traceback (most recent call last):
Module you want
is in this folder
File "", line 1, in
NameError: name 'x' is not defined
>>> module.x
9
>>> help(module)
7
Have to navigate to folder
BEFORE running Python
8
Modules vs. Scripts
Module
Scripts and Print Statements
Script
? Provides functions, variables
¡́ Example: temp.py
? Behaves like an application
¡́ Example: helloApp.py
? import it into Python shell
>>> import temp
>>> temp.to_fahrenheit(100)
? Run it from command line:
python helloApp.y
212.0
>>>
Files look the same. Difference is how you use them.
9
module.py
script.py
""" A simple module.
""" A simple script.
This file shows how modules work
"""
This file shows why we use print
"""
# This is a comment
x = 1+2
x = 3*x
Only difference
x
# This is a comment
x = 1+2
x = 3*x
print(x)
10
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'
11
Numeric Input
¡́ int(), float(), bool(), etc.
>>> x = input('Number: ')
Number: 3
>>> x
Value is a string.
'3'
>>> x + 1
¡́ Error if cannot convert
TypeError: must be str, not int
? input returns a string
¡́ Even if looks like int
¡́ It cannot know better
? You must convert values
? One way to program
¡́ But it is a bad way
¡́ Cannot be automated
>>> x = int(x)
Must convert to
>>> x+1
int.
4
12
2
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
Related searches
- calculator built in windows 10
- vba built in function list
- access built in functions
- systemverilog built in functions
- r built in dataset
- bash shell built in commands
- verilog built in functions
- built in mean function python
- what is being built in my area
- toyotas built in usa
- python built in functions
- python built in types