Docsity

Advanced Python Cheat Sheet | OOP, Lists, Dictionaries & Interview Notes, Cheat Sheet of Computer science

Toggle sidebar
bg1
Ultimate Python Advanced Cheat Sheet
All in One Reference for Professionals & Learne
Table of ConTenTs
1. Advanced Data Types
2. Functions & Lambdas
3. Decorators & Generators
4. Context Managers
5. Modules & Packages
6. File Handling
7. Exception Handling
8. OOP (Classes & Objects)
9. Useful Built-in Functions
1. Advanced Data Types
# Set operations
a = {1,2,3}
b = {3,4,5}
print(a & b) # Intersection
print(a | b) # Union
# Dictionary comprehension
pf3
pf4

Partial preview of the text

Download Advanced Python Cheat Sheet | OOP, Lists, Dictionaries & Interview Notes and more Cheat Sheet Computer science in PDF only on Docsity!

Ultimate Python Advanced Cheat Sheet

All in One Reference for Professionals & Learne

Table of ConTenTs

1. Advanced Data Types

2. Functions & Lambdas

3. Decorators & Generators

4. Context Managers

5. Modules & Packages

6. File Handling

7. Exception Handling

8. OOP (Classes & Objects)

9. Useful Built-in Functions

  1. Advanced Data Types

Set operations

a = {1,2,3} b = {3,4,5} print(a & b) # Intersection print(a | b) # Union

Dictionary comprehension

squares = {x: x*x for x in range(5)}

  1. Functions & Lambdas

Lambda function

square = lambda x: x*x print(square(5))

Multiple return

def calc(a, b): return a+b, a*b

  1. Decorators def decorator(func): def wrapper(*args, *kwargs): print("Before function") return func(args, **kwargs) return wrapper @decorator def greet(name): print(f"Hello {name}")
  2. Generators

print(f"Hello {self.name}") p = Person("SAGOR") p.greet()

  1. Useful Built-ins

 map(), filter(), zip(), enumerate(),sorted()

 any(), all(), hasattr(), getattr()

  1. Tricks & Tips

 Swap values: a, b = b, a

 List flatten: [y for x in matrix for y in x]

 Conditional expression: x if condition else y