Basics | Data Types & Collections
Integers |
|
Floats |
|
Strings |
|
Lists |
['John', 'Peter', 'Debora']
|
Tuples |
('table', 'chair', 'rack')
|
|
Tuples are immutable objects, Lists are mutable. |
Sets |
A set is an unordered collection with no duplicate elements |
|
Don't use empty curly braces { } or you will get an empty dictionary |
|
s = {1, 2, 3, 2, 3, 4}
| # {1, 2, 3, 4} |
Dictionaries |
my_cat = { 'size': 'fat'}
|
Basics | Operators [a = 5, b = 10]
Multiplication |
* |
Addition |
+ |
Subtraction |
- |
Division |
/ |
Exponent |
** |
Integer Division |
// |
Modulus / Remainder |
% |
AND |
|
OR |
|
NOT |
|
Equal / Not equal |
|
Bigger / Smaller than |
|
Bigger / Smaller than or equal to |
|
Basics | Operations
Generic |
sum(), range(), min(), max(), input(""), sorted(), import
|
List |
list = [], list[i] = a, list[i], list[i:j:x]
|
String |
string[i], string[i:j:x]
|
Dictionary |
dict = {}, dict[i] = a, dict[i]
|
|
|
Conditionals and Control Flow
IF-ELSE Statement |
name = 'Antony if name == 'Debora': ... print('Hi Debora!') elif name == 'George': ... print('Hi George!') else: ... print('Who are you?')
|
WHILE Loop |
spam = 0 while spam < 5: ... print('Hello, world.') ... spam = spam + 1
|
FOR Loop |
#start, stop, step
for i in range(0, 10, 2): ... print(i)
|
Functions | Modularity and Documentation
Schema Example |
def new_function(x,y): """Descripion of the function, Known as Docstring Arguments: Returns: """ ...number = x**y ...print("Hellow World") ...return number new_function(5,2)
|
Single-line comment |
|
Multi-line comment |
|
File Handling
Open / With |
The with statement automatically closes the file |
Write |
with open('bacon.txt', 'w') as bacon_file: ... bacon_file.write('Hello world!\n')
|
Append |
with open('bacon.txt', 'a') as bacon_file: ... bacon_file.write('Bacon is not a vegetable')
|
Read |
with open('bacon.txt') as bacon_file: ... content = bacon_file.read()
|
|
print(content) Hello world! Bacon is not a vegetable
|
|
|
Exception Handling
Try - Except |
def divide(dividend , divisor): ... try: ... print(dividend / divisor) ... except ZeroDivisionError as e: ... print('You can not divide by 0') ... finally: ... print('Execution finished')
|
OOP | Classes and Objects
Schema Example |
class Number: ... def __init__(self, val): ... self.val = val obj = Number(2) obj.val
|
Inheritance |
Using details from a new class without modifying existing classes |
Encapsulation |
Hiding the details of a class from other objects |
Polymorphism |
Using common operations in different ways for different data |
Common Methods
Strings |
strip(), len(), lower() /upper(), replace(),split(separator)
|
Lists |
append(element), extend(iterable), insert(index, element), remove(element), pop(index)
|
Sets |
add(element), remove(element) union(other_set), intersection(other_set)
|
Tuples |
count(value), index(value)
|
Dictionaries |
.keys(), .values(), .items(), get(key, default), pop(key)
|
JIC
Pandas |
import pandas as pd df = pd.DataFrame df.loc[:]/df.iloc[:] df.describe()
|
|