- BASICS
- Statements
- Operators
-
Functions
- Incremental
- Errors
- FUNCTIONS
- Recursion
- Objects
- Lambda
- STRINGS
- Immutable
- Raw Strings
- Validation
- Config
- Security
- CLASS
- Definition
- Attributes
- Functional
- Methods
- COLLECTIONS
- Lists
- Dictionaries
- Efficiency
- Tree
- Iterator
- Tuples
- References
- STORAGE
- Files
- Databases
- Pipes
- With Open
- Shelve
- Zip
- Csv
- Json
Definition
""" Specify the name and the sequence of statements.
Later, you can call the function by name.
Split a single instruction on multple lines with backslash.
"""
def f(a):
return a%2
def h(x):
return
print(f(3))
print(h("2"))
print("Hello " + \
"World")
"""
1
None
Hello World
"""
Import
""" To use a function from a module, you have to import it.
When you save your scripts don't use modules names like math, sys, csv.
"""
import math
print( math.pi )
print( math.sin(math.pi/2) )
"""
3.141592653589793
1.0
"""
Local variable
""" A local variable is destroyed after the function is called.
To modify a global variable from within a function, use global statement.
"""
def f(a, b):
c = a + b
return c
n = 0
def h():
global n
# Look Here
for i in range(10):
n = i
return n
print(f(3,4))
print(h())
try:
print(c)
except Exception as e:
print(e)
"""
7
9
name 'c' is not defined
"""
Last update: 203 days ago
- Think Python, Allen Downey / amazon.com
- Machine Learning with Python Cookbook, Chris Albon / amazon.com
- Python for Data Analysis, Wes McKinney / amazon.com
- Python Functions / greenteapress.com
- Python Global Scope / automatetheboringstuff.com
- Exceptions to identation rules / automatetheboringstuff.com
- Exercises / github.com