Python Programming: A Practical
Tutorial
From Beginner to Confident Coder — Exercises, Examples & Best Practices
Chapter 1: Getting Started with Python
Python is one of the world's most popular programming languages, celebrated for its
readability, versatility, and vast ecosystem of libraries. It powers web applications, data
science, machine learning, automation, and scientific computing. Python's philosophy
emphasizes code that is easy to read and write — making it the ideal first language for
beginners and a trusted tool for experts.
Your First Python Program
# Hello World print('Hello, World!') # Variables and basic data types name =
'Alice' age = 30 height = 1.75 is_student = False print(f'Name: {name}, Age:
{age}')
Chapter 2: Data Structures
Python provides four built-in collection types, each optimized for different use cases. Lists are
ordered, mutable sequences ideal for storing sequences of items. Tuples are immutable
sequences useful for fixed collections. Dictionaries store key-value pairs and provide O(1)
average-case lookup. Sets store unique elements and support mathematical set operations.
# Lists fruits = ['apple', 'banana', 'cherry'] [Link]('mango')
print(fruits[0]) # apple # Dictionaries person = {'name': 'Bob', 'age': 25,
'city': 'Delhi'} print(person['name']) # Bob # List comprehension squares =
[x**2 for x in range(10)] print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Chapter 3: Functions and Modules
Functions are the fundamental building blocks of modular, reusable code. Python supports
default arguments, keyword arguments, *args for variable positional arguments, and **kwargs
for variable keyword arguments. Lambda functions provide concise inline function definitions.
Python's standard library contains hundreds of modules covering everything from file I/O to
network programming.
def calculate_bmi(weight_kg, height_m): '''Calculate Body Mass Index.''' bmi =
weight_kg / (height_m ** 2) if bmi < 18.5: category = 'Underweight' elif bmi <
25: category = 'Normal' elif bmi < 30: category = 'Overweight' else: category =
'Obese' return round(bmi, 2), category bmi, cat = calculate_bmi(70, 1.75)
print(f'BMI: {bmi} ({cat})') # BMI: 22.86 (Normal)
Chapter 4: Object-Oriented Programming
Object-oriented programming (OOP) organizes code around objects that combine data
(attributes) and behavior (methods). Python supports all core OOP principles: encapsulation,
inheritance, and polymorphism. Classes serve as blueprints for creating objects. Using OOP
leads to more maintainable, scalable, and reusable code in larger projects.
class BankAccount: def __init__(self, owner, balance=0): [Link] = owner
self._balance = balance def deposit(self, amount): if amount > 0: self._balance
+= amount return f'Deposited {amount}. Balance: {self._balance}' def
withdraw(self, amount): if amount <= self._balance: self._balance -= amount
return f'Withdrew {amount}. Balance: {self._balance}' return 'Insufficient
funds' acc = BankAccount('Alice', 1000) print([Link](500)) # Deposited 500.
Balance: 1500 print([Link](200)) # Withdrew 200. Balance: 1300
Quick Reference: Common Built-in Functions
Function Description Example
len() Returns length of an object len([1,2,3]) → 3
range() Generates a sequence of numbers range(0, 10, 2)
zip() Combines multiple iterables zip([1,2], ["a","b"])
map() Applies function to all items map(str, [1, 2, 3])
filter() Filters items by condition filter(lambda x: x>0, lst)
sorted() Returns sorted list sorted([3,1,2]) → [1,2,3]
enumerate() Adds index to iterable enumerate(["a","b","c"])