Python — Class Notes (Concise & Practical)
Author: ChatGPT — Python crash course notes
1. Introduction
Python is a high-level, interpreted programming language known for readability and rapid development. Use
cases: scripting, web development, data science, automation, machine learning, and more.
2. Setting up
Install Python (3.8+ recommended). Use virtual environments: python -m venv venv and activate
(venv\Scripts\activate on Windows, source venv/bin/activate on macOS/Linux). Install packages with pip install
package_name.
3. Basic Syntax & Execution
Run a script: python script.py. REPL: python or ipython. File extension: .py.
4. Variables & Data Types
Common types: int, float, bool, str, list, tuple, dict, set, NoneType. Dynamic typing: types are checked at
runtime.
5. Operators
Arithmetic: + - * / // % **. Comparison: == != > < >= <=. Logical: and, or, not. Membership: in, not in. Identity: is,
is not.
6. Control Flow
if, elif, else. for loops (iterate over iterables). while loops. break, continue, else on loops.
7. Functions
Define with def name(params):. Return values with return. Default args, *args, **kwargs, keyword-only args.
Docstrings with triple quotes.
8. Data Structures (short examples)
List: ordered, mutable -> lst = [1,2,3]. Tuple: ordered, immutable -> t = (1,2). Dict: key-value -> d = {'a':1}. Set:
unordered unique -> s = {1,2,3}.
9. Comprehensions & Generators
List comprehension: [x*x for x in range(5)]. Dict comprehension: {k:v for k,v in pairs}. Generator expression: (x
for x in range(5)). Generators with yield.
10. Modules & Packages
Import: import math or from math import sqrt. Create packages with __init__.py. Use pip and PyPI for external
packages.
11. File I/O
Open files: with open('file.txt','r') as f:. Read: f.read(), f.readline(), f.readlines(). Write: f.write(text) and
f.writelines(list_of_lines)
12. Exceptions
Handle errors: try / except / finally / else. Raise exceptions: raise ValueError('msg'). Custom exceptions by
subclassing Exception.
13. Object-Oriented Programming
Define classes with class MyClass:. Constructor: def __init__(self, ...):. Instance vs class attributes, inheritance,
method overriding, magic methods like __str__, __repr__, __len__.
14. Useful Standard Library Modules
os, sys, pathlib, json, datetime, collections, itertools, functools, math, random, subprocess, logging, re
15. Virtual Environments & Packaging
venv, pip, pipenv, poetry for dependency and packaging management. setup.cfg / pyproject.toml for packaging.
16. Testing & Debugging
Use print() for quick checks, pdb or python -m pdb script.py for debugging. Unit testing: unittest, pytest
recommended. Logging module for production diagnostics.
17. Type Hints (Optional but helpful)
Function annotation: def f(x: int) -> str:. Use mypy or IDE support for static type checking.
18. Common Tools & Workflow
IDEs: VS Code, PyCharm, Jupyter Notebooks for data work. Version control: git + GitHub/GitLab. CI/CD
basics, linters: flake8, black.
19. Short Examples
1) Fibonacci function (iterative)
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
2) Read CSV simple
import csv
with open('data.csv') as f:
for row in csv.reader(f):
print(row)
3) Simple class
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says woof"
20. Exercises (Practice)
Write a function to check for prime numbers. Reverse a string without slicing. Count word frequencies in a text
file. Implement a basic CLI calculator. Create a small package and publish locally.
21. Resources to Learn More
Official docs: https://docs.python.org/3/. Real Python, freeCodeCamp, Automate the Boring Stuff (book),
Python Crash Course (book).
22. Quick Tips
Read error messages — they tell you what went wrong and where. Keep functions small and focused. Write
tests for important logic. Use version control early.
Appendix — Python Cheat Sheet
Run file: python script.py
Create venv: python -m venv venv
Install package: pip install package_name
List files: ls or dir
Show Python version: python --version