This tutorial is designed to take you from a beginner to an advanced level in Python programming. We will cover basic concepts with real-world examples, code snippets, and datasets. Each section will include problem-solving exercises and solutions.
Table of Contents
- Basic Python
- Variables, Data Types, and Operators
- Control Structures (if-else, loops)
- Functions
- Working with Lists, Tuples, and Dictionaries
- File Handling
1. Basic Python
1.1 Variables, Data Types, and Operators
Python supports various data types like integers, floats, strings, and booleans.
# Example
x = 10 # Integer
y = 20.5 # Float
name = “Alice” # String
is_active = True # Boolean
# Arithmetic Operations
sum = x + y
print(“Sum:”, sum)
1.2 Control Structures
Control structures include if-else statements and loops (for, while).
# If-Else Example
age = 18
if age >= 18:
print(“You are an adult.”)
else:
print(“You are a minor.”)
# For Loop Example
for i in range(5):
print(i)
# While Loop Example
count = 0
while count < 5:
print(count)
count += 1
1.3 Functions
Functions are reusable blocks of code.
# Function Example
def greet(name):
return f”Hello, {name}!”
print(greet(“Alice”))
1.4 Lists, Tuples, and Dictionaries
- Lists: Ordered, mutable collections.
- Tuples: Ordered, immutable collections.
- Dictionaries: Key-value pairs.
# List Example
fruits = [“apple”, “banana”, “cherry”]
fruits.append(“orange”)
print(fruits)
# Tuple Example
coordinates = (10.0, 20.0)
print(coordinates)
# Dictionary Example
person = {“name”: “Alice”, “age”: 25}
print(person[“name”])
1.5 File Handling
Reading and writing files in Python.
# Writing to a file
with open(“example.txt”, “w”) as file:
file.write(“Hello, World!”)
# Reading from a file
with open(“example.txt”, “r”) as file:
content = file.read()
print(content)