Python
/
Language
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Language Functions
To end the function definition, enter empty line Global statement, called inside function def myfunc(a): global n return a + n
Definition
1 p45 A function is a named sequence of statements.
""" Function definition
A function is a named sequence of statements
To end the function, you have to enter an empty line
Split a single instruction on multple lines with \
"""
def myfunc(a):
return a%2
def myprint(x):
return
print(myfunc(3)) # 1
print(myprint("2")) # None
print("Hello " + \
"World") # Hello World
Import
To use a function from a module, you have to import it.
""" Import modules
To use a function from a module, you have to import it
When you save your scripts don't use
Python modules names like math, sys, csv
"""
import math
print(math.pi) # 3.141592653589793
print(math.sin(math.pi/2)) # 1.0
Local variable
After the function is called, the local variable is destroyed.
""" Local, global variables
Local variable is destroyed after the function is called
To modify a global variable from within a function, use global statement
"""
def myfunc(a, b):
c = a + b # Look Here
return c
n = 0
def parse():
global n # Look Here
for i in range(10):
n = i
print(myfunc(3,4)) # 7
parse(); print(n) # 9
try:
print(c)
except Exception as e:
print(e) # name c not defined
➥ Questions