🐍 Python

Variables & Data Types

Lesson 2 of 5 ~6 min

Python uses dynamic typing, meaning you don't need to declare a variable's type — Python figures it out automatically. Just assign a value and you're good to go.

Dynamic typing in action

Editor
Python
Output

Try it

Change the values of the variables and watch how type() reflects their data types.

Data types

# Integer — whole numbers
count = 42

# Float — decimal numbers
price = 19.99

# String — text
greeting = "Hello, World!"

# Boolean — True or False
is_active = True

# None — absence of a value
result = None

Type conversion

You can convert between types explicitly using built-in functions:

# String to integer
num = int("42")       # 42

# Integer to string
text = str(42)        # "42"

# Integer to float
decimal = float(5)    # 5.0

# Float to integer (truncates)
whole = int(3.7)      # 3

F-strings

F-strings let you embed variables directly inside strings using f"..." syntax:

name = "Alice"
age = 25
print(f"Hi, I'm {name} and I'm {age}.")
# Output: Hi, I'm Alice and I'm 25.

# You can also do expressions
print(f"In 5 years, I'll be {age + 5}.")
# Output: In 5 years, I'll be 30.

Quiz

How do you check a variable's type in Python?
Challenge

Create one variable of each type (int, float, str, bool, None) and print them all using f-strings.