0% found this document useful (0 votes)
11 views98 pages

Python Mastery Guide

The Complete Python Mastery Guide covers essential Python concepts, including installation, fundamentals, data structures, control flow, functions, and modules. It emphasizes Python's simplicity, versatility, and strong community support, making it a popular choice for various applications. The guide also includes best practices, real-world projects, and performance optimization techniques for effective Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views98 pages

Python Mastery Guide

The Complete Python Mastery Guide covers essential Python concepts, including installation, fundamentals, data structures, control flow, functions, and modules. It emphasizes Python's simplicity, versatility, and strong community support, making it a popular choice for various applications. The guide also includes best practices, real-world projects, and performance optimization techniques for effective Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

The Complete Python Mastery Guide

Table of Contents
1. Introduction to Python
2. Python Fundamentals
3. Data Structures
4. Control Flow and Loops
5. Functions and Modules
6. Object-Oriented Programming
7. File Handling and I/O
8. Exception Handling
9. Advanced Python Features
10. Working with Libraries
11. Best Practices and Patterns
12. Real-World Projects
13. Performance Optimization
14. Testing and Debugging
15. Deployment and Automation

---

1. Introduction to Python

Why Python?
Python has become the world's most popular programming language for several compelling
reasons:

 Simple and Readable**: Python's syntax is designed to be intuitive and easy to read,
making it accessible to beginners while powerful enough for experts.
 Versatile**: From web development to data science, machine learning to automation,
Python does it all.
 Massive Ecosystem**: Over 300,000 packages available on PyPI for virtually any task.
 Strong Community**: Millions of developers worldwide contribute to its growth and
improvement.
 High Demand**: Python consistently ranks as one of the most sought-after skills in the
job market.

Python's History and Philosophy


Created by Guido van Rossum in 1991, Python was designed with the philosophy that "code
is read much more often than it is written." This principle is embodied in The Zen of Python:
reated by Guido van Rossum in 1991, Python was designed with the
philosophy that "code is read much more often than it is
written." This principle is embodied in The Zen of Python:

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

reated by Guido van Rossum in 1991, Python was designed with the
philosophy that "code is read much more often than it is
written." This principle is embodied in The Zen of Python:

Setting Up Your Environment

Installation
Windows:

16. Download Python from [Link]


17. Run the installer
18. **CRITICAL**: Check "Add Python to PATH"
19. Verify installation: `python --version`

macOS:

*Windows:**
1. Download Python from [Link]
2. Run the installer
3. **CRITICAL**: Check "Add Python to PATH"
4. Verify installation: `python --version`

**macOS:**

brew install python3

*Windows:**
1. Download Python from [Link]
2. Run the installer
3. **CRITICAL**: Check "Add Python to PATH"
4. Verify installation: `python --version`
**macOS:**

Linux:

*Windows:**
1. Download Python from [Link]
2. Run the installer
3. **CRITICAL**: Check "Add Python to PATH"
4. Verify installation: `python --version`

**macOS:**

sudo apt-get install python3

*Windows:**
1. Download Python from [Link]
2. Run the installer
3. **CRITICAL**: Check "Add Python to PATH"
4. Verify installation: `python --version`

**macOS:**

Essential Tools
Virtual Environments:

*Virtual Environments:**

python -m venv myenv

source myenv/bin/activate # macOS/Linux

myenv\Scripts\activate # Windows

*Virtual Environments:**

Package Management:

*Virtual Environments:**

pip install package_name

pip freeze > [Link]

pip install -r [Link]

*Virtual Environments:**

Code Editors:
 VS Code (recommended)
 PyCharm (professional IDE)
 Jupyter Notebook (data science)

---

2. Python Fundamentals

Variables and Data Types


Python is dynamically typed, meaning you don't need to declare variable types.

ython is dynamically typed, meaning you don't need to declare


variable types.

Numbers
integer = 42

float_num = 3.14159

complex_num = 3 + 4j

Strings
name = "Python"

message = 'Hello, World!'

multiline = """This is a

multi-line string"""

Booleans
is_active = True

is_complete = False

None (represents absence of value)


result = None
sult = None

String Operations
```python

text = "Python Programming"

String methods
print([Link]()) # python programming

print([Link]()) # PYTHON PROGRAMMING

print([Link]()) # Python Programming

print([Link]()) # Remove whitespace

print([Link]()) # ['Python', 'Programming']

print([Link]('Python', 'Java')) # Java Programming

String formatting
name = "Alice"

age = 25

print(f"My name is {name} and I'm {age} years old")

print("My name is {} and I'm {} years old".format(name, age))

me = "Alice"
age = 25
print(f"My name is {name} and I'm {age} years old")
print("My name is {} and I'm {} years old".format(name, age))

Numbers and Math Operations


```python

Basic operations
a, b = 10, 3

print(a + b) # 13 (addition)
print(a - b) # 7 (subtraction)

print(a * b) # 30 (multiplication)

print(a / b) # 3.333... (division)

print(a // b) # 3 (floor division)

print(a % b) # 1 (modulo)

print(a ** b) # 1000 (exponentiation)

Math module
import math

print([Link](16)) # 4.0

print([Link]) # 3.14159...

print([Link]([Link]/2)) # 1.0

print([Link](3.2)) # 4

print([Link](3.8)) # 3

port math
print([Link](16)) # 4.0
print([Link]) # 3.14159...
print([Link]([Link]/2)) # 1.0
print([Link](3.2)) # 4
print([Link](3.8)) # 3

Type Conversion
```python

Converting between types


num_str = "42"

num_int = int(num_str) # 42

num_float = float(num_str) # 42.0

str_num = str(42) # "42"


bool_val = bool(1) # True

bool_val = bool(0) # False

Checking types
print(type(42)) # <class 'int'>

print(isinstance(42, int)) # True

int(type(42)) # <class 'int'>


print(isinstance(42, int)) # True

---

3. Data Structures

Lists
Lists are ordered, mutable collections that can hold any type of data.

ists are ordered, mutable collections that can hold any type of
data.

Creating lists
fruits = ["apple", "banana", "cherry"]

numbers = [1, 2, 3, 4, 5]

mixed = [1, "hello", 3.14, True]

Accessing elements
print(fruits[0]) # apple

print(fruits[-1]) # cherry (last element)

print(fruits[1:3]) # ['banana', 'cherry']

Modifying lists
[Link]("orange") # Add to end

[Link](1, "mango") # Insert at index

[Link]("banana") # Remove by value

[Link]() # Remove last element

del fruits[0] # Remove by index

List operations
numbers = [3, 1, 4, 1, 5, 9, 2, 6]

[Link]() # Sort in place

[Link]() # Reverse in place

sorted_nums = sorted(numbers) # Return new sorted list

List comprehensions
squares = [x**2 for x in range(10)]

evens = [x for x in range(20) if x % 2 == 0]

Common methods
print(len(numbers)) # Length

print(max(numbers)) # Maximum

print(min(numbers)) # Minimum

print(sum(numbers)) # Sum

print([Link](1)) # Count occurrences

print([Link](5)) # Find index

int(len(numbers)) # Length
print(max(numbers)) # Maximum
print(min(numbers)) # Minimum
print(sum(numbers)) # Sum
print([Link](1)) # Count occurrences
print([Link](5)) # Find index

Tuples
Tuples are ordered, immutable collections.

uples are ordered, immutable collections.

Creating tuples
coordinates = (10, 20)

colors = ("red", "green", "blue")

single = (42,) # Note the comma!

Accessing elements
print(coordinates[0]) # 10

print(colors[1:3]) # ('green', 'blue')

Tuple unpacking
x, y = coordinates

r, g, b = colors

Named tuples (from collections


module)
from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])

p = Point(10, 20)

print(p.x, p.y) # 10 20

om collections import namedtuple


Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p.x, p.y) # 10 20

Dictionaries
Dictionaries are key-value pairs, unordered (in Python 3.7+ they maintain insertion order).

ictionaries are key-value pairs, unordered (in Python 3.7+ they


maintain insertion order).

Creating dictionaries
person = {

"name": "Alice",

"age": 25,

"city": "New York"

Accessing values
print(person["name"]) # Alice

print([Link]("age")) # 25

print([Link]("job", "N/A")) # N/A (default)

Modifying dictionaries
person["email"] = "alice@[Link]" # Add key

person["age"] = 26 # Update value

del person["city"] # Delete key

Dictionary methods
print([Link]()) # dict_keys(['name', 'age', 'email'])

print([Link]()) # dict_values(['Alice', 26, 'alice@[Link]'])


print([Link]()) # dict_items([('name', 'Alice'), ...])

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

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
```

Sets
Sets are unordered collections of unique elements.

ets are unordered collections of unique elements.

Creating sets
numbers = {1, 2, 3, 4, 5}

letters = set("hello") # {'h', 'e', 'l', 'o'}

Set operations
set1 = {1, 2, 3, 4}

set2 = {3, 4, 5, 6}

print(set1 | set2) # Union: {1, 2, 3, 4, 5, 6}

print(set1 & set2) # Intersection: {3, 4}

print(set1 - set2) # Difference: {1, 2}

print(set1 ^ set2) # Symmetric difference: {1, 2, 5, 6}

Set methods
[Link](6) # Add element

[Link](3) # Remove element (raises error if not found)


[Link](10) # Remove element (no error if not found)

[Link]() # Remove and return arbitrary element

Checking membership
print(3 in numbers) # True

print(10 in numbers) # False

int(3 in numbers) # True


print(10 in numbers) # False

---

4. Control Flow and Loops

Conditional Statements
```python

age = 18

if age < 13:

print("Child")

elif age < 18:

print("Teenager")

elif age < 65:

print("Adult")

else:

print("Senior")

Ternary operator
status = "Adult" if age >= 18 else "Minor"

Multiple conditions
x, y = 10, 20

if x > 5 and y > 15:

print("Both conditions true")

if x > 15 or y > 15:

print("At least one condition true")

if not x < 5:

print("x is not less than 5")

y = 10, 20
if x > 5 and y > 15:
print("Both conditions true")

if x > 15 or y > 15:


print("At least one condition true")

if not x < 5:
print("x is not less than 5")

For Loops
```python

Iterating over sequences


fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

print(fruit)

Using range
for i in range(5): # 0, 1, 2, 3, 4

print(i)

for i in range(2, 10, 2): # 2, 4, 6, 8

print(i)
Enumerate (get index and value)
for index, fruit in enumerate(fruits):

print(f"{index}: {fruit}")

Dictionary iteration
person = {"name": "Alice", "age": 25}

for key in person:

print(key)

for value in [Link]():

print(value)

for key, value in [Link]():

print(f"{key}: {value}")

Loop control
for i in range(10):

if i == 3:

continue # Skip this iteration

if i == 7:

break # Exit the loop

print(i)

r i in range(10):
if i == 3:
continue # Skip this iteration
if i == 7:
break # Exit the loop
print(i)
While Loops
```python

Basic while loop


count = 0

while count < 5:

print(count)

count += 1

While with else


count = 0

while count < 3:

print(count)

count += 1

else:

print("Loop completed")

Infinite loop with break


while True:

user_input = input("Enter 'quit' to exit: ")

if user_input.lower() == 'quit':

break

print(f"You entered: {user_input}")

ile True:
user_input = input("Enter 'quit' to exit: ")
if user_input.lower() == 'quit':
break
print(f"You entered: {user_input}")
Advanced Loop Patterns
```python

Nested loops
for i in range(3):

for j in range(3):

print(f"({i}, {j})")

List comprehension with condition


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

evens = [x for x in numbers if x % 2 == 0]

squares = [x**2 for x in numbers if x % 2 == 0]

Dictionary comprehension
word = "hello"

char_count = {char: [Link](char) for char in set(word)}

Zip (iterate over multiple sequences)


names = ["Alice", "Bob", "Charlie"]

ages = [25, 30, 35]

for name, age in zip(names, ages):

print(f"{name} is {age} years old")

mes = ["Alice", "Bob", "Charlie"]


ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")

---
5. Functions and Modules

Defining Functions
```python

Basic function
def greet():

print("Hello, World!")

greet()

Function with parameters


def greet_person(name):

print(f"Hello, {name}!")

greet_person("Alice")

Function with return value


def add(a, b):

return a + b

result = add(5, 3)

print(result) # 8

Default parameters
def greet_with_title(name, title="Mr."):

print(f"Hello, {title} {name}")

greet_with_title("Smith") # Hello, Mr. Smith

greet_with_title("Johnson", "Dr.") # Hello, Dr. Johnson


Keyword arguments
def describe_person(name, age, city):

print(f"{name} is {age} years old from {city}")

describe_person(name="Alice", age=25, city="NYC")

describe_person(age=30, city="LA", name="Bob")

Variable number of arguments


def sum_all(*args):

return sum(args)

print(sum_all(1, 2, 3, 4, 5)) # 15

def print_info(**kwargs):

for key, value in [Link]():

print(f"{key}: {value}")

print_info(name="Alice", age=25, city="NYC")

f sum_all(*args):
return sum(args)

print(sum_all(1, 2, 3, 4, 5)) # 15

def print_info(**kwargs):
for key, value in [Link]():
print(f"{key}: {value}")

print_info(name="Alice", age=25, city="NYC")

Lambda Functions
```python

Lambda (anonymous) functions


square = lambda x: x**2
print(square(5)) # 25

Lambda with multiple arguments


add = lambda x, y: x + y

print(add(3, 4)) # 7

Lambda with built-in functions


numbers = [1, 2, 3, 4, 5]

squared = list(map(lambda x: x**2, numbers))

evens = list(filter(lambda x: x % 2 == 0, numbers))

Lambda in sorting
students = [("Alice", 25), ("Bob", 20), ("Charlie", 30)]

[Link](key=lambda x: x[1]) # Sort by age

udents = [("Alice", 25), ("Bob", 20), ("Charlie", 30)]


[Link](key=lambda x: x[1]) # Sort by age

Scope and Closures


```python

Local vs Global scope


x = 10 # Global variable

def my_function():

x = 5 # Local variable

print(f"Local x: {x}")

my_function()

print(f"Global x: {x}")
Using global keyword
count = 0

def increment():

global count

count += 1

increment()

print(count) # 1

Closures
def outer_function(x):

def inner_function(y):

return x + y

return inner_function

add_five = outer_function(5)

print(add_five(3)) # 8

f outer_function(x):
def inner_function(y):
return x + y
return inner_function

add_five = outer_function(5)
print(add_five(3)) # 8

Modules and Packages


```python

Importing modules
import math

print([Link](16))
Importing specific functions
from math import sqrt, pi

print(sqrt(25))

print(pi)

Importing with alias


import numpy as np

import pandas as pd

Creating your own module


Save this in [Link]
def greet(name):

return f"Hello, {name}!"

PI = 3.14159

Using your module


import mymodule

print([Link]("Alice"))

print([Link])

from mymodule import greet, PI


```

Decorators
```python
Basic decorator
def my_decorator(func):

def wrapper():

print("Before function call")

func()

print("After function call")

return wrapper

@my_decorator

def say_hello():

print("Hello!")

say_hello()

Decorator with arguments


def timer(func):

import time

def wrapper(*args, **kwargs):

start = [Link]()

result = func(*args, **kwargs)

end = [Link]()

print(f"{func.__name__} took {end - start:.2f} seconds")

return result

return wrapper

@timer

def slow_function():

import time
[Link](1)

return "Done"

slow_function()

f timer(func):
import time
def wrapper(*args, **kwargs):
start = [Link]()
result = func(*args, **kwargs)
end = [Link]()
print(f"{func.__name__} took {end - start:.2f} seconds")
return result
return wrapper

@timer
def slow_function():
import time
[Link](1)
return "Done"

slow_function()

---

6. Object-Oriented Programming

Classes and Objects


```python

Defining a class
class Person:

def __init__(self, name, age):

[Link] = name # Instance attribute

[Link] = age

def greet(self):

return f"Hello, my name is {[Link]}"


def have_birthday(self):

[Link] += 1

return f"Happy {[Link]}th birthday!"

Creating objects
person1 = Person("Alice", 25)

person2 = Person("Bob", 30)

print([Link]) # Alice

print([Link]()) # Hello, my name is Alice

print(person1.have_birthday()) # Happy 26th birthday!

rson1 = Person("Alice", 25)


person2 = Person("Bob", 30)

print([Link]) # Alice
print([Link]()) # Hello, my name is Alice
print(person1.have_birthday()) # Happy 26th birthday!

Class Attributes and Methods


```python

class Car:

# Class attribute (shared by all instances)

wheels = 4

def __init__(self, make, model, year):

# Instance attributes

[Link] = make

[Link] = model

[Link] = year

[Link] = 0

# Instance method
def drive(self, miles):

[Link] += miles

return f"Drove {miles} miles"

# Class method

@classmethod

def from_string(cls, car_string):

make, model, year = car_string.split("-")

return cls(make, model, int(year))

# Static method

@staticmethod

def is_valid_year(year):

return 1886 <= year <= 2024

Using the class


car1 = Car("Toyota", "Camry", 2020)

print([Link]) # 4

print([Link](100)) # Drove 100 miles

print([Link]) # 100

Using class method


car2 = Car.from_string("Honda-Civic-2019")

Using static method


print(Car.is_valid_year(2020)) # True

print(Car.is_valid_year(1800)) # False
int(Car.is_valid_year(2020)) # True
print(Car.is_valid_year(1800)) # False

Inheritance
```python

Parent class
class Animal:

def __init__(self, name):

[Link] = name

def speak(self):

raise NotImplementedError("Subclass must implement abstract method")

Child classes
class Dog(Animal):

def speak(self):

return f"{[Link]} says Woof!"

class Cat(Animal):

def speak(self):

return f"{[Link]} says Meow!"

Using inheritance
dog = Dog("Buddy")

cat = Cat("Whiskers")

print([Link]()) # Buddy says Woof!

print([Link]()) # Whiskers says Meow!

Method overriding and super()


class Vehicle:

def __init__(self, brand):

[Link] = brand

def start(self):

return f"{[Link]} vehicle starting..."

class ElectricCar(Vehicle):

def __init__(self, brand, battery_capacity):

super().__init__(brand) # Call parent's __init__

self.battery_capacity = battery_capacity

def start(self):

return f"{[Link]} electric car starting silently..."

def charge(self):

return f"Charging {self.battery_capacity}kWh battery"

tesla = ElectricCar("Tesla", 75)

print([Link]()) # Tesla electric car starting silently...

print([Link]()) # Charging 75kWh battery

ass Vehicle:
def __init__(self, brand):
[Link] = brand

def start(self):
return f"{[Link]} vehicle starting..."

class ElectricCar(Vehicle):
def __init__(self, brand, battery_capacity):
super().__init__(brand) # Call parent's __init__
self.battery_capacity = battery_capacity

def start(self):
return f"{[Link]} electric car starting silently..."

def charge(self):
return f"Charging {self.battery_capacity}kWh battery"
tesla = ElectricCar("Tesla", 75)
print([Link]()) # Tesla electric car starting silently...
print([Link]()) # Charging 75kWh battery

Encapsulation and Property Decorators


``python
class BankAccount:
def __init__(self, owner, balance=0):
[Link] = owner
self.__balance = balance # Private attribute (name
mangling)

def deposit(self, amount):


if amount > 0:
self.__balance += amount
return f"Deposited ${amount}"
return "Invalid amount"

def withdraw(self, amount):


if 0 < amount <= self.__balance:
self.__balance -= amount
return f"Withdrew ${amount}"
return "Insufficient funds or invalid amount"

@property
def balance(self):
return self.__balance

@[Link]
def balance(self, value):
if value >= 0:
self.__balance = value
else:
raise ValueError("Balance cannot be negative")

account = BankAccount("Alice", 1000)


print([Link](500)) # Deposited $500
print([Link](200)) # Withdrew $200
print([Link]) # 1300
[Link] = 2000 # Using setter
print([Link]) # 2000

class BankAccount:

def __init__(self, owner, balance=0):


[Link] = owner

self.__balance = balance # Private attribute (name mangling)

def deposit(self, amount):

if amount > 0:

self.__balance += amount

return f"Deposited ${amount}"

return "Invalid amount"

def withdraw(self, amount):

if 0 < amount <= self.__balance:

self.__balance -= amount

return f"Withdrew ${amount}"

return "Insufficient funds or invalid amount"

@property

def balance(self):

return self.__balance

@[Link]

def balance(self, value):

if value >= 0:

self.__balance = value

else:

raise ValueError("Balance cannot be negative")

account = BankAccount("Alice", 1000)

print([Link](500)) # Deposited $500

print([Link](200)) # Withdrew $200

print([Link]) # 1300

[Link] = 2000 # Using setter


print([Link]) # 2000

``python
class BankAccount:
def __init__(self, owner, balance=0):
[Link] = owner
self.__balance = balance # Private attribute (name
mangling)

def deposit(self, amount):


if amount > 0:
self.__balance += amount
return f"Deposited ${amount}"
return "Invalid amount"

def withdraw(self, amount):


if 0 < amount <= self.__balance:
self.__balance -= amount
return f"Withdrew ${amount}"
return "Insufficient funds or invalid amount"

@property
def balance(self):
return self.__balance

@[Link]
def balance(self, value):
if value >= 0:
self.__balance = value
else:
raise ValueError("Balance cannot be negative")

account = BankAccount("Alice", 1000)


print([Link](500)) # Deposited $500
print([Link](200)) # Withdrew $200
print([Link]) # 1300
[Link] = 2000 # Using setter
print([Link]) # 2000

Special Methods (Magic Methods)


``python
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"Vector({self.x}, {self.y})"

def __repr__(self):
return f"Vector(x={self.x}, y={self.y})"

def __add__(self, other):


return Vector(self.x + other.x, self.y + other.y)

def __len__(self):
return int((self.x**2 + self.y**2)**0.5)

def __eq__(self, other):


return self.x == other.x and self.y == other.y

v1 = Vector(3, 4)
v2 = Vector(1, 2)

print(v1) # Vector(3, 4)
print(v1 + v2) # Vector(4, 6)
print(len(v1)) # 5
print(v1 == Vector(3, 4)) # True

class Vector:

def __init__(self, x, y):

self.x = x

self.y = y

def __str__(self):

return f"Vector({self.x}, {self.y})"

def __repr__(self):

return f"Vector(x={self.x}, y={self.y})"

def __add__(self, other):

return Vector(self.x + other.x, self.y + other.y)

def __len__(self):

return int((self.x**2 + self.y**2)**0.5)


def __eq__(self, other):

return self.x == other.x and self.y == other.y

v1 = Vector(3, 4)

v2 = Vector(1, 2)

print(v1) # Vector(3, 4)

print(v1 + v2) # Vector(4, 6)

print(len(v1)) #5

print(v1 == Vector(3, 4)) # True

``python
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y

def __str__(self):
return f"Vector({self.x}, {self.y})"

def __repr__(self):
return f"Vector(x={self.x}, y={self.y})"

def __add__(self, other):


return Vector(self.x + other.x, self.y + other.y)

def __len__(self):
return int((self.x**2 + self.y**2)**0.5)

def __eq__(self, other):


return self.x == other.x and self.y == other.y

v1 = Vector(3, 4)
v2 = Vector(1, 2)

print(v1) # Vector(3, 4)
print(v1 + v2) # Vector(4, 6)
print(len(v1)) # 5
print(v1 == Vector(3, 4)) # True

---
7. File Handling and I/O

Reading and Writing Files


```python

Reading files
with open('[Link]', 'r') as file:

content = [Link]()

print(content)

Reading line by line


with open('[Link]', 'r') as file:

for line in file:

print([Link]())

Reading all lines


with open('[Link]', 'r') as file:

lines = [Link]()

print(lines)

Writing files
with open('[Link]', 'w') as file:

[Link]("Hello, World!\n")

[Link]("This is a new line.")

Appending to files
with open('[Link]', 'a') as file:
[Link]("\nThis line is appended.")

Writing multiple lines


lines = ["Line 1\n", "Line 2\n", "Line 3\n"]

with open('[Link]', 'w') as file:

[Link](lines)

nes = ["Line 1\n", "Line 2\n", "Line 3\n"]


with open('[Link]', 'w') as file:
[Link](lines)

Working with CSV Files


```python

import csv

Reading CSV
with open('[Link]', 'r') as file:

reader = [Link](file)

for row in reader:

print(row)

Reading CSV with DictReader


with open('[Link]', 'r') as file:

reader = [Link](file)

for row in reader:

print(row['name'], row['age'])

Writing CSV
data = [
['name', 'age', 'city'],

['Alice', 25, 'NYC'],

['Bob', 30, 'LA']

with open('[Link]', 'w', newline='') as file:

writer = [Link](file)

[Link](data)

Writing CSV with DictWriter


with open('[Link]', 'w', newline='') as file:

fieldnames = ['name', 'age', 'city']

writer = [Link](file, fieldnames=fieldnames)

[Link]()

[Link]({'name': 'Alice', 'age': 25, 'city': 'NYC'})

[Link]({'name': 'Bob', 'age': 30, 'city': 'LA'})

th open('[Link]', 'w', newline='') as file:


fieldnames = ['name', 'age', 'city']
writer = [Link](file, fieldnames=fieldnames)
[Link]()
[Link]({'name': 'Alice', 'age': 25, 'city': 'NYC'})
[Link]({'name': 'Bob', 'age': 30, 'city': 'LA'})

Working with JSON


```python

import json

Reading JSON
with open('[Link]', 'r') as file:

data = [Link](file)
print(data)

Writing JSON
data = {

"name": "Alice",

"age": 25,

"city": "NYC"

with open('[Link]', 'w') as file:

[Link](data, file, indent=2)

JSON to string
json_string = [Link](data, indent=2)

print(json_string)

String to JSON
parsed = [Link](json_string)

print(parsed)

rsed = [Link](json_string)
print(parsed)

Working with Directories


```python

import os

import shutil

Working with paths


current_dir = [Link]()

print(current_dir)

List files in directory


files = [Link]('.')

print(files)

Create directory
[Link]('new_folder', exist_ok=True)

Check if path exists


print([Link]('new_folder'))

print([Link]('[Link]'))

print([Link]('new_folder'))

Join paths
path = [Link]('folder', 'subfolder', '[Link]')

print(path)

Using pathlib (modern approach)


from pathlib import Path

path = Path('folder/subfolder/[Link]')

print([Link]) # folder/subfolder

print([Link]) # [Link]

print([Link]) # file

print([Link]) # .txt
Create directories
Path('new_folder').mkdir(exist_ok=True)

List files
for file in Path('.').iterdir():

print(file)

r file in Path('.').iterdir():
print(file)

---

8. Exception Handling

Basic Exception Handling


```python

Try-except block
try:

result = 10 / 0

except ZeroDivisionError:

print("Cannot divide by zero!")

Multiple exceptions
try:

number = int(input("Enter a number: "))

result = 10 / number

except ValueError:

print("Please enter a valid number!")


except ZeroDivisionError:

print("Cannot divide by zero!")

except Exception as e:

print(f"An error occurred: {e}")

Else and finally blocks


try:

file = open('[Link]', 'r')

content = [Link]()

except FileNotFoundError:

print("File not found!")

else:

print("File read successfully!")

print(content)

finally:

if 'file' in locals():

[Link]()

print("Cleanup complete!")

y:
file = open('[Link]', 'r')
content = [Link]()
except FileNotFoundError:
print("File not found!")
else:
print("File read successfully!")
print(content)
finally:
if 'file' in locals():
[Link]()
print("Cleanup complete!")
Raising Exceptions
```python

Raising exceptions
def divide(a, b):

if b == 0:

raise ValueError("Cannot divide by zero!")

return a / b

try:

result = divide(10, 0)

except ValueError as e:

print(f"Error: {e}")

Custom exceptions
class InvalidAgeError(Exception):

def __init__(self, age, message="Age must be between 0 and 120"):

[Link] = age

[Link] = message

super().__init__([Link])

def set_age(age):

if not 0 <= age <= 120:

raise InvalidAgeError(age)

return f"Age set to {age}"

try:

print(set_age(150))

except InvalidAgeError as e:
print(f"Invalid age {[Link]}: {[Link]}")

ass InvalidAgeError(Exception):
def __init__(self, age, message="Age must be between 0 and
120"):
[Link] = age
[Link] = message
super().__init__([Link])

def set_age(age):
if not 0 <= age <= 120:
raise InvalidAgeError(age)
return f"Age set to {age}"

try:
print(set_age(150))
except InvalidAgeError as e:
print(f"Invalid age {[Link]}: {[Link]}")

Exception Best Practices


```python

Specific exception handling


try:

# Some operation

pass

except (ValueError, TypeError) as e:

print(f"Value or type error: {e}")

except Exception as e:

print(f"Unexpected error: {e}")

# Log the error for debugging

import traceback

traceback.print_exc()
Context managers for resource
management
with open('[Link]', 'r') as file:

content = [Link]()

File automatically closed here


Logging exceptions
import logging

[Link](filename='[Link]', level=[Link])

try:

result = 10 / 0

except Exception as e:

[Link](f"An error occurred: {e}", exc_info=True)

port logging

[Link](filename='[Link]', level=[Link])

try:
result = 10 / 0
except Exception as e:
[Link](f"An error occurred: {e}", exc_info=True)

---

9. Advanced Python Features

Generators
```python

Generator function
def countdown(n):

while n > 0:

yield n

n -= 1

Using generator
for i in countdown(5):

print(i) # 5, 4, 3, 2, 1

Generator expression
squares = (x**2 for x in range(10))

for square in squares:

print(square)

Benefits: memory efficient, lazy


evaluation
def read_large_file(file_path):

with open(file_path, 'r') as file:

for line in file:

yield [Link]()

Process large file without loading into


memory
for line in read_large_file('large_file.txt'):

process(line)
r line in read_large_file('large_file.txt'):
process(line)

Iterators
```python

Creating custom iterator


class Countdown:

def __init__(self, start):

[Link] = start

def __iter__(self):

return self

def __next__(self):

if [Link] <= 0:

raise StopIteration

current = [Link]

[Link] -= 1

return current

Using custom iterator


countdown = Countdown(5)

for i in countdown:

print(i)

Itertools module
import itertools

Infinite iterator
counter = [Link](start=0, step=2)

for i in range(5):

print(next(counter)) # 0, 2, 4, 6, 8

Cycle
colors = [Link](['red', 'green', 'blue'])

for i in range(5):

print(next(colors)) # red, green, blue, red, green

Combinations
items = ['A', 'B', 'C']

for combo in [Link](items, 2):

print(combo) # ('A', 'B'), ('A', 'C'), ('B', 'C')

ems = ['A', 'B', 'C']


for combo in [Link](items, 2):
print(combo) # ('A', 'B'), ('A', 'C'), ('B', 'C')

Context Managers
```python

Creating custom context manager


class FileManager:

def __init__(self, filename, mode):

[Link] = filename

[Link] = mode

[Link] = None

def __enter__(self):

[Link] = open([Link], [Link])


return [Link]

def __exit__(self, exc_type, exc_val, exc_tb):

if [Link]:

[Link]()

if exc_type:

print(f"An error occurred: {exc_val}")

return True # Suppress exception

Using custom context manager


with FileManager('[Link]', 'r') as file:

content = [Link]()

print(content)

Using contextlib decorator


from contextlib import contextmanager

@contextmanager

def timer():

import time

start = [Link]()

yield

end = [Link]()

print(f"Elapsed time: {end - start:.2f} seconds")

with timer():

# Some operation

import time

[Link](1)
om contextlib import contextmanager

@contextmanager
def timer():
import time
start = [Link]()
yield
end = [Link]()
print(f"Elapsed time: {end - start:.2f} seconds")

with timer():
# Some operation
import time
[Link](1)

Metaclasses
```python

Simple metaclass
class SingletonMeta(type):

_instances = {}

def __call__(cls, *args, **kwargs):

if cls not in cls._instances:

cls._instances[cls] = super().__call__(*args, **kwargs)

return cls._instances[cls]

class Singleton(metaclass=SingletonMeta):

def __init__(self):

[Link] = 0

Both instances are the same


s1 = Singleton()

s2 = Singleton()

print(s1 is s2) # True


Metaclass for adding methods
class AddMethodMeta(type):

def __new__(cls, name, bases, namespace):

namespace['greet'] = lambda self: f"Hello from {name}"

return super().__new__(cls, name, bases, namespace)

class MyClass(metaclass=AddMethodMeta):

pass

obj = MyClass()

print([Link]()) # Hello from MyClass

ass AddMethodMeta(type):
def __new__(cls, name, bases, namespace):
namespace['greet'] = lambda self: f"Hello from {name}"
return super().__new__(cls, name, bases, namespace)

class MyClass(metaclass=AddMethodMeta):
pass

obj = MyClass()
print([Link]()) # Hello from MyClass

---

10. Working with Libraries

NumPy for Numerical Computing


```python

import numpy as np

Creating arrays
arr = [Link]([1, 2, 3, 4, 5])

matrix = [Link]([[1, 2, 3], [4, 5, 6]])


Array operations
print(arr * 2) # [2, 4, 6, 8, 10]

print(arr + arr) # [2, 4, 6, 8, 10]

print([Link](arr)) # 3.0

print([Link](arr)) # 1.414...

Array slicing
print(arr[1:4]) # [2, 3, 4]

print(matrix[:, 1]) # [2, 5]

Mathematical functions
print([Link](arr))

print([Link](arr))

print([Link](arr))

Random numbers
print([Link](5)) # 5 random numbers [0, 1)

print([Link](5)) # 5 random numbers from normal distribution

print([Link](0, 10, 5)) # 5 random integers [0, 10)

int([Link](5)) # 5 random numbers [0, 1)


print([Link](5)) # 5 random numbers from normal
distribution
print([Link](0, 10, 5)) # 5 random integers [0, 10)

Pandas for Data Analysis


```python

import pandas as pd
Creating DataFrames
data = {

'name': ['Alice', 'Bob', 'Charlie'],

'age': [25, 30, 35],

'city': ['NYC', 'LA', 'Chicago']

df = [Link](data)

Reading data
df = pd.read_csv('[Link]')

df = pd.read_excel('[Link]')

df = pd.read_json('[Link]')

Data exploration
print([Link]()) # First 5 rows

print([Link]()) # DataFrame info

print([Link]()) # Statistical summary

print([Link]) # Column names

Data selection
print(df['name']) # Single column

print(df[['name', 'age']]) # Multiple columns

print([Link][0]) # First row by position

print([Link][0]) # First row by label


Filtering
filtered = df[df['age'] > 25]

print(filtered)

Data manipulation
df['new_column'] = df['age'] * 2

df = [Link]('new_column', axis=1)

df = [Link](0) # Fill missing values

df = [Link]() # Drop rows with missing values

Grouping and aggregation


grouped = [Link]('city')

print(grouped['age'].mean())

Sorting
df_sorted = df.sort_values('age', ascending=False)

_sorted = df.sort_values('age', ascending=False)

Matplotlib for Visualization


```python

import [Link] as plt

Line plot
x = [1, 2, 3, 4, 5]

y = [2, 4, 6, 8, 10]

[Link](x, y)
[Link]('X axis')

[Link]('Y axis')

[Link]('Line Plot')

[Link]()

Bar chart
categories = ['A', 'B', 'C', 'D']

values = [10, 20, 15, 25]

[Link](categories, values)

[Link]('Bar Chart')

[Link]()

Scatter plot
x = [1, 2, 3, 4, 5]

y = [2, 4, 1, 3, 5]

[Link](x, y)

[Link]('Scatter Plot')

[Link]()

Histogram
data = [1, 2, 2, 3, 3, 3, 4, 4, 5]

[Link](data, bins=5)

[Link]('Histogram')

[Link]()

Multiple plots
fig, axes = [Link](2, 2)

axes[0, 0].plot(x, y)

axes[0, 1].bar(categories, values)

axes[1, 0].scatter(x, y)

axes[1, 1].hist(data)

plt.tight_layout()

[Link]()

g, axes = [Link](2, 2)
axes[0, 0].plot(x, y)
axes[0, 1].bar(categories, values)
axes[1, 0].scatter(x, y)
axes[1, 1].hist(data)
plt.tight_layout()
[Link]()

Requests for HTTP


```python

import requests

GET request
response = [Link]('[Link]

print(response.status_code) # 200

print([Link]) # Response text

print([Link]()) # JSON response

POST request
data = {'name': 'Alice', 'age': 25}

response = [Link]('[Link] json=data)

print(response.status_code)
Headers and parameters
headers = {'Authorization': 'Bearer token'}

params = {'page': 1, 'limit': 10}

response = [Link]('[Link]

headers=headers, params=params)

Error handling
try:

response = [Link]('[Link]

response.raise_for_status() # Raise exception for bad status codes

except [Link] as e:

print(f"Request failed: {e}")

y:
response = [Link]('[Link]
response.raise_for_status() # Raise exception for bad status
codes
except [Link] as e:
print(f"Request failed: {e}")

---

11. Best Practices and Patterns

Code Style (PEP 8)


```python

Follow PEP 8 guidelines


- Use 4 spaces for indentation
- Maximum line length: 79 characters
- Use snake_case for variables and
functions
- Use PascalCase for classes
- Use UPPER_CASE for constants
Good
def calculate_average(numbers):

"""Calculate the average of a list of numbers."""

if not numbers:

return 0

return sum(numbers) / len(numbers)

Bad
def calcAvg(n):

a=0

for i in n:

a+=i

return a/len(n)

Docstrings
def complex_function(param1, param2):

"""

Brief description of the function.

Args:

param1: Description of param1


param2: Description of param2

Returns:

Description of return value

Raises:

ValueError: If something goes wrong

"""

pass

f complex_function(param1, param2):
"""
Brief description of the function.

Args:
param1: Description of param1
param2: Description of param2

Returns:
Description of return value

Raises:
ValueError: If something goes wrong
"""
pass

Design Patterns
```python

Singleton Pattern
class Singleton:

_instance = None

def __new__(cls):

if cls._instance is None:

cls._instance = super().__new__(cls)

return cls._instance
Factory Pattern
class AnimalFactory:

@staticmethod

def create_animal(animal_type):

if animal_type == 'dog':

return Dog()

elif animal_type == 'cat':

return Cat()

else:

raise ValueError("Unknown animal type")

Observer Pattern
class Observer:

def update(self, message):

pass

class Subject:

def __init__(self):

self._observers = []

def attach(self, observer):

self._observers.append(observer)

def notify(self, message):

for observer in self._observers:

[Link](message)

Strategy Pattern
class PaymentStrategy:

def pay(self, amount):

pass

class CreditCardPayment(PaymentStrategy):

def pay(self, amount):

print(f"Paid ${amount} with credit card")

class PayPalPayment(PaymentStrategy):

def pay(self, amount):

print(f"Paid ${amount} with PayPal")

ass PaymentStrategy:
def pay(self, amount):
pass

class CreditCardPayment(PaymentStrategy):
def pay(self, amount):
print(f"Paid ${amount} with credit card")

class PayPalPayment(PaymentStrategy):
def pay(self, amount):
print(f"Paid ${amount} with PayPal")

Performance Optimization
```python

Use built-in functions (faster than


loops)
numbers = list(range(1000000))

Slow
sum_slow = 0

for num in numbers:


sum_slow += num

Fast
sum_fast = sum(numbers)

List comprehensions (faster than loops)


Slow
squares_slow = []

for num in range(1000):

squares_slow.append(num ** 2)

Fast
squares_fast = [num ** 2 for num in range(1000)]

Use generators for large datasets


Memory intensive
def get_squares_list(n):

return [x**2 for x in range(n)]

Memory efficient
def get_squares_generator(n):

for x in range(n):

yield x**2

Use appropriate data structures


Slow for lookups
items_list = ['apple', 'banana', 'cherry']

if 'banana' in items_list: # O(n)

pass

Fast for lookups


items_set = {'apple', 'banana', 'cherry'}

if 'banana' in items_set: # O(1)

pass

Avoid premature optimization


Profile first, then optimize
import cProfile

def my_function():

# Your code here

pass

[Link]('my_function()')

port cProfile

def my_function():
# Your code here
pass

[Link]('my_function()')

---
12. Real-World Projects

Web Scraper
``python
import requests
from bs4 import BeautifulSoup
import pandas as pd

def scrape_quotes():
"""Scrape quotes from a website."""
url = '[Link]
quotes = []

for page in range(1, 4): # Scrape 3 pages


response = [Link](f'{url}/page/{page}')
soup = BeautifulSoup([Link], '[Link]')

for quote in soup.find_all('div', class_='quote'):


text = [Link]('span', class_='text').text
author = [Link]('small', class_='author').text
tags = [[Link] for tag in quote.find_all('a',
class_='tag')]

[Link]({
'text': text,
'author': author,
'tags': ', '.join(tags)
})

# Save to CSV
df = [Link](quotes)
df.to_csv('[Link]', index=False)
print(f"Scraped {len(quotes)} quotes")

scrape_quotes()

import requests

from bs4 import BeautifulSoup

import pandas as pd

def scrape_quotes():

"""Scrape quotes from a website."""


url = '[Link]

quotes = []

for page in range(1, 4): # Scrape 3 pages

response = [Link](f'{url}/page/{page}')

soup = BeautifulSoup([Link], '[Link]')

for quote in soup.find_all('div', class_='quote'):

text = [Link]('span', class_='text').text

author = [Link]('small', class_='author').text

tags = [[Link] for tag in quote.find_all('a', class_='tag')]

[Link]({

'text': text,

'author': author,

'tags': ', '.join(tags)

})

# Save to CSV

df = [Link](quotes)

df.to_csv('[Link]', index=False)

print(f"Scraped {len(quotes)} quotes")

scrape_quotes()

``python
import requests
from bs4 import BeautifulSoup
import pandas as pd

def scrape_quotes():
"""Scrape quotes from a website."""
url = '[Link]
quotes = []

for page in range(1, 4): # Scrape 3 pages


response = [Link](f'{url}/page/{page}')
soup = BeautifulSoup([Link], '[Link]')

for quote in soup.find_all('div', class_='quote'):


text = [Link]('span', class_='text').text
author = [Link]('small', class_='author').text
tags = [[Link] for tag in quote.find_all('a',
class_='tag')]

[Link]({
'text': text,
'author': author,
'tags': ', '.join(tags)
})

# Save to CSV
df = [Link](quotes)
df.to_csv('[Link]', index=False)
print(f"Scraped {len(quotes)} quotes")

scrape_quotes()

Data Analysis Project


``python
import pandas as pd
import [Link] as plt
import seaborn as sns

def analyze_sales_data():
"""Analyze sales data and create visualizations."""
# Load data
df = pd.read_csv('sales_data.csv')

# Data cleaning
df['date'] = pd.to_datetime(df['date'])
df = [Link]()

# Basic statistics
print("Sales Statistics:")
print(df['amount'].describe())

# Monthly sales
df['month'] = df['date'].[Link]
monthly_sales = [Link]('month')['amount'].sum()

# Visualization
[Link](figsize=(12, 6))
monthly_sales.plot(kind='bar')
[Link]('Monthly Sales')
[Link]('Month')
[Link]('Sales Amount')
[Link]('monthly_sales.png')

# Top products
top_products = [Link]('product')
['amount'].sum().sort_values(ascending=False).head(10)

[Link](figsize=(12, 6))
top_products.plot(kind='barh')
[Link]('Top 10 Products by Sales')
[Link]('Sales Amount')
[Link]('top_products.png')

print("Analysis complete!")

analyze_sales_data()

import pandas as pd

import [Link] as plt

import seaborn as sns

def analyze_sales_data():

"""Analyze sales data and create visualizations."""

# Load data

df = pd.read_csv('sales_data.csv')

# Data cleaning

df['date'] = pd.to_datetime(df['date'])

df = [Link]()

# Basic statistics

print("Sales Statistics:")

print(df['amount'].describe())

# Monthly sales
df['month'] = df['date'].[Link]

monthly_sales = [Link]('month')['amount'].sum()

# Visualization

[Link](figsize=(12, 6))

monthly_sales.plot(kind='bar')

[Link]('Monthly Sales')

[Link]('Month')

[Link]('Sales Amount')

[Link]('monthly_sales.png')

# Top products

top_products = [Link]('product')
['amount'].sum().sort_values(ascending=False).head(10)

[Link](figsize=(12, 6))

top_products.plot(kind='barh')

[Link]('Top 10 Products by Sales')

[Link]('Sales Amount')

[Link]('top_products.png')

print("Analysis complete!")

analyze_sales_data()

``python
import pandas as pd
import [Link] as plt
import seaborn as sns

def analyze_sales_data():
"""Analyze sales data and create visualizations."""
# Load data
df = pd.read_csv('sales_data.csv')

# Data cleaning
df['date'] = pd.to_datetime(df['date'])
df = [Link]()
# Basic statistics
print("Sales Statistics:")
print(df['amount'].describe())

# Monthly sales
df['month'] = df['date'].[Link]
monthly_sales = [Link]('month')['amount'].sum()

# Visualization
[Link](figsize=(12, 6))
monthly_sales.plot(kind='bar')
[Link]('Monthly Sales')
[Link]('Month')
[Link]('Sales Amount')
[Link]('monthly_sales.png')

# Top products
top_products = [Link]('product')
['amount'].sum().sort_values(ascending=False).head(10)

[Link](figsize=(12, 6))
top_products.plot(kind='barh')
[Link]('Top 10 Products by Sales')
[Link]('Sales Amount')
[Link]('top_products.png')

print("Analysis complete!")

analyze_sales_data()

Automation Script
``python
import os
import shutil
from datetime import datetime

def organize_downloads():
"""Organize files in Downloads folder by type."""
downloads_path = [Link]('~/Downloads')

# File type categories


categories = {
'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
'Documents': ['.pdf', '.doc', '.docx', '.txt', '.xls',
'.xlsx'],
'Videos': ['.mp4', '.avi', '.mov', '.mkv'],
'Music': ['.mp3', '.wav', '.flac'],
'Archives': ['.zip', '.rar', '.7z', '.tar']
}

# Create category folders


for category in categories:
category_path = [Link](downloads_path, category)
[Link](category_path, exist_ok=True)

# Move files to appropriate folders


for filename in [Link](downloads_path):
file_path = [Link](downloads_path, filename)

if [Link](file_path):
file_ext = [Link](filename)[1].lower()

for category, extensions in [Link]():


if file_ext in extensions:
dest_path = [Link](downloads_path,
category, filename)
[Link](file_path, dest_path)
print(f"Moved {filename} to {category}")
break

print("Downloads organized!")

organize_downloads()

import os

import shutil

from datetime import datetime

def organize_downloads():

"""Organize files in Downloads folder by type."""

downloads_path = [Link]('~/Downloads')

# File type categories

categories = {

'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],


'Documents': ['.pdf', '.doc', '.docx', '.txt', '.xls', '.xlsx'],

'Videos': ['.mp4', '.avi', '.mov', '.mkv'],

'Music': ['.mp3', '.wav', '.flac'],

'Archives': ['.zip', '.rar', '.7z', '.tar']

# Create category folders

for category in categories:

category_path = [Link](downloads_path, category)

[Link](category_path, exist_ok=True)

# Move files to appropriate folders

for filename in [Link](downloads_path):

file_path = [Link](downloads_path, filename)

if [Link](file_path):

file_ext = [Link](filename)[1].lower()

for category, extensions in [Link]():

if file_ext in extensions:

dest_path = [Link](downloads_path, category, filename)

[Link](file_path, dest_path)

print(f"Moved {filename} to {category}")

break

print("Downloads organized!")

organize_downloads()

``python
import os
import shutil
from datetime import datetime

def organize_downloads():
"""Organize files in Downloads folder by type."""
downloads_path = [Link]('~/Downloads')

# File type categories


categories = {
'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
'Documents': ['.pdf', '.doc', '.docx', '.txt', '.xls',
'.xlsx'],
'Videos': ['.mp4', '.avi', '.mov', '.mkv'],
'Music': ['.mp3', '.wav', '.flac'],
'Archives': ['.zip', '.rar', '.7z', '.tar']
}

# Create category folders


for category in categories:
category_path = [Link](downloads_path, category)
[Link](category_path, exist_ok=True)

# Move files to appropriate folders


for filename in [Link](downloads_path):
file_path = [Link](downloads_path, filename)

if [Link](file_path):
file_ext = [Link](filename)[1].lower()

for category, extensions in [Link]():


if file_ext in extensions:
dest_path = [Link](downloads_path,
category, filename)
[Link](file_path, dest_path)
print(f"Moved {filename} to {category}")
break

print("Downloads organized!")

organize_downloads()

API Integration
```python

import requests

import pandas as pd

from datetime import datetime, timedelta

def get_weather_data(city, api_key):


"""Get weather data from OpenWeatherMap API."""

base_url = "[Link]

params = {

'q': city,

'appid': api_key,

'units': 'metric'

response = [Link](base_url, params=params)

if response.status_code == 200:

data = [Link]()

weather_info = {

'city': data['name'],

'temperature': data['main']['temp'],

'feels_like': data['main']['feels_like'],

'humidity': data['main']['humidity'],

'description': data['weather'][0]['description'],

'timestamp': [Link]().strftime('%Y-%m-%d %H:%M:%S')

return weather_info

else:

print(f"Error: {response.status_code}")

return None

def track_weather(cities, api_key, duration_hours=24):

"""Track weather for multiple cities over time."""

weather_data = []

for _ in range(duration_hours):
for city in cities:

data = get_weather_data(city, api_key)

if data:

weather_data.append(data)

# Wait for an hour (in real scenario)

# [Link](3600)

# Save to CSV

df = [Link](weather_data)

df.to_csv('weather_data.csv', index=False)

print(f"Collected {len(weather_data)} weather readings")

Usage
cities = ['New York', 'London', 'Tokyo']

api_key = 'your_api_key_here'

track_weather(cities, api_key)

ties = ['New York', 'London', 'Tokyo']


api_key = 'your_api_key_here'
track_weather(cities, api_key)

---

13. Performance Optimization

Profiling and Benchmarking


```python

import time

import cProfile

import timeit
Simple timing
def measure_time(func):

def wrapper(*args, **kwargs):

start = [Link]()

result = func(*args, **kwargs)

end = [Link]()

print(f"{func.__name__} took {end - start:.4f} seconds")

return result

return wrapper

@measure_time

def slow_function():

total = 0

for i in range(1000000):

total += i

return total

slow_function()

Using timeit
code = """

sum(range(1000000))

"""

execution_time = [Link](code, number=100)

print(f"Average time: {execution_time / 100:.6f} seconds")

Using cProfile
def profile_function():

total = 0

for i in range(1000000):

total += i ** 2

return total

[Link]('profile_function()')

f profile_function():
total = 0
for i in range(1000000):
total += i ** 2
return total

[Link]('profile_function()')

Memory Optimization
```python

import sys

import gc

Check memory usage


def get_size(obj):

"""Get size of object in bytes."""

return [Link](obj)

Generator vs List
def list_version(n):

return [x**2 for x in range(n)]

def generator_version(n):

return (x**2 for x in range(n))

list_size = get_size(list_version(1000000))
print(f"List size: {list_size} bytes")

Generators don't store all values in


memory
gen = generator_version(1000000)

print(f"Generator size: {get_size(gen)} bytes")

Memory-efficient data structures


import array

List (more memory)


list_int = [1, 2, 3, 4, 5]

print(f"List: {get_size(list_int)} bytes")

Array (less memory)


array_int = [Link]('i', [1, 2, 3, 4, 5])

print(f"Array: {get_size(array_int)} bytes")

Garbage collection
class MyClass:

def __del__(self):

print("Object deleted")

obj = MyClass()

del obj # Explicit deletion

[Link]() # Force garbage collection


ass MyClass:
def __del__(self):
print("Object deleted")

obj = MyClass()
del obj # Explicit deletion
[Link]() # Force garbage collection

Algorithm Optimization
```python

Example: Finding duplicates in a list


O(n²) - Slow
def find_duplicates_slow(arr):

duplicates = []

for i in range(len(arr)):

for j in range(i + 1, len(arr)):

if arr[i] == arr[j] and arr[i] not in duplicates:

[Link](arr[i])

return duplicates

O(n) - Fast
def find_duplicates_fast(arr):

seen = set()

duplicates = set()

for item in arr:

if item in seen:

[Link](item)

else:
[Link](item)

return list(duplicates)

Benchmark
import timeit

arr = list(range(10000)) + [5000, 5000, 5000]

slow_time = [Link](lambda: find_duplicates_slow(arr), number=1)

fast_time = [Link](lambda: find_duplicates_fast(arr), number=1)

print(f"Slow: {slow_time:.4f} seconds")

print(f"Fast: {fast_time:.4f} seconds")

print(f"Speedup: {slow_time / fast_time:.2f}x")

port timeit

arr = list(range(10000)) + [5000, 5000, 5000]

slow_time = [Link](lambda: find_duplicates_slow(arr),


number=1)
fast_time = [Link](lambda: find_duplicates_fast(arr),
number=1)

print(f"Slow: {slow_time:.4f} seconds")


print(f"Fast: {fast_time:.4f} seconds")
print(f"Speedup: {slow_time / fast_time:.2f}x")

Caching and Memoization


```python

from functools import lru_cache

import time

Without caching
def fibonacci_slow(n):

if n <= 1:
return n

return fibonacci_slow(n - 1) + fibonacci_slow(n - 2)

With LRU cache


@lru_cache(maxsize=None)

def fibonacci_fast(n):

if n <= 1:

return n

return fibonacci_fast(n - 1) + fibonacci_fast(n - 2)

Benchmark
start = [Link]()

result = fibonacci_slow(35)

slow_time = [Link]() - start

start = [Link]()

result = fibonacci_fast(35)

fast_time = [Link]() - start

print(f"Slow: {slow_time:.4f} seconds")

print(f"Fast: {fast_time:.4f} seconds")

print(f"Speedup: {slow_time / fast_time:.2f}x")

Custom memoization
def memoize(func):

cache = {}

def wrapper(*args):

if args not in cache:


cache[args] = func(*args)

return cache[args]

return wrapper

@memoize

def expensive_function(x):

# Simulate expensive computation

[Link](0.1)

return x ** 2

First call (slow)


start = [Link]()

result1 = expensive_function(5)

first_time = [Link]() - start

Second call (fast - from cache)


start = [Link]()

result2 = expensive_function(5)

second_time = [Link]() - start

print(f"First call: {first_time:.4f} seconds")

print(f"Second call: {second_time:.4f} seconds")

art = [Link]()
result2 = expensive_function(5)
second_time = [Link]() - start

print(f"First call: {first_time:.4f} seconds")


print(f"Second call: {second_time:.4f} seconds")

---
14. Testing and Debugging

Unit Testing with unittest


``python
import unittest

def add(a, b):


return a + b

def divide(a, b):


if b == 0:
raise ValueError("Cannot divide by zero")
return a / b

class TestMathFunctions([Link]):
def test_add(self):
[Link](add(2, 3), 5)
[Link](add(-1, 1), 0)
[Link](add(0, 0), 0)

def test_divide(self):
[Link](divide(10, 2), 5)
[Link](divide(-10, 2), -5)

def test_divide_by_zero(self):
with [Link](ValueError):
divide(10, 0)

if __name__ == '__main__':
[Link]()

import unittest

def add(a, b):

return a + b

def divide(a, b):

if b == 0:

raise ValueError("Cannot divide by zero")

return a / b

class TestMathFunctions([Link]):
def test_add(self):

[Link](add(2, 3), 5)

[Link](add(-1, 1), 0)

[Link](add(0, 0), 0)

def test_divide(self):

[Link](divide(10, 2), 5)

[Link](divide(-10, 2), -5)

def test_divide_by_zero(self):

with [Link](ValueError):

divide(10, 0)

if __name__ == '__main__':

[Link]()

``python
import unittest

def add(a, b):


return a + b

def divide(a, b):


if b == 0:
raise ValueError("Cannot divide by zero")
return a / b

class TestMathFunctions([Link]):
def test_add(self):
[Link](add(2, 3), 5)
[Link](add(-1, 1), 0)
[Link](add(0, 0), 0)

def test_divide(self):
[Link](divide(10, 2), 5)
[Link](divide(-10, 2), -5)

def test_divide_by_zero(self):
with [Link](ValueError):
divide(10, 0)
if __name__ == '__main__':
[Link]()

Testing with pytest


``python
import pytest

def add(a, b):


return a + b

def divide(a, b):


if b == 0:
raise ValueError("Cannot divide by zero")
return a / b

def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0

def test_divide():
assert divide(10, 2) == 5
assert divide(-10, 2) == -5

def test_divide_by_zero():
with [Link](ValueError):
divide(10, 0)

@[Link]("a,b,expected", [
(2, 3, 5),
(-1, 1, 0),
(0, 0, 0),
])
def test_add_parametrized(a, b, expected):
assert add(a, b) == expected

import pytest

def add(a, b):

return a + b

def divide(a, b):

if b == 0:
raise ValueError("Cannot divide by zero")

return a / b

def test_add():

assert add(2, 3) == 5

assert add(-1, 1) == 0

assert add(0, 0) == 0

def test_divide():

assert divide(10, 2) == 5

assert divide(-10, 2) == -5

def test_divide_by_zero():

with [Link](ValueError):

divide(10, 0)

@[Link]("a,b,expected", [

(2, 3, 5),

(-1, 1, 0),

(0, 0, 0),

])

def test_add_parametrized(a, b, expected):

assert add(a, b) == expected

``python
import pytest

def add(a, b):


return a + b

def divide(a, b):


if b == 0:
raise ValueError("Cannot divide by zero")
return a / b

def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0

def test_divide():
assert divide(10, 2) == 5
assert divide(-10, 2) == -5

def test_divide_by_zero():
with [Link](ValueError):
divide(10, 0)

@[Link]("a,b,expected", [
(2, 3, 5),
(-1, 1, 0),
(0, 0, 0),
])
def test_add_parametrized(a, b, expected):
assert add(a, b) == expected

Debugging Techniques
```python

Using print statements


def debug_function(x):

print(f"Input: {x}")

result = x ** 2

print(f"Result: {result}")

return result

Using pdb (Python debugger)


import pdb

def debug_with_pdb(x):

pdb.set_trace() # Breakpoint

result = x ** 2
return result

Common pdb commands:


n (next): Execute next line
s (step): Step into function
c (continue): Continue execution
p (print): Print variable
q (quit): Quit debugger
Using logging
import logging

[Link](

level=[Link],

format='%(asctime)s - %(levelname)s - %(message)s'

def log_function(x):

[Link](f"Starting with x={x}")

result = x ** 2

[Link](f"Result: {result}")

return result

Exception handling with traceback


import traceback

def safe_divide(a, b):

try:
return a / b

except Exception as e:

[Link](f"Error dividing {a} by {b}")

[Link](traceback.format_exc())

raise

port traceback

def safe_divide(a, b):


try:
return a / b
except Exception as e:
[Link](f"Error dividing {a} by {b}")
[Link](traceback.format_exc())
raise

Code Quality Tools


```python

Type hints (Python 3.5+)


from typing import List, Dict, Optional

def process_data(data: List[Dict[str, int]]) -> Dict[str, float]:

"""Process a list of dictionaries and return summary."""

total = sum(item['value'] for item in data)

average = total / len(data)

return {'total': total, 'average': average}

Using mypy for type checking


Run: mypy your_file.py
Docstrings with Google style
def calculate_metrics(data: List[float]) -> Dict[str, float]:
"""Calculate statistical metrics for a list of numbers.

Args:

data: A list of numerical values.

Returns:

A dictionary containing mean, median, and std.

Raises:

ValueError: If data is empty.

Example:

calculate_metrics([1, 2, 3, 4, 5])

{'mean': 3.0, 'median': 3.0, 'std': 1.414...}

"""

if not data:

raise ValueError("Data cannot be empty")

import statistics

return {

'mean': [Link](data),

'median': [Link](data),

'std': [Link](data)

f calculate_metrics(data: List[float]) -> Dict[str, float]:


"""Calculate statistical metrics for a list of numbers.

Args:
data: A list of numerical values.

Returns:
A dictionary containing mean, median, and std.

Raises:
ValueError: If data is empty.
Example:
>>> calculate_metrics([1, 2, 3, 4, 5])
{'mean': 3.0, 'median': 3.0, 'std': 1.414...}
"""
if not data:
raise ValueError("Data cannot be empty")

import statistics
return {
'mean': [Link](data),
'median': [Link](data),
'std': [Link](data)
}

---

15. Deployment and Automation

Virtual Environments
```bash

Create virtual environment


python -m venv myenv

Activate (Windows)
myenv\Scripts\activate

Activate (macOS/Linux)
source myenv/bin/activate

Install packages
pip install numpy pandas requests
Export requirements
pip freeze > [Link]

Install from requirements


pip install -r [Link]

Deactivate
deactivate

activate

Creating Executable Scripts


``python
#!/usr/bin/env python3
"""
Main script for data processing application.
"""

import argparse
import sys

def process_file(input_file, output_file):


"""Process input file and save to output file."""
print(f"Processing {input_file}...")
# Your processing logic here
print(f"Saved to {output_file}")

def main():
parser = [Link](
description='Process data files'
)
parser.add_argument(
'input',
help='Input file path'
)
parser.add_argument(
'-o', '--output',
default='[Link]',
help='Output file path (default: [Link])'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Enable verbose output'
)

args = parser.parse_args()

if [Link]:
print(f"Input: {[Link]}")
print(f"Output: {[Link]}")

try:
process_file([Link], [Link])
except Exception as e:
print(f"Error: {e}", file=[Link])
[Link](1)

if __name__ == '__main__':
main()

#!/usr/bin/env python3

"""

Main script for data processing application.

"""

import argparse

import sys

def process_file(input_file, output_file):

"""Process input file and save to output file."""

print(f"Processing {input_file}...")

# Your processing logic here

print(f"Saved to {output_file}")

def main():

parser = [Link](
description='Process data files'

parser.add_argument(

'input',

help='Input file path'

parser.add_argument(

'-o', '--output',

default='[Link]',

help='Output file path (default: [Link])'

parser.add_argument(

'-v', '--verbose',

action='store_true',

help='Enable verbose output'

args = parser.parse_args()

if [Link]:

print(f"Input: {[Link]}")

print(f"Output: {[Link]}")

try:

process_file([Link], [Link])

except Exception as e:

print(f"Error: {e}", file=[Link])

[Link](1)

if __name__ == '__main__':
main()

``python
#!/usr/bin/env python3
"""
Main script for data processing application.
"""

import argparse
import sys

def process_file(input_file, output_file):


"""Process input file and save to output file."""
print(f"Processing {input_file}...")
# Your processing logic here
print(f"Saved to {output_file}")

def main():
parser = [Link](
description='Process data files'
)
parser.add_argument(
'input',
help='Input file path'
)
parser.add_argument(
'-o', '--output',
default='[Link]',
help='Output file path (default: [Link])'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Enable verbose output'
)

args = parser.parse_args()

if [Link]:
print(f"Input: {[Link]}")
print(f"Output: {[Link]}")

try:
process_file([Link], [Link])
except Exception as e:
print(f"Error: {e}", file=[Link])
[Link](1)

if __name__ == '__main__':
main()

Task Scheduling
```python

import schedule

import time

def job():

print("Running scheduled job...")

def backup_data():

print("Backing up data...")

Schedule jobs
[Link](10).[Link](job)

[Link]().[Link](job)

[Link]().[Link]("10:30").do(job)

[Link]().[Link](backup_data)

Run scheduled tasks


while True:

schedule.run_pending()

[Link](1)

Using APScheduler for more complex


scheduling
from [Link] import BlockingScheduler
def scheduled_task():

print("Task executed at", [Link]())

scheduler = BlockingScheduler()

scheduler.add_job(scheduled_task, 'interval', minutes=30)

[Link]()

om [Link] import BlockingScheduler

def scheduled_task():
print("Task executed at", [Link]())

scheduler = BlockingScheduler()
scheduler.add_job(scheduled_task, 'interval', minutes=30)
[Link]()

Docker for Python Applications


```dockerfile

Dockerfile
FROM python:3.9-slim

WORKDIR /app

Copy requirements first for better


caching
COPY [Link] .

RUN pip install --no-cache-dir -r [Link]

Copy application code


COPY . .

Set environment variables


ENV PYTHONUNBUFFERED=1

Run the application


CMD ["python", "[Link]"]

D ["python", "[Link]"]

D ["python", "[Link]"]

[Link]
version: '3.8'

services:

web:

build: .

ports:

 "8000:8000"

volumes:

 .:/app

environment:

 DEBUG=True
 DATABASE_URL=postgresql://user:pass@db:5432/mydb

db:

image: postgres:13

environment:

 POSTGRES_USER=user
 POSTGRES_PASSWORD=pass
 POSTGRES_DB=mydb

volumes:

 postgres_data:/var/lib/postgresql/data
volumes:

postgres_data:

rsion: '3.8'

services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
environment:
- DEBUG=True
- DATABASE_URL=postgresql://user:pass@db:5432/mydb

db:
image: postgres:13
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=mydb
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:

CI/CD with GitHub Actions


```yaml

.github/workflows/[Link]
name: Python Application

on:

push:

branches: [ main ]

pull_request:

branches: [ main ]
jobs:

test:

runs-on: ubuntu-latest

steps:

 uses: actions/checkout@v2
 name: Set up Python

uses: actions/setup-python@v2

with:

python-version: '3.9'

 name: Install dependencies

run: |

python -m pip install --upgrade pip

pip install -r [Link]

pip install pytest pytest-cov

 name: Run tests

run: |

pytest --cov=. --cov-report=xml

 name: Upload coverage

uses: codecov/codecov-action@v2

me: Python Application

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python


uses: actions/setup-python@v2
with:
python-version: '3.9'

- name: Install dependencies


run: |
python -m pip install --upgrade pip
pip install -r [Link]
pip install pytest pytest-cov

- name: Run tests


run: |
pytest --cov=. --cov-report=xml

- name: Upload coverage


uses: codecov/codecov-action@v2

---

Conclusion
This comprehensive guide has covered the essential aspects of Python programming, from
basic syntax to advanced concepts and real-world applications. Python's simplicity and
power make it an excellent choice for beginners and experts alike.

Key Takeaways:
20. **Master the Fundamentals**: Variables, data types, control flow, and functions form
the foundation.
21. **Embrace Object-Oriented Programming**: Classes, inheritance, and polymorphism
enable code organization and reuse.
22. **Leverage the Ecosystem**: NumPy, Pandas, and other libraries provide powerful tools
for specific tasks.
23. **Write Clean Code**: Follow PEP 8 guidelines, use meaningful names, and document
your code.
24. **Test Thoroughly**: Unit tests and debugging ensure code reliability.
25. **Optimize When Necessary**: Profile before optimizing, and use appropriate data
structures.
26. **Automate Everything**: Use scripts, schedulers, and CI/CD to streamline workflows.

Next Steps:
 Build personal projects to apply what you've learned
 Contribute to open-source Python projects
 Explore specialized libraries for your field (data science, web development, etc.)
 Stay updated with Python's evolving ecosystem
 Join Python communities and attend meetups or conferences

Python continues to grow and evolve, with new features and libraries being developed
constantly. The skills you've gained from this guide will serve as a solid foundation for your
programming journey.

Remember: The best way to learn Python is to write Python code. Start small, build
incrementally, and don't be afraid to experiment. Happy coding!

---

Additional Resources

Official Documentation
 Python Documentation: [Link]
 PEP 8 Style Guide: [Link]

Learning Platforms
 Real Python: [Link]
 [Link] Tutorial: [Link]
 Codecademy Python Course: [Link]

Books
 "Python Crash Course" by Eric Matthes
 "Automate the Boring Stuff with Python" by Al Sweigart
 "Fluent Python" by Luciano Ramalho
 "Effective Python" by Brett Slatkin

Communities
 Python Discord: [Link]
 r/learnpython on Reddit
 Stack Overflow Python tag

Practice Platforms
 LeetCode: [Link]
 HackerRank: [Link]
 Codewars: [Link]

---

This guide provides a comprehensive foundation for Python programming. Continue exploring,
building, and learning to master this powerful language.

You might also like