Variables & Data Types

Strings, lists, dictionaries, tuples

BEGINNER

Master Variables & Data Types

The foundation of every Python program. Learn how to store, manipulate, and work with different types of data like a professional developer.

Why Variables Are Essential

Variables are named containers that store data values in memory. Think of them as labeled boxes where you can store information your program needs.

Real World Analogy Python Variable
Label on a storage box name = "Azeem"
Number on a price tag age = 25
Shopping list items fruits = ["apple", "banana"]
name = "Azeem" # String (text) age = 25 # Integer (whole number) height = 5.9 # Float (decimal number) is_student = True # Boolean (True/False) grades = [85, 92, 78] # List (collection)

The 6 Core Data Types

Type Example Use Case Mutable?
str "Hello World" Text, names, messages No
int 42 Counts, IDs, ages No
float 3.14 Prices, measurements No
bool True Conditions, flags No
list [1, 2, 3] Ordered collections Yes
dict {"name": "Bob"} Key-value storage Yes

Strings - Text Mastery

Basic String Operations

# Single & Double Quotes (interchangeable) name = "Azeem" greeting = 'Hello' # Concatenation (+) full_greeting = greeting + ", " + name # "Hello, Azeem" # Length message_length = len("Python") # 6 # Multi-line strings poem = """Roses are red, Violets are blue""" print(poem)

f-Strings (Python 3.6+)

# OLD WAY (messy) name = "Azeem" age = 25 print("My name is " + name + " and I'm " + str(age) + " years old") # NEW WAY (f-strings - CLEAN!) print(f"My name is {name} and I'm {age} years old") print(f"Next year: {age + 1}") # Math works too!

Lists & Dictionaries

Lists - Ordered Collections

# Create lists fruits = ["apple", "banana", "cherry"] numbers = [1, 2, 3, 4, 5] # Access by index (starts at 0) print(fruits[0]) # "apple" print(fruits[-1]) # "cherry" (last item) # Modify lists (MUTABLE) fruits[1] = "blueberry" fruits.append("orange") # Slicing print(fruits[1:3]) # ["blueberry", "cherry"]

Dictionaries - Key-Value Power

# Create dictionary student = { "name": "Azeem", "age": 25, "grades": [85, 92, 88], "active": True } # Access values print(student["name"]) # "Azeem" print(student.get("age")) # 25 (safer) # Add/Update student["city"] = "NYC" student["age"] = 26 # Check existence if "email" in student: print("Has email")

Tuples - Immutable Lists

Tuples are like lists but cannot be modified. Perfect for fixed data like coordinates or configuration values.

# Coordinates (immutable) location = (40.7128, -74.0060) # NYC lat, lon = location # Unpacking print(f"Lat: {lat}, Lon: {lon}") # Multiple assignment name, age, city = "Bob", 30, "LA" print(f"{name} is {age} from {city}")

Practice Exercises

Exercise 1: Personal Profile

Create variables for your name, age, favorite color, and skills list. Print a formatted introduction using f-strings.

# YOUR CODE HERE name = "YOUR_NAME" # ... complete the exercise

Exercise 2: Shopping Cart

Build a shopping cart dictionary with items and prices. Calculate total cost and add tax.

Exercise 3: Student Gradebook

Create a gradebook dictionary with multiple students. Calculate class average and find top performer.

Common Mistakes

# ❌ WRONG - Don't do this age = 25 age = age + 1 # Creates new variable
# ✅ CORRECT - Use += age = 25 age += 1 # age = age + 1 (cleaner)

Type Conversion

# Convert between types age_str = "25" # String age_int = int(age_str) # Convert to int: 25 price = 19.99 price_int = int(price) # Truncates: 19 is_active = bool(1) # True is_empty = bool("") # False

You've Mastered Variables!

✅ What You Can Now Build:

Personal Info App
Store & display user data
Shopping Cart
Items, prices, totals
Student Gradebook
Multiple students & averages
Configuration Manager
Settings & preferences

Next Steps: Control Flow (if/else, loops) → Functions → Real Projects