← Back to Python Basics
Module 1Lesson 230 minutes

Variables and Data Types

What are Variables?

Variables are containers that store data values. Think of them as labeled boxes where you put information.

name = "Alice" # A box labeled 'name' contains "Alice" age = 25 # A box labeled 'age' contains 25

Why Use Variables?

Without variables (repeating values):

print("Alice") print("Alice is 25 years old") print("Alice likes Python")

With variables (reusable):

name = "Alice" age = 25 print(name) print(name, "is", age, "years old") print(name, "likes Python")

Change the value once, it updates everywhere!


Creating Variables

Basic Syntax

variable_name = value

Examples

# Numbers score = 100 temperature = 98.6 pi = 3.14159 # Text (strings) greeting = "Hello" name = "Bob" city = 'New York' # True/False (booleans) is_active = True is_logged_in = False

Rules for Variable Names

Allowed:

age = 25 user_name = "Alice" total_score = 100 _private = "hidden" number2 = 42

Not Allowed:

2nd_place = "Silver" # Can't start with number user-name = "Bob" # No hyphens first name = "Alice" # No spaces class = "Python" # Can't use reserved words

Naming Conventions (PEP 8)

Use snake_case for variables:

# Good user_name = "Alice" total_amount = 100 is_valid = True # Avoid userName = "Alice" # camelCase TotalAmount = 100 # PascalCase

Make names descriptive:

# Good - clear meaning student_count = 30 max_temperature = 75 user_email = "alice@example.com" # Bad - unclear x = 30 temp = 75 ue = "alice@example.com"

Data Types

Python has several built-in data types. Let's explore the basics:

1. Integers (int)

Whole numbers without decimals:

age = 25 year = 2025 temperature = -10 large_number = 1000000

Operations:

x = 10 y = 3 print(x + y) # 13 (addition) print(x - y) # 7 (subtraction) print(x * y) # 30 (multiplication) print(x / y) # 3.333... (division) print(x // y) # 3 (integer division) print(x % y) # 1 (remainder/modulus) print(x ** y) # 1000 (exponentiation)

2. Floats (float)

Numbers with decimal points:

price = 19.99 temperature = 98.6 pi = 3.14159 negative = -5.5

Operations:

x = 10.5 y = 2.5 print(x + y) # 13.0 print(x - y) # 8.0 print(x * y) # 26.25 print(x / y) # 4.2

3. Strings (str)

Text enclosed in quotes:

name = "Alice" message = 'Hello, World!' address = """123 Main Street Apt 4B New York, NY"""

String Operations:

first_name = "John" last_name = "Doe" # Concatenation full_name = first_name + " " + last_name print(full_name) # John Doe # Repetition laugh = "ha" * 3 print(laugh) # hahaha # Length print(len(full_name)) # 8

4. Booleans (bool)

True or False values:

is_student = True is_graduated = False has_license = True print(5 > 3) # True print(10 == 5) # False print(7 < 2) # False

Checking Types

Use type() to check a variable's type:

age = 25 print(type(age)) # <class 'int'> price = 19.99 print(type(price)) # <class 'float'> name = "Alice" print(type(name)) # <class 'str'> is_active = True print(type(is_active)) # <class 'bool'>

Type Conversion

Convert between types:

# String to Integer age_str = "25" age_int = int(age_str) print(age_int + 5) # 30 # String to Float price_str = "19.99" price_float = float(price_str) print(price_float * 2) # 39.98 # Number to String score = 100 score_str = str(score) print("Your score is " + score_str) # Integer to Float x = 5 y = float(x) print(y) # 5.0

Multiple Assignment

Assign multiple variables at once:

# Multiple assignment x, y, z = 10, 20, 30 print(x, y, z) # 10 20 30 # Same value to multiple variables a = b = c = 100 print(a, b, c) # 100 100 100 # Swap values x, y = 5, 10 x, y = y, x print(x, y) # 10 5

Practice Exercises

  1. Create variables for your name, age, and city
  2. Calculate and print your age in months
  3. Create a greeting message using string concatenation
  4. Convert a string number to an integer and perform math
  5. Check the types of different variables

Example Solution:

# Exercise 1 name = "Alice" age = 25 city = "New York" # Exercise 2 age_in_months = age * 12 print("Age in months:", age_in_months) # Exercise 3 greeting = "Hello, my name is " + name print(greeting) # Exercise 4 num_str = "100" num_int = int(num_str) result = num_int + 50 print("Result:", result) # Exercise 5 print(type(name)) print(type(age)) print(type(city))

Key Takeaways

✅ Variables store data values
✅ Use descriptive names in snake_case
✅ Main types: int, float, str, bool
✅ Use type() to check data types
✅ Convert types with int(), float(), str()

Next Lesson: Basic Operations