Variable Types
|
Int |
|
Float |
|
Complex |
|
String |
|
Tuple |
|
List |
|
Dictionary |
|
Set |
|
Booleans |
Basic Functions
|
Prints the string s to the screen |
|
Prints the msg to the screen, waits for user keyboard input and returns the input as string |
|
Returns the number of elements of the sequence x |
|
Returns the help (docstring) of a module or function |
|
Evaluates a string and executes it as a line of code |
|
Evaluates a block of code in a string or a code object |
|
Returns the greatest value in a sequence |
|
Returns the smallest value in a sequence |
|
Returns the variable type of x |
Variable Assignment and Operations
|
Assigns 10 to a |
|
Assigns 5 to b |
|
Evaluates a + b and assigns it to c |
|
Prints the value of c (15) |
If Statements
a = 5
n = 6
if a > n**3:
print("Is much greater")
elif a > n:
print("Is greater")
else:
print("Is lower")
|
For Loop
a = 0
for i in range(1, 101):
a += i
print(a)
|
While Loop
a = 0
i = 1
while i < 101:
a += i
i += 1
print(a)
|
Function
def double(x):
return 2*x
print(double(3))
|
Function with Default Arguments
def multiply(a, b=1):
return a * b
x1 = multiply(3, 4)
x2 = multiply(5)
print(f"x1 = {x1}")
print(f"x2 = {x2}")
|
Function - Keyword Arguments
def kword_func(a, b, c):
print(f"a = {a}")
print(f"b = {b}")
print(f"c = {c}")
kword_func(c=10, a=5, b=2)
|
Lambda Function
f = lambda x, y: x2 + x*y + y2
print(f(2, 3))
|
List Comprehension
lc = [i**2 for i in range(6)]
print(lc)
|
Dictionary Comprehension
from math import factorial
dc = {i:factorial(i) for i in range(7)}
print(dc)
|
{0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720}
Python Keywords
|
Starts an if statement definition |
|
Tests another condition if the previous isn't True |
|
Executes the block of code if all previous tests are False |
|
Returns True only if both statements are True |
|
Returns True if at least one statement is True |
|
Returns the opposite Boolean |
|
Checks if an element is in a sequence |
|
Checks if two elements are equal to each other |
|
Checks a logic test and raises an error message if the test fails |
|
Starts a while loop definition |
|
Starts a for loop definition |
in
(on for loop statements) |
Takes each element of a sequence, one per iteration |
|
Skips to the next loop iteration |
|
Exits loop statement |
|
Starts a function definition |
|
Starts a lambda function definition |
|
Exits function and returns a value |
|
Used to create generators. Returns a value but allows to resume the function |
|
Starts a class definition |
|
Does nothing. Used where a block of text is need syntactically |
|
From a module or library... |
|
Imports a module or function... |
|
With this name... |
|
Starts an exception handling statement |
|
Catches an exception |
|
Executes at the end of the exception handling statement |
|
Throws an error |
|
Make a variable global |
|
Make a variable local |
|
Wraps code block with methods defined by a context manager |
|
Delete a variable from memory |
Write File
a = [1, 3, 2, 5, 6, 2, 4]
b = [3, 5, 1 ,2 ,7, 7, 8]
with open('data.csv', 'w') as file:
for i, j in zip(a, b):
file.write(f"{i}, {j}\n")
|
This code writes a csv file based on the data in a and b
Read File
with open('data.csv', 'r') as file:
for line in file:
print(line)
|
This code reads the file and prints it line by line.
|
|
|