Python + Data Analytics Cheat Sheet

Topic Short Definition Example
Variable Used to store a value in memory. You can change it later. x = 5
Data Types Defines kind of data (text, number, list, etc). type(5), type(‘hello’)
List Ordered, changeable collection. Holds mixed types. [1, ‘a’, 3.5]
Tuple Ordered, unchangeable collection. Faster than lists. (1, 2, 3)
Set Unordered, unique elements. No duplicates allowed. {1, 2, 3}
Dictionary Key-value pairs for quick lookup. {‘name’: ‘Alice’, ‘age’: 25}
If/Else Conditional execution of code blocks. if x > 0: print(“positive”)
For Loop Iterates over items or ranges. for i in range(3): print(i)
While Loop Repeats while condition is True. while x < 5: x += 1
Function Reusable block of code. def greet(): return “Hi”
Return Outputs value from a function. return a + b
Input Takes user input as string. input(“Enter name”)
Print Displays text/output. print(“Hello”)
append() Adds item to list. my_list.append(10)
insert() Inserts at position in list. my_list.insert(1, 50)
remove() Removes first occurrence. my_list.remove(10)
del Deletes item or variable. del my_list[0]
slice Extracts part of string/list. list[1:4]
len() Length of list/string. len(“abc”)
sum() Total of list numbers. sum([1,2,3])
min()/max() Find smallest/largest. min([5,1,9])
sorted() Returns sorted list. sorted([3,1,2])
type() Returns type of value. type(3.5)
map() Applies function to all items. map(str, [1,2])
filter() Filters items by condition. filter(lambda x: x>5, [2,7])
range() Creates number sequence. range(1, 5)
enumerate() Gets index with value. enumerate([‘a’,’b’])
zip() Combines two lists. zip([1,2], [‘a’,’b’])
Try/Except Handles errors gracefully. try: 1/0 except: print(“error”)
f-string String formatting with { }. f”Hello {name}”
open() Opens a file. open(“file.txt”)
read()/write() Reads or writes file content. f.read(), f.write()
Class Defines blueprint for object. class Car: pass
Object Instance of a class. my_car = Car()
Inheritance Child class inherits parent. class Dog(Animal): pass
Overriding Child class replaces method. def speak(): print(“Bark”)
Polymorphism Same function, diff types. len(“abc”), len([1,2])
Overloading Same method diff params. def add(a, b=0): return a+b
union() Set union of elements. {1,2}.union({2,3})
intersection() Common in two sets. {1,2}.intersection({2,3})
random.randint() Random int from range. randint(1,10)
random.sample() Random unique sample. sample(range(10), 3)
pandas Library for data analysis. pd.DataFrame()
groupby() Groups & aggregates. df.groupby(“city”)
isnull() Checks for null values. df.isnull()
fillna() Fills missing values. df.fillna(0)
dropna() Drops null rows. df.dropna()
describe() Summary stats of data. df.describe()
numpy Handles arrays, math ops. np.array([1,2,3])
set() Converts list to set (unique). set([1,1,2])
any() True if any True in list. any([0, 1, 0])
all() True if all True in list. all([1, 1, 1])

Share on :

Social Share

Recent Post

© 2024 All rights reserved by Go1digital.com