Python Programming: From Basics to Advanced

Python is a versatile, high-level programming language known for its simplicity and readability. This comprehensive guide will take you from basic concepts to advanced Python programming.

Python Basics

Let's start with the fundamental concepts of Python programming:


# Variables and Data Types
name = "John"  # String
age = 25      # Integer
height = 1.75  # Float
is_student = True  # Boolean

# Basic Operations
print(f"Name: {name}, Age: {age}")  # String formatting
result = 10 + 5  # Addition
product = 4 * 3  # Multiplication

# String Operations
greeting = "Hello"
name = "World"
full_greeting = greeting + " " + name  # String concatenation
print(full_greeting.upper())  # String methods

# Type Conversion
age_str = str(age)  # Convert to string
height_int = int(height)  # Convert to integer

# Input and Output
user_input = input("Enter your name: ")
print(f"Hello, {user_input}!")
                        

Control Flow

Control flow structures help you control the execution of your code:


# If-Else Statements
age = 18
if age >= 18:
    print("You are an adult")
elif age >= 13:
    print("You are a teenager")
else:
    print("You are a child")

# Loops
# For Loop
for i in range(5):
    print(i)  # Prints 0 to 4

# While Loop
count = 0
while count < 5:
    print(count)
    count += 1

# Loop Control
for i in range(10):
    if i == 5:
        continue  # Skip 5
    if i == 8:
        break    # Stop at 8
    print(i)

# Match Statement (Python 3.10+)
status = "error"
match status:
    case "success":
        print("Operation successful")
    case "error":
        print("An error occurred")
    case _:
        print("Unknown status")
                        

Functions & Modules

Functions help organize code into reusable blocks:


# Basic Function
def greet(name):
    return f"Hello, {name}!"

# Function with Default Parameters
def calculate_area(length, width=1):
    return length * width

# *args and **kwargs
def print_all(*args, **kwargs):
    for arg in args:
        print(arg)
    for key, value in kwargs.items():
        print(f"{key}: {value}")

# Lambda Functions
square = lambda x: x**2

# Decorators
def timer(func):
    def wrapper(*args, **kwargs):
        import time
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f"Function took {end-start} seconds")
        return result
    return wrapper

@timer
def slow_function():
    import time
    time.sleep(1)
    return "Done!"

# Importing Modules
import math
from datetime import datetime
import numpy as np  # Third-party module
                        

Data Structures

Python provides several built-in data structures:


# Lists
fruits = ["apple", "banana", "orange"]
fruits.append("grape")  # Add item
fruits.pop()  # Remove last item
first_fruit = fruits[0]  # Indexing
sliced_fruits = fruits[1:3]  # Slicing

# Tuples (Immutable)
coordinates = (10, 20)
x, y = coordinates  # Tuple unpacking

# Dictionaries
person = {
    "name": "John",
    "age": 30,
    "city": "New York"
}
person["email"] = "john@example.com"  # Add key-value pair

# Sets
numbers = {1, 2, 3, 4, 5}
more_numbers = {4, 5, 6, 7, 8}
union = numbers | more_numbers
intersection = numbers & more_numbers

# List Comprehensions
squares = [x**2 for x in range(10)]
even_squares = [x**2 for x in range(10) if x % 2 == 0]

# Dictionary Comprehensions
square_dict = {x: x**2 for x in range(5)}
                        

Object-Oriented Programming

Python supports object-oriented programming principles:


# Class Definition
class Animal:
    def __init__(self, name):
        self.name = name
    
    def speak(self):
        pass

# Inheritance
class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed
    
    def speak(self):
        return "Woof!"

# Properties and Class Methods
class Circle:
    def __init__(self, radius):
        self._radius = radius
    
    @property
    def radius(self):
        return self._radius
    
    @radius.setter
    def radius(self, value):
        if value >= 0:
            self._radius = value
    
    @property
    def area(self):
        return 3.14 * self._radius ** 2
    
    @classmethod
    def from_diameter(cls, diameter):
        return cls(diameter / 2)

# Magic Methods
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def __str__(self):
        return f"Point({self.x}, {self.y})"
    
    def __add__(self, other):
        return Point(self.x + other.x, self.y + other.y)
                        

Exception Handling

Handle errors and exceptions gracefully:


# Basic Exception Handling
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")
except Exception as e:
    print(f"An error occurred: {e}")
else:
    print("No exceptions occurred!")
finally:
    print("This always executes")

# Custom Exceptions
class CustomError(Exception):
    def __init__(self, message):
        self.message = message
        super().__init__(self.message)

# Context Managers
class FileManager:
    def __init__(self, filename):
        self.filename = filename
    
    def __enter__(self):
        self.file = open(self.filename, 'r')
        return self.file
    
    def __exit__(self, exc_type, exc_value, traceback):
        self.file.close()

# Using context manager
with FileManager('example.txt') as file:
    content = file.read()
                        

File I/O

Work with files and input/output operations:


# Reading Files
with open('file.txt', 'r') as file:
    content = file.read()  # Read entire file
    lines = file.readlines()  # Read lines into list

# Writing Files
with open('output.txt', 'w') as file:
    file.write('Hello, World!\n')
    file.writelines(['Line 1\n', 'Line 2\n'])

# CSV Files
import csv
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

# JSON Files
import json
data = {'name': 'John', 'age': 30}
with open('data.json', 'w') as file:
    json.dump(data, file)

# Binary Files
with open('image.jpg', 'rb') as file:
    binary_data = file.read()
                        

Advanced Concepts

Explore advanced Python features and concepts:


# Generators
def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

# Coroutines and Async Programming
import asyncio

async def fetch_data():
    print("Starting...")
    await asyncio.sleep(2)
    print("Done!")
    return {"data": "result"}

# Type Hints (Python 3.5+)
from typing import List, Dict, Optional

def process_items(items: List[str]) -> Dict[str, int]:
    return {item: len(item) for item in items}

# Metaclasses
class MetaClass(type):
    def __new__(cls, name, bases, attrs):
        # Modify class creation
        return super().__new__(cls, name, bases, attrs)

# Decorators with Arguments
def repeat(times):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(times):
                result = func(*args, **kwargs)
            return result
        return wrapper
    return decorator

@repeat(3)
def greet(name):
    print(f"Hello {name}")
                        

Best Practices

  • Follow PEP 8 style guide for Python code
  • Write docstrings for functions and classes
  • Use virtual environments for project isolation
  • Implement proper error handling
  • Write unit tests for your code
  • Use type hints for better code clarity
  • Keep functions and methods small and focused
  • Use meaningful variable and function names
  • Comment your code appropriately
  • Use list comprehensions judiciously
  • Profile your code for performance optimization
  • Practice defensive programming