Lesson 3: Variables, Data Types, and Type Conversion

How Python stores and handles different kinds of data

What Are Variables?

Variables are names that store values in memory. In Python, you create a variable simply by assigning a value to a name:

name = "Alice"
age = 25
height = 1.72

Python uses dynamic typing, meaning you don’t need to declare a type before assigning a value.

Core Data Types

Python includes several built‑in data types used in everyday programming:

Strings

message = "Hello, world!"

Integers

count = 42

Floats

price = 19.99

Booleans

is_active = True

None (represents “no value”)

result = None

Checking Data Types

You can check the type of any variable using type():

print(type(name))     # <class 'str'>
print(type(age))      # <class 'int'>
print(type(height))   # <class 'float'>

Type Conversion (Casting)

Python allows converting between types when possible:

age_str = "30"
age_num = int(age_str)     # "30" → 30

pi = 3.14
pi_str = str(pi)           # 3.14 → "3.14"

flag = bool(1)             # 1 → True

Common Conversion Pitfalls

Working With User Input

All input from input() is a string, so conversion is often needed:

age = input("Enter your age: ")
age = int(age)
print(age + 5)

Next Steps

Now that you understand variables and data types, you’re ready to explore how Python handles text in Lesson 4: Working with Strings.

← Back to Lesson Index