Python Python Basics 1. Introduction to Python Python is an interpreted, high-level, dynamically typed programming language. It supports multiple programming paradigms: procedural, object-oriented, and functional. Python uses indentation for code blocks instead of {} . 2. Python Syntax Comments: # This is a comment Multi-line comments: """ This is a multi-line comment """ Variables: No need to declare types. python CopyEdit x = 10 # Integer y = 3.14 # Float name = "Python" # String is_active = True # Boolean 3. Data Types Numeric: int , float , complex Text: str Boolean: True , False Sequence: list , tuple , range Python 1 Set: set , frozenset Mapping: dict Binary: bytes , bytearray , memoryview 4. Operators Arithmetic: + , , , / , // , % , * Comparison: == , != , > , < , >= , <= Logical: and , or , not Assignment: = , += , = , = , /= Membership: in , not in Identity: is , is not Control Flow 1. Conditional Statements python CopyEdit if condition: # code block elif condition: # code block else: # code block 2. Loops For Loop python CopyEdit Python 2 for i in range(5): # 0 to 4 print(i) While Loop python CopyEdit x=0 while x < 5: print(x) x += 1 Loop Control Statements break : Exits the loop continue : Skips the current iteration pass : Placeholder for empty loops Functions 1. Defining Functions python CopyEdit def greet(name): return f"Hello, {name}!" 2. Default Arguments python CopyEdit Python 3 def greet(name="User"): return f"Hello, {name}!" 3. Lambda Functions python CopyEdit add = lambda x, y: x + y print(add(5, 3)) # Output: 8 Data Structures 1. Lists (Mutable) python CopyEdit fruits = ["apple", "banana", "cherry"] fruits.append("mango") # Add item fruits.remove("banana") # Remove item fruits[0] = "grape" # Modify item 2. Tuples (Immutable) python CopyEdit coordinates = (4, 5) 3. Dictionaries Python 4 python CopyEdit person = {"name": "Alice", "age": 25} print(person["name"]) # Alice person["city"] = "New York" # Add key-value pair 4. Sets (Unique Values) python CopyEdit numbers = {1, 2, 3, 4} numbers.add(5) Object-Oriented Programming (OOP) 1. Class & Objects python CopyEdit class Car: def __init__(self, brand, model): self.brand = brand self.model = model def display(self): print(f"Car: {self.brand} {self.model}") car1 = Car("Toyota", "Corolla") car1.display() Python 5 2. Inheritance python CopyEdit class ElectricCar(Car): def __init__(self, brand, model, battery): super().__init__(brand, model) self.battery = battery File Handling 1. Read File python CopyEdit with open("file.txt", "r") as file: content = file.read() 2. Write File python CopyEdit with open("file.txt", "w") as file: file.write("Hello, World!") Modules & Libraries 1. Importing Modules Python 6 python CopyEdit import math print(math.sqrt(16)) # 4.0 2. Creating Modules Create mymodule.py : python CopyEdit def greet(name): return f"Hello, {name}!" Use it in another file: python CopyEdit import mymodule print(mymodule.greet("Alice")) Exception Handling python CopyEdit try: x = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") finally: Python 7 print("Execution finished.") Python Advanced Topics 1. List Comprehension python CopyEdit squares = [x**2 for x in range(10)] 2. Decorators python CopyEdit def decorator(func): def wrapper(): print("Before function call") func() print("After function call") return wrapper @decorator def say_hello(): print("Hello!") say_hello() 3. Multithreading Python 8 python CopyEdit import threading def print_numbers(): for i in range(5): print(i) t = threading.Thread(target=print_numbers) t.start() Python 9