Week 2 — Variables & Data Types
Explore Python's four core data types, learn how to assign variables, use type() to inspect values, and perform basic type conversions.
What you'll learn
- 1Declare and reassign variables
- 2Distinguish int, float, str, and bool
- 3Use type() to inspect a value's type
- 4Convert between types with int(), float(), str()
- 5Understand dynamic typing in Python
1. Variables
A variable is a named container for a value. Python uses dynamic typing — you don't declare the type, Python infers it.
x = 10
name = "Alice"
is_student = True
print(type(x)) # <class 'int'>Variable names are case-sensitive. Use snake_case by convention (my_variable, not myVariable).
2. Core Data Types
| Type | Example | Description |
|---|---|---|
| int | 42, -7 | Whole numbers |
| float | 3.14, -0.5 | Decimal numbers |
| str | "hello" | Text (immutable) |
| bool | True, False | Boolean logic |
age = 25 # int
price = 9.99 # float
greeting = "Hi!" # str
active = True # bool
print(type(age)) # <class 'int'>3. Type Conversion
Convert between types using built-in functions: int(), float(), str(), bool().
num_str = "42"
num = int(num_str) # "42" → 42
pi = float("3.14") # "3.14" → 3.14
text = str(100) # 100 → "100"
print(bool(0), bool(1)) # False Trueint("3.7") raises ValueError — convert to float first: int(float("3.7"))
4. User Input
input() always returns a string. Remember to convert when you need a number.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello, {name}! You are {age} years old.")5. Common Mistakes
- Adding a number to a string without conversion — TypeError: can only concatenate str to str. Use str(num) + "text".
- int("3.7") → ValueError. Use int(float("3.7")) instead.
- Forgetting that input() returns str — age > 18 fails if age is a string.
💻 Examples
Run these examples and check the output yourself.
x = 42
pi = 3.14
name = "Alice"
flag = True
print(x, type(x))
print(pi, type(pi))
print(name, type(name))
print(flag, type(flag))
42 <class 'int'>
3.14 <class 'float'>
Alice <class 'str'>
True <class 'bool'>print(int("100")) # 100
print(float("3.14")) # 3.14
print(str(42)) # "42"
print(bool(0)) # False
print(bool("hi")) # True
100
3.14
42
False
Truename = input("Name: ")
year = int(input("Birth year: "))
age = 2024 - year
print(f"{name} is about {age} years old.")
Name: Alice
Birth year: 2000
Alice is about 24 years old.📝 Exercises
Try them yourself first, then open the solution to compare.
Type Inspector
Goal: Print the type and value of at least 5 different variables.
- Assign 5+ variables of different types
- Print both type() and value for each
▶Toggle solution
a = 10
b = 3.14
c = "hello"
d = True
e = None
for v in [a, b, c, d, e]:
print(type(v).__name__, "→", v)
int → 10
float → 3.14
str → hello
bool → True
NoneType → NonePersonal Info Calculator
Goal: Read name and birth year from input, calculate age, and print a greeting.
- Use input() for name and birth year
- Convert birth year to int
- Print formatted greeting with calculated age
Name: Alice
Birth year: 1998
Hello, Alice! You are 26 years old.▶Toggle solution
name = input("Name: ")
year = int(input("Birth year: "))
age = 2024 - year
print(f"Hello, {name}! You are {age} years old.")
All lecture materials and example code are openly available on GitHub.
View on GitHub ↗