Python Mastery Guide
1. Introduction to Python
Python is a high-level, interpreted programming language known for its simplicity and readability. It
is widely used in web development, data science, artificial intelligence, automation, and more.
### Why Learn Python?
- Easy to read and write
- Large community and libraries
- Cross-platform compatibility
### First Python Program
print("Hello, World!")
2. Variables, Data Types & Operators
### Variables & Data Types
Python supports multiple data types:
x = 10 # Integer
y = 3.14 # Float
name = "Alice" # String
is_valid = True # Boolean
### Operators in Python
# Arithmetic Operators
a = 10 + 5 # Addition
b = 10 - 5 # Subtraction
# Comparison Operators
print(10 > 5) # True
Python Mastery Guide
3. Control Flow (If-Else, Loops)
### If-Else Conditionals
age = 18
if age >= 18:
print("You can vote.")
else:
print("You cannot vote.")
### Loops in Python
for i in range(5): # Loop from 0 to 4
print(i)
x=0
while x < 5: # While loop example
print(x)
x += 1
4. Functions & Modules
### Defining Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
### Importing Modules
import math
print(math.sqrt(16)) # 4.0
5. File Handling
Python Mastery Guide
### Reading a File
with open("sample.txt", "r") as file:
content = file.read()
print(content)
### Writing to a File
with open("sample.txt", "w") as file:
file.write("Hello, Python!")
6. Error Handling
### Using Try-Except
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
7. Object-Oriented Programming (OOP)
### Classes & Objects
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def show_info(self):
print(f"Car: {self.brand} {self.model}")
car1 = Car("Toyota", "Corolla")
car1.show_info()
### Inheritance
Python Mastery Guide
class ElectricCar(Car):
def __init__(self, brand, model, battery):
super().__init__(brand, model)
self.battery = battery
tesla = ElectricCar("Tesla", "Model S", "100kWh")
tesla.show_info()
8. Advanced Python Topics
### Decorators
def decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@decorator
def say_hello():
print("Hello, world!")
say_hello()
### Generators
def count_up_to(n):
count = 1
while count <= n:
yield count
count += 1
Python Mastery Guide
gen = count_up_to(3)
print(next(gen)) # 1
print(next(gen)) # 2
9. Essential Python Libraries
### NumPy for Numerical Computing
import numpy as np
arr = np.array([1, 2, 3])
print(arr * 2) # [2 4 6]
### Pandas for Data Analysis
import pandas as pd
df = pd.DataFrame({"Name": ["Alice", "Bob"], "Age": [25, 30]})
print(df)
### Matplotlib for Visualization
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [10, 20, 30])
plt.show()
10. Real-World Python Projects
### Project 1: Data Analysis
- Load a dataset with Pandas
- Analyze and visualize trends
### Project 2: Web Scraper
- Scrape product prices from a website
### Project 3: Flask Web App
- Build a to-do list API
Python Mastery Guide
Final Challenge
### Build Your Own Python Project
1. Choose a problem to solve
2. Implement using Python libraries
3. Optimize performance and deploy online