Python for Data Science Cheat Sheet
Python Basics
Enhance your data science skills at: https://www.justintodata.com/
Basic Data Types and Structures Lists in
l1 = ['data', 1, True, 2.6, [1,2]] Indexing and Slicing 'x' in ['x','s'] 'data' not in 'data science'
l2 = [1,3,2] Start index is 0! Out: True Out: False
l1[0]
Functions: Out: 'data'
len(l2) l1[1:3] # excluding the end index
Out: 3 Out: [1, True] if elif else statement
min(l2) l1[4][1] weather = 'snowing'
max(l2) Out: 2 if weather == 'sunny':
print("Let's go to the park!")
Methods: Operations: elif weather == 'cloudy':
Fast key to run in Jupyter Notebook: Shift + Enter l2.append(4) # append elements l1 + l2 print('Maybe we can go to the park, what do you think?')
Comments: # These are comments l2 Out: ['data', 1, True, 2.6, [1, 2], 1, 3, 2] else:
Out: [1, 3, 2, 4] print("Let's stay at home")
Variables l2.pop() # remove elements, default the last one Out: Let's stay at home
x=5 l2.sort() # sort in order
y=3 Loops
z = 'hello world' for loop while loop
Dictionary (key-value pairs) l = [(1,2),(3,4)] num = 1
Numeric Operations d1 = {'k1':'value1','k2':'value2'} for v1, v2 in l: while num < 10:
Addition: Subtraction: Dictionary Value Query: print(v1+v2) num += 2 # same as num = num + 2
x+y x-y d1['k2'] Out: print(num)
Out: 8 Out: 2 Out: 'value2' 3 Out:
Multiplication: Division: 7 3
x*y x/y Sets 5
Out: 15 Out: 1.6666666666666667 s1 = {1,2,3,4} 7
Remainder of division: To the power of: 9
x%y x ** y list to set Operations: 11
Out: 2 Out: 125 s2 = set([1,2,3,5]) s1 - s2
Out: {4} Define functions
Strings Methods: s1 & s2 def abs_value(k):
a = 'data' s1.add(5) Out: {1,2,3} if k > 0:
question = 'What are you studying?' s1 s1 | s2 return k
answer = "I'm studying data science." Out: {1, 2, 3, 4, 5} Out: {1,2,3,4,5} else:
Functions: Indexing and Slicing: return -k
print(a) Start index is 0! Comparison operators
Out: data a=1 abs_value(-5) # call the function with input of -5
len(a) b=2 Out: 5
Out: 4
type(a) a == 2 # if a equals 2 a<b import packages/libraries
Out: str a[0] Out: False Out: True import packages with alias name: import functions from a package:
Out: 'd' '2' == b a >= b import numpy as np from numpy import arange
Methods: a[2] Out: False Out: False import pandas as pd
a.upper() Out: 't' a != b # if a doesn't equal b
Out: 'DATA' #string[start:stop:step] Out: True More tutorials/cheat sheets
a.lower() a[1:] NumPy: Python NumPy Tutorial: Practical Basics for Data Science
Out: 'data' Out: 'ata' and, or, not Pandas: Learn Python Pandas for Data Science: Quick Tutorial
# index of the first occurrence (1 == 1) and ('h' == 'h') # if both statements are True
a.find('t') Operations: Out: True
Out: 2 print(question + '\n' + answer) (1 == 1) or (2 == 1) # if one of them is True
print('I like {} science'.format(a)) Out: Out: True Prepared for you by: https://www.justintodata.com/
Out: I like data science What are you studying? not(1==1) # the opposite of the statement
I'm studying data science. Out: False