Python Tutorial — Learn Python Step by Step
Key takeaway: Master Python programming from scratch — variables, loops, functions, OOP, file handling, and libraries — with clear human-readable explanations and runnable code examples.
Python CSV Overview
CSV (Comma-Separated Values) is a simple format for storing tabular data. Python's built-in csv module makes reading and writing CSV files easy and reliable.
What Is CSV?
# CSV is plain text with values separated by commas (or other delimiters)
# Example CSV file (data.csv):
# name,age,city
# Alice,30,New York
# Bob,25,London
# Charlie,35,Tokyo
# Each line is a row, commas separate columns
# First row is usually the headerReading CSV Files
import csv
# Basic reading with csv.reader (returns lists)
with open("data.csv", "r") as f:
reader = csv.reader(f)
header = next(reader) # first row is header
print(header) # ["name", "age", "city"]
for row in reader:
name, age, city = row
print(f"{name} is {age} years old from {city}")
# Reading as dictionaries (more readable)
with open("data.csv", "r") as f:
reader = csv.DictReader(f)
for row in reader:
# Each row is an OrderedDict with header keys
print(f"{row[\"name\"]} - {row[\"city\"]}")
# Reading with different delimiter
with open("data.tsv", "r") as f:
reader = csv.reader(f, delimiter="\t") # tab-separated
for row in reader:
print(row)Writing CSV Files
import csv
# Writing with csv.writer (from lists)
data = [
["name", "age", "city"],
["Alice", 30, "New York"],
["Bob", 25, "London"],
["Charlie", 35, "Tokyo"],
]
with open("output.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(data[0]) # write header
writer.writerows(data[1:]) # write all data rows
# Writing with DictWriter (from dictionaries)
users = [
{"name": "Alice", "age": 30, "city": "New York"},
{"name": "Bob", "age": 25, "city": "London"},
]
with open("users.csv", "w", newline="") as f:
fieldnames = ["name", "age", "city"]
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader() # writes the header row
writer.writerows(users)
# IMPORTANT: always use newline="" on Windows to prevent blank rowsHandling Edge Cases
import csv
# Values with commas, quotes, or newlines are auto-quoted
data = [
["name", "address", "note"],
["Alice", "123 Main St, Apt 4", "She said \"hello\""],
["Bob", "456 Oak Ave", "Line 1\nLine 2"],
]
with open("special.csv", "w", newline="") as f:
writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
writer.writerows(data)
# Reading with different encodings
with open("data.csv", "r", encoding="utf-8-sig") as f: # handles BOM
reader = csv.reader(f)
for row in reader:
print(row)
# Skip empty rows
with open("data.csv") as f:
reader = csv.reader(f)
for row in reader:
if not row or all(cell.strip() == "" for cell in row):
continue
print(row)Practical Example: Data Processing
import csv
# Filter and transform CSV data
def filter_csv(input_file, output_file, min_age=25):
"""Read a CSV, filter by age, and write results."""
with open(input_file) as fin, open(output_file, "w", newline="") as fout:
reader = csv.DictReader(fin)
writer = csv.DictWriter(fout, fieldnames=reader.fieldnames)
writer.writeheader()
count = 0
for row in reader:
if int(row["age"]) >= min_age:
writer.writerow(row)
count += 1
print(f"Wrote {count} rows to {output_file}")
filter_csv("people.csv", "adults.csv", min_age=18)
# Convert CSV to list of dicts (load all into memory)
def load_csv(filename):
with open(filename) as f:
return list(csv.DictReader(f))
people = load_csv("people.csv")
print(f"Loaded {len(people)} records")
print(people[0]) # {"name": "Alice", "age": "30", "city": "New York"}- Use
csv.DictReader/csv.DictWriterfor readable, header-based access. - Always pass
newline=""when opening files for writing on Windows. - The csv module handles quoting, escaping commas, and newlines inside values automatically.
- For large CSV files, process row-by-row instead of loading everything into memory.
- For complex data analysis, consider
pandas.read_csv()which is faster and more powerful.
Python Programming Tutorial — Learn Python in a practical, step-by-step way. According to the official Python documentation, Python emphasizes code readability and simplicity, making it ideal for beginners.
Related tools: Python Online Compiler | Python Examples | JavaScript Tutorial | Java Tutorial
Python Career & Learning Guide
Python is the world's most popular programming language for beginners, data science, AI/ML, web development, and automation. This tutorial teaches Python step-by-step with clear explanations and runnable code examples in our free Python Compiler.
Why Learn Python in 2026?
- #1 Most Popular Language: Ranked first on TIOBE, Stack Overflow, and GitHub.
- AI and Data Science: The primary language for machine learning (TensorFlow, PyTorch, scikit-learn) and data analysis (Pandas, NumPy).
- Web Development: Django and Flask power scalable backends at companies like Instagram and Spotify.
- Automation & Scripting: Automate files, emails, web scraping, and data processing in minutes.
Python vs Other Languages
| Feature | Python | Java | JavaScript | C++ |
|---|---|---|---|---|
| Syntax | Very clean, readable | Verbose | Moderate | Complex |
| Typing | Dynamic, strong | Static, strong | Dynamic, weak | Static, strong |
| Best For | AI/ML, Data, Web | Enterprise, Android | Web Frontend/Backend | Systems, Games |
| Learning Time | 2–4 weeks basics | 4–6 weeks basics | 3–4 weeks basics | 8–12 weeks basics |
Frequently Asked Questions
Do I need prior programming experience to learn Python?
No! Python is specifically designed to be beginner-friendly. This tutorial starts from zero and builds up gradually.
Which Python version should I use?
Python 3.10+ is recommended. All examples in this tutorial use Python 3 syntax.
Is this Python tutorial completely free?
Yes, 100% free with no registration or sign-up required. Access all lessons and code examples anytime.