Python Lesson 5: Data Types

🐍 Python CourseLesson 5 of 26 · 19% complete

Every value in Python has a type. The type tells Python what kind of data it is and what operations you can do with it. There are 5 basic types every beginner must know.

The 5 Basic Data Types

# 1. int — whole numbers
age = 25
score = -10
year = 2024

# 2. float — decimal numbers
height = 5.9
price = 19.99
pi = 3.14159

# 3. str — text (string)
name = "Alice"
message = 'Hello, World!'
empty = ""

# 4. bool — True or False only
is_logged_in = True
has_error = False

# 5. None — no value
result = None

Checking Types

print(type(42))       # int
print(type(3.14))     # float
print(type("hello"))  # str
print(type(True))     # bool
print(type(None))     # NoneType

Type Conversion

# str to int
age_text = "25"
age_num = int(age_text)
print(age_num + 1)   # 26

# int to str
score = 100
message = "Your score: " + str(score)
print(message)       # Your score: 100

# str to float
price = float("9.99")
print(price * 2)     # 19.98

# float to int (truncates!)
x = int(3.9)
print(x)             # 3 (not rounded!)

Common Mistake

# ERROR: cannot add str and int
name = "Alice"
age = 25
# print("Name: " + name + " Age: " + age)  ERROR!

# Fix 1: convert age to str
print("Name: " + name + " Age: " + str(age))

# Fix 2: f-string (cleanest!)
print(f"Name: {name} Age: {age}")

🏋️ Practice Task

Create one variable of each type (int, float, str, bool, None). Print each with type() to confirm. Then convert your age (int) to string and build the sentence: “I am [age] years old.”

💡 Hint: print(type(my_variable)) shows the type. Use str(age) to convert.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *