Python Tutorial – Learn Python from Scratch

python tutorial

Python Tutorial

Lesson 1: Introduction to Python

Python is one of the most popular programming languages today, widely used for web development, data analysis, artificial intelligence, and automation. Its simple and readable syntax makes it beginner‑friendly, while powerful libraries make it suitable for advanced projects too.

Example

print("Hello, Python!")
Hello, Python!

Lesson 2: History & Features of Python

Before diving deep, it’s useful to know Python’s journey and why it stands out. Created by Guido van Rossum in the late 1980s, Python focuses on simplicity and readability—values that still define it today.

Key Milestones

  • 1989 – Development started
  • 1991 – First release
  • 2000 – Python 2.0
  • 2008 – Python 3.0

Core Features

  • Easy to learn & read
  • Interpreted & cross‑platform
  • Extensive standard library & ecosystem
  • Object‑oriented & functional support

Lesson 3: Installing Python

To start coding, install Python on your system. Python is available for Windows, macOS, and Linux, and the setup is beginner‑friendly. Remember to enable Add to PATH on Windows.

Quick Steps

  1. Visit python.org/downloads
  2. Download the latest version
  3. Install and verify:
python --version

Lesson 4: Your First Python Program

Your first Python program is a simple way to confirm everything works. Open a terminal or an editor and run this line:

print("Welcome to Python Tutorial!")
Welcome to Python Tutorial!

Lesson 5: Python Syntax

Python’s syntax is clean and readable. Instead of curly braces, indentation defines code blocks—usually four spaces. Correct indentation is essential to avoid errors.

if 5 > 2:
    print("Five is greater than two!")

Lesson 6: Variables

Variables store data. You don’t need to declare a type—Python infers it from the assigned value. Use descriptive names to keep code readable.

name = "John"
age = 25
print(name, age)
John 25

Lesson 7: Data Types

Data types describe the kind of values variables hold. Common built‑ins include integers, floats, strings, lists, tuples, dictionaries, and booleans.

x = 10       # int
y = 3.14     # float
name = "Ali" # str
nums = [1,2,3] # list
info = {"lang":"Python","ver":3} # dict

Lesson 8: Operators

Operators perform actions on values: arithmetic (+ - * / % ** //), comparison (== != > < >= <=), and logical (and, or, not).

x = 5
y = 3
print(x + y)  # 8
8

Lesson 9: Conditional Statements

Conditionals let your program make decisions using if, elif, and else. They run different code paths based on truth values.

age = 18
if age >= 18:
    print("You are an adult")
else:
    print("You are a minor")
You are an adult

Lesson 10: Loops

Loops repeat actions. Use for for iterables and while for condition‑based repetition.

for i in range(5):
    print(i)
0
1
2
3
4
count = 0
while count < 3:
    print(count)
    count += 1
0
1
2

Lesson 11: Functions

Functions group reusable logic. Define them with def and return values with return. Keeping functions small improves readability and testing.

def greet(name):
    return "Hello, " + name

print(greet("Ali"))
Hello, Ali

Lesson 12: Modules & Packages

Modules are Python files with reusable code; packages are collections of modules. Importing encourages clean, organized programs.

# mymodule.py

def add(a, b):
    return a + b
# use_module.py
import mymodule
print(mymodule.add(5, 3))
8

Lesson 13: File Handling

Python can create, read, and write files using built‑in functions. Always close files—or, better, use a context manager to do it automatically.

# write
with open("test.txt", "w") as f:
    f.write("Hello File!")

# read
with open("test.txt", "r") as f:
    print(f.read())
Hello File!

Lesson 14: Exception Handling

Exceptions happen when something goes wrong at runtime. Use try/except to handle them gracefully and keep your program stable.

try:
    x = 5 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
Cannot divide by zero

Lesson 15: Object‑Oriented Programming

OOP organizes code into classes (blueprints) and objects (instances). It supports encapsulation, inheritance, and polymorphism—useful for large programs.

class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        print("Hello, my name is", self.name)

p1 = Person("John")
p1.greet()
Hello, my name is John

Lesson 16: Popular Libraries

Python’s ecosystem is massive. Start with NumPy for arrays, Pandas for data analysis, and Matplotlib for plotting. Install packages via pip.

# Terminal
pip install numpy pandas matplotlib
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
[1 2 3]

Lesson 17: Projects & Practice

The best way to learn is by building. Try a calculator, a to‑do app, or a number‑guessing game. Start small, iterate, and keep notes on what you learn.

def add(a, b): return a + b

def sub(a, b): return a - b

print("1: Add\n2: Subtract")
choice = int(input("Enter choice: "))
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))

if choice == 1:
    print("Result:", add(x, y))
else:
    print("Result:", sub(x, y))