← Back to Python series
🌱
Basic
int · float · str · bool · type()

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.

variablesintfloatstrbooltype conversion
Duration
2 hours
Level
📊 Beginner
Prerequisite
🎯 Week 1
OUTCOME
Declare variables of all basic types and convert between them

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.

python
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

TypeExampleDescription
int42, -7Whole numbers
float3.14, -0.5Decimal numbers
str"hello"Text (immutable)
boolTrue, FalseBoolean logic
python
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().

python
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 True
⚠️

int("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.

python
name = input("Enter your name: ")
age  = int(input("Enter your age: "))
print(f"Hello, {name}! You are {age} years old.")

5. Common Mistakes

  1. Adding a number to a string without conversion — TypeError: can only concatenate str to str. Use str(num) + "text".
  2. int("3.7") → ValueError. Use int(float("3.7")) instead.
  3. Forgetting that input() returns str — age > 18 fails if age is a string.

💻 Examples

Run these examples and check the output yourself.

01_variables.pyBasic variable assignment and type()
CODE
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))
▶ Output
42 <class 'int'>
3.14 <class 'float'>
Alice <class 'str'>
True <class 'bool'>
02_conversion.pyType conversion examples
CODE
print(int("100"))      # 100
print(float("3.14"))   # 3.14
print(str(42))         # "42"
print(bool(0))         # False
print(bool("hi"))      # True
▶ Output
100
3.14
42
False
True
03_input.pyReading user input and converting types
CODE
name = input("Name: ")
year = int(input("Birth year: "))
age = 2024 - year
print(f"{name} is about {age} years old.")
▶ Output
Name: Alice
Birth year: 2000
Alice is about 24 years old.

📝 Exercises

Try them yourself first, then open the solution to compare.

Exercise 1

Type Inspector

Goal: Print the type and value of at least 5 different variables.

Requirements
  • Assign 5+ variables of different types
  • Print both type() and value for each
Toggle solution
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)
▶ Output
int → 10
float → 3.14
str → hello
bool → True
NoneType → None
Exercise 2

Personal Info Calculator

Goal: Read name and birth year from input, calculate age, and print a greeting.

Requirements
  • Use input() for name and birth year
  • Convert birth year to int
  • Print formatted greeting with calculated age
Sample I/O
Name: Alice
Birth year: 1998
Hello, Alice! You are 26 years old.
Toggle solution
SOLUTION
name = input("Name: ")
year = int(input("Birth year: "))
age = 2024 - year
print(f"Hello, {name}! You are {age} years old.")
Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub ↗