Loading...
Development

Complete Python Tutorial Notes

Complete Python Tutorial Notes

With Best Practices & Real-World Examples


Table of Contents

  1. Introduction to Python
  2. Setup & Environment
  3. Basic Syntax & Data Types
  4. Control Flow
  5. Functions
  6. Data Structures
  7. File Handling
  8. Modules & Packages
  9. Object-Oriented Programming (OOP)
  10. Error Handling
  11. Working with Libraries
  12. Virtual Environments & Dependency Management
  13. Best Practices & Code Style
  14. Project Structure Example
  15. Bonus: Mini Project – Task Manager CLI

1. Introduction to Python

  • High-level, interpreted, general-purpose language
  • Readable syntax → great for beginners
  • Used in: Web (Django/Flask), Data Science, AI/ML, Automation, DevOps
  • Zen of Python (import this):

    "Beautiful is better than ugly. Simple is better than complex."


2. Setup & Environment

Install Python

# Check version
python --version
# or
python3 --version

IDEs / Editors

  • VS Code (with Python extension)
  • PyCharm (Community/Professional)
  • Jupyter Notebook (for data science)

3. Basic Syntax & Data Types

# hello.py
print("Hello, Python!")

# Variables (dynamically typed)
name = "Alice"        # str
age = 25              # int
height = 5.6          # float
is_student = True     # bool

# Type hints (best practice)
def greet(name: str) -> str:
    return f"Hello, {name}!"

print(greet(name))

Common Data Types

TypeExampleMutable?
int42No
float3.14No
str"hello"No
boolTrueNo
list[1, 2, 3]Yes
tuple(1, 2)No
dict{"key": "value"}Yes
set{1, 2, 3}Yes

4. Control Flow

# if-elif-else
score = 85
if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
else:
    grade = 'C'

# for loop
for i in range(5):
    print(i)

# list comprehension (Pythonic!)
squares = [x**2 for x in range(10)]

# while
count = 0
while count < 3:
    print("Counting...", count)
    count += 1

5. Functions

# Basic function
def add(a: int, b: int) -> int:
    """Return sum of two numbers."""
    return a + b

# Default arguments
def greet(name: str, msg: str = "Good morning!") -> str:
    return f"{msg}, {name}"

# *args and **kwargs
def print_items(*args, **kwargs):
    print("Args:", args)
    print("Kwargs:", kwargs)

print_items(1, 2, 3, name="Alice", age=25)

# Lambda functions
double = lambda x: x * 2
print(double(5))

6. Data Structures

List

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits.remove("banana")
print(fruits[1:])  # slicing

Dictionary

person = {
    "name": "Bob",
    "age": 30,
    "city": "New York"
}
person["email"] = "bob@example.com"
print(person.get("phone", "N/A"))

Set

unique = {1, 2, 2, 3}  # {1, 2, 3}
a = {1, 2, 3}
b = {3, 4, 5}
print(a & b)  # intersection
print(a | b)  # union

Tuple

point = (10, 20)
x, y = point  # unpacking

7. File Handling

# Writing
with open("data.txt", "w") as f:
    f.write("Hello, File!\n")
    f.write("Second line.")

# Reading
with open("data.txt", "r") as f:
    content = f.read()
    print(content)

# JSON
import json

data = {"name": "Alice", "age": 25}
with open("data.json", "w") as f:
    json.dump(data, f, indent=2)

with open("data.json", "r") as f:
    loaded = json.load(f)

8. Modules & Packages

Create a Module: math_utils.py

# math_utils.py
def is_even(n: int) -> bool:
    """Check if number is even."""
    return n % 2 == 0

def factorial(n: int) -> int:
    if n == 0:
        return 1
    return n * factorial(n - 1)

Use Module

# main.py
from math_utils import is_even, factorial

print(is_even(4))        # True
print(factorial(5))      # 120

9. Object-Oriented Programming (OOP)

class Dog:
    species = "Canis familiaris"  # class variable

    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    def bark(self) -> str:
        return f"{self.name} says Woof!"

    def __str__(self) -> str:
        return f"{self.name}, {self.age} years old"

# Inheritance
class Bulldog(Dog):
    def __init__(self, name: str, age: int, stubborn: bool = True):
        super().__init__(name, age)
        self.stubborn = stubborn

    def bark(self) -> str:
        return f"{self.name} grumbles!"

# Usage
my_dog = Bulldog("Rex", 3)
print(my_dog)
print(my_dog.bark())

10. Error Handling

def divide(a: float, b: float) -> float:
    try:
        result = a / b
    except ZeroDivisionError:
        print("Cannot divide by zero!")
        return None
    except TypeError:
        print("Invalid input type!")
        return None
    else:
        print("Division successful.")
        return result
    finally:
        print("Operation complete.")

print(divide(10, 0))

11. Working with Libraries

pip install requests pandas numpy
# requests - HTTP
import requests
response = requests.get("https://api.github.com")
print(response.json()["current_user_url"])

# pandas - Data analysis
import pandas as pd
df = pd.DataFrame({
    "Name": ["Alice", "Bob"],
    "Age": [25, 30]
})
print(df.describe())

12. Virtual Environments & Dependency Management

# Create virtual environment
python -m venv myenv

# Activate
# Windows:
myenv\Scripts\activate
# macOS/Linux:
source myenv/bin/activate

# Install packages
pip install requests

# Save dependencies
pip freeze > requirements.txt

# Later: install from file
pip install -r requirements.txt

13. Best Practices & Code Style

Follow PEP 8

  • 4 spaces per indent
  • Max line length: 79 (or 88 with Black)
  • Use snake_case for variables/functions
  • CapWords for classes
  • Meaningful names
# Good
def calculate_area(radius: float) -> float:
    import math
    return math.pi * radius ** 2

# Bad
def calc(r):
    return 3.14 * r * r

Use Type Hints

from typing import List, Dict

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

Use pathlib instead of os.path

from pathlib import Path

file_path = Path("data") / "output.txt"
file_path.parent.mkdir(exist_ok=True)
file_path.write_text("Hello")

Logging > Print

import logging
logging.basicConfig(level=logging.INFO)
logging.info("Process started")

14. Project Structure Example

my_project/
│
├── src/
│   ├── __init__.py
│   ├── main.py
│   ├── utils/
│   │   ├── __init__.py
│   │   └── helpers.py
│   └── models/
│       ├── __init__.py
│       └── user.py
│
├── tests/
│   ├── __init__.py
│   └── test_user.py
│
├── requirements.txt
├── README.md
└── .gitignore

15. Bonus: Mini Project – Task Manager CLI

task_manager.py

from dataclasses import dataclass
from typing import List
import json
from pathlib import Path

@dataclass
class Task:
    title: str
    done: bool = False

class TaskManager:
    def __init__(self, file_path: str = "tasks.json"):
        self.file = Path(file_path)
        self.tasks: List[Task] = self.load()

    def load(self) -> List[Task]:
        if self.file.exists():
            data = json.loads(self.file.read_text())
            return [Task(**t) for t in data]
        return []

    def save(self):
        data = [t.__dict__ for t in self.tasks]
        self.file.write_text(json.dumps(data, indent=2))

    def add(self, title: str):
        self.tasks.append(Task(title))
        self.save()

    def list(self):
        for i, task in enumerate(self.tasks, 1):
            status = "✓" if task.done else "✗"
            print(f"{i}. [{status}] {task.title}")

    def complete(self, index: int):
        if 1 <= index <= len(self.tasks):
            self.tasks[index-1].done = True
            self.save()

def main():
    tm = TaskManager()
    import sys
    if len(sys.argv) < 2:
        print("Usage: python task_manager.py [add|list|done] ...")
        return

    cmd = sys.argv[1]
    if cmd == "add":
        title = " ".join(sys.argv[2:])
        tm.add(title)
    elif cmd == "list":
        tm.list()
    elif cmd == "done" and len(sys.argv) > 2:
        tm.complete(int(sys.argv[2]))
    else:
        print("Unknown command")

if __name__ == "__main__":
    main()

Run:

python task_manager.py add "Learn Python"
python task_manager.py add "Build a project"
python task_manager.py list
python task_manager.py done 1
python task_manager.py list

Final Tips

DoDon't
Write docstringsUse vague names like x, temp
Use virtual environmentsInstall packages globally
Write testsIgnore errors
Use version control (Git)Commit everything at once

Resources


You now have a complete, production-ready Python foundation!
Build projects, contribute to open source, and keep learning.

"Code is like humor. When you have to explain it, it’s bad." – Cory House


Happy Coding!