Show Menu
Cheatography

Data Science INF111 Cheat Sheet (DRAFT) by

Practical Programming for Data Science @ The University of Sheffield

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basics | Data Types & Collec­tions

Integers
-2, -1, 0, 1, 2, 3, 4, 5
Floats
-1.0, --0.5, 0.0, 0.5
Strings
'a', 'aa', 'Hello!'
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}
Dictio­naries
my_cat = { 'size': 'fat'}

Basics | Operators [a = 5, b = 10]

Multip­lic­ation
*
Addition
+
Subtra­ction
-
Division
/
Exponent
**
Integer Division
//
Modulus / Remainder
%
AND
a AND b
OR
a OR b
NOT
NOT a
Equal / Not equal
a == b, a != b
Bigger / Smaller than
a > b, a < b
Bigger / Smaller than or equal to
a >= b, a <= b

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]
 

Condit­ionals 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 Docume­ntation

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
#in-line
Multi-line comment
"""
lines
"""

File Handling

Open / With
The with statement
automa­tically closes the file
Write
 with open('­bac­on.t­xt', 'w') as bacon_file:
... bacon_­fil­e.w­rit­e('­Hello world!\n')
Append
 with open('­bac­on.t­xt', 'a') as bacon_file:
... bacon_­fil­e.w­rit­e('­Bacon is not a vegeta­ble')
Read
with open('­bac­on.t­xt') as bacon_file:
... content = bacon_­fil­e.r­ead()
 
print(content)
Hello world!
Bacon is not a vegetable
 

Exception Handling

Try - Except
def divide­(di­vidend , divisor): 
... try:
   ... print(­div­idend / divisor)
... except ZeroDi­vis­ion­Error as e:
   ... print('You can not divide by 0')
... finally:
   ... print(­'Ex­ecution finished')

OOP | Classes and Objects

Schema Example
class Number: 
... def __init­__(­self, val):
... self.val = val
obj = Number(2)
obj.val
Inheri­tance
Using details from a new class
without modifying existing classes
Encaps­ulation
Hiding the details of a class
from other objects
Polymo­rphism
Using common operations in
different ways for different data

Common Methods

Strings
strip(), len(), lower() /upper(),
replace(),split(separator)
Lists
append­(el­ement), extend­(it­era­ble), 
insert(index, element),
remove(element), pop(index)
Sets
add(el­ement), remove(element)
union(other_set), inters­ect­ion­(ot­her­_set)
Tuples
count(­value),  index(­value)
Dictio­naries
.keys(), .values(), .items(),
get(key, default), pop(key)

JIC

Pandas
import pandas as pd 
df = pd.Dat­aFrame
df.loc[:]/df.iloc[:]
df.describe()