1. Basic Syntax & Data Types Comments: Single line: # This is a comment Multi-line: """This is a multi-line comment""" Variables: No explicit type declaration. x = 10 name = "Alice" is_student = True Basic Data Types: Integers: 1, 100, -5 Floats: 3.14, 2.0, -0.5 Strings: "hello", 'world', """multi-line""" Booleans: True, False None: Represents absence of a value. Type Conversion: int("123") # -> 123 str(123) # -> "123" float(5) # -> 5.0 2. Operators Arithmetic: +, -, *, /, %, **, // 10 / 3 # -> 3.333... 10 // 3 # -> 3 (floor division) 2 ** 3 # -> 8 (exponentiation) Comparison: ==, !=, <, >, <=, >= Logical: and, or, not Assignment: =, +=, -=, *=, /=, %=, **=, //= 3. Control Flow If/Elif/Else: if condition1: # code if condition1 is True elif condition2: # code if condition1 is False, condition2 is True else: # code if all conditions are False For Loop: Iterating over sequences. for item in iterable: # code for each item for i in range(5): # 0, 1, 2, 3, 4 print(i) While Loop: while condition: # code while condition is True Break & Continue: break : Exits the loop immediately. continue : Skips the rest of the current iteration. 4. Data Structures 4.1. Lists (Mutable, Ordered) Definition: my_list = [1, 2, "three", True] Access: my_list[0] (first), my_list[-1] (last) Slicing: my_list[1:3] (from index 1 up to 3, exclusive) Methods: append(item) : Add to end. insert(idx, item) : Insert at index. remove(item) : Remove first occurrence. pop(idx) : Remove and return item at index (default last). len(list) : Length of list. sort() : Sorts in place. sorted(list) : Returns new sorted list. 4.2. Tuples (Immutable, Ordered) Definition: my_tuple = (1, 2, "three") or 1, 2 Access/Slicing: Same as lists. Use cases: Fixed collections, function return values. 4.3. Dictionaries (Mutable, Unordered, Key-Value Pairs) Definition: my_dict = {"name": "Alice", "age": 30} Access: my_dict["name"] Add/Modify: my_dict["city"] = "NY" Methods: my_dict.keys() : View of keys. my_dict.values() : View of values. my_dict.items() : View of key-value pairs. my_dict.get("key", default) : Get value safely. pop("key") : Remove and return value. 4.4. Sets (Mutable, Unordered, Unique Elements) Definition: my_set = {1, 2, 3} , set([1, 2, 2]) -> {1, 2} Methods: add(item) remove(item) union(), intersection(), difference() 5. Functions Definition: def greet(name): return f"Hello, {name}!" greet("Bob") # -> "Hello, Bob!" Parameters: Default values: def func(a, b=1): Arbitrary arguments: def func(*args): (tuple) Arbitrary keyword arguments: def func(**kwargs): (dict) Lambda Functions (Anonymous): add = lambda x, y: x + y add(2, 3) # -> 5 6. Classes & Objects (OOP) Definition: class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): return f"{self.name} says Woof!" my_dog = Dog("Buddy", 3) my_dog.bark() # -> "Buddy says Woof!" Inheritance: class Poodle(Dog): def __init__(self, name, age, color): super().__init__(name, age) self.color = color 7. Modules & Packages Importing: import math print(math.pi) from datetime import date today = date.today() import pandas as pd 8. File I/O Reading a file: with open("file.txt", "r") as f: content = f.read() # read all lines = f.readlines() # read all lines into a list for line in f: # iterate line by line print(line) Writing to a file: with open("output.txt", "w") as f: # "a" for append f.write("Hello, Python!\n") f.writelines(["Line 1\n", "Line 2\n"]) 9. Error Handling Try/Except: try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") except Exception as e: print(f"An error occurred: {e}") else: print("No error occurred.") finally: print("This always executes.") 10. String Formatting f-strings (Python 3.6+): name = "Charlie" age = 25 print(f"Name: {name}, Age: {age}") pi = 3.14159 print(f"Pi: {pi:.2f}") # -> Pi: 3.14 .format() method: "Name: {}, Age: {}".format(name, age) "Name: {0}, Age: {1}".format(name, age) "Name: {n}, Age: {a}".format(n=name, a=age) 11. List Comprehensions Creating lists concisely: squares = [x**2 for x in range(5)] # -> [0, 1, 4, 9, 16] evens = [x for x in range(10) if x % 2 == 0] # -> [0, 2, 4, 6, 8] 12. Common Built-in Functions len(obj) : Length of sequence/collection. print(*args) : Output to console. input(prompt) : Get user input. range(stop) , range(start, stop) , range(start, stop, step) map(func, iterable) : Apply func to all items. filter(func, iterable) : Filter items based on func. zip(*iterables) : Aggregate elements from iterables. enumerate(iterable) : Get (index, value) pairs. max(), min(), sum()