← Project 1

Lesson 3

Data Type

~30 min

1. What are Data Types?

In Python, a data type defines what kind of value a variable stores and how Python should interpret it. Every value in Python—whether it's a number, text, or true/false statement—belongs to a specific data type. Python is **dynamically typed**, meaning it automatically figures out the data type based on the value you assign.

Think of data types like different containers: a box for numbers, a box for text, a box for true/false values, and so on. Each container has specific rules for what can go inside and how it behaves.

2. Why Data Types Matter

Understanding data types is essential because:

  • Operations differ by type: Adding two numbers gives a sum, but "adding" two strings joins them together.
  • Prevents errors: You can't divide text by 2. Knowing types helps avoid crashes.
  • Memory efficiency: Different types use different amounts of memory. Choosing the right type makes your code faster.
  • Code readability: When you see data types, you instantly understand what kind of information is being stored.

3. The Four Core Data Types

🔢 Integer (int)

Integers are whole numbers without decimal points. They can be positive, negative, or zero.

Examples:

  • age = 18
  • marks = 95
  • temperature = -10
  • balance = 0
age = 18
marks = 95
temperature = -10

print(age)
print(type(age))
print(type(marks))

Output:

18
<class 'int'>
<class 'int'>

🔗 Float (float)

Floats are decimal numbers. They represent values that need fractional parts.

Examples:

  • height = 5.9
  • price = 19.99
  • temperature = 36.5
  • percentage = 99.9
height = 5.9
price = 19.99
gpa = 3.85

print(height)
print(type(height))
print(type(price))

Output:

5.9
<class 'float'>
<class 'float'>

📝 String (str)

Strings are sequences of characters enclosed in quotes (either single or double). They represent text data.

Examples:

  • name = "Alice"
  • city = 'New York'
  • message = "Hello, World!"
  • username = "john_doe"
name = "Alice"
city = 'New York'
message = "Python is fun!"

print(name)
print(type(name))
print(message)

Output:

Alice
<class 'str'>
Python is fun!

✓/✗ Boolean (bool)

Booleans represent truth values: either True or False. Note the capital letters—Python is case-sensitive.

Examples:

  • is_student = True
  • is_raining = False
  • has_license = True
is_student = True
is_raining = False
has_license = True

print(is_student)
print(type(is_student))
print(type(has_license))

Output:

True
<class 'bool'>
<class 'bool'>

4. Checking Data Types with type()

Use the built-in type() function to check the data type of any variable. This is incredibly useful for debugging.

name = "Sam"
age = 18
score = 94.5
is_passing = True

print(type(name))         # <class 'str'>
print(type(age))          # <class 'int'>
print(type(score))        # <class 'float'>
print(type(is_passing))   # <class 'bool'>

5. Converting Between Data Types

Sometimes you need to convert a value from one type to another. Python provides functions for this: int(), float(), and str().

# Convert string to integer
age_string = "25"
age_int = int(age_string)
print(age_int + 5)        # 30

# Convert integer to string
number = 42
text = str(number)
print("The answer is " + text)  # The answer is 42

# Convert to float
price = float("19.99")
print(price)              # 19.99
print(type(price))        # <class 'float'>

6. Real-World Examples

Student Profile System:

student_name = "Alice"        # str
student_id = 12345            # int
gpa = 3.85                     # float
is_active = True               # bool

print(f"Name: {student_name}")
print(f"ID: {student_id}")
print(f"GPA: {gpa}")
print(f"Active: {is_active}")

Shopping Cart:

product = "Laptop"             # str
price = 999.99                 # float
quantity = 2                   # int
in_stock = True                # bool

total = price * quantity
print(f"{product}: ${price} x {quantity} = ${total}")
print(f"In Stock: {in_stock}")

7. Common Beginner Mistakes

⚠️ Watch out for these mistakes:

  • Forgetting Quotes Around Strings: name = John causes an error. Use name = "John" instead.
  • Mixing Strings and Numbers in Concatenation: print("Age: " + 18) fails. Use print("Age: " + str(18)) or f-strings: print(f"Age: {18}").
  • Using Wrong Boolean Capitalization: is_active = true causes an error. Use is_active = True (capital T).
  • Decimal Point Confusion: 3,14 (comma) is not a float. Use 3.14 (period) instead.
  • Assuming Types Convert Automatically: result = "100" + 50 fails. Convert first: result = int("100") + 50.
Interactive Practice

Mini Practice: Student Profile Builder

Create a program that stores information about yourself using different data types. Store your name (string), age (integer), height in meters (float), and whether you're a student (boolean). Then print all values with their types using the type() function.

0% complete
Check when done
Quiz

Test Your Understanding

1. What data type would you use to store a person's age?

2. What is the output of `type("Hello")`?

3. What will happen if you try: `print("Price: " + 19.99)`?

🚀 Progression

You've mastered data types! Now you understand the building blocks of programming. Next, you'll learn how to make decisions with **Conditionals** (if/else statements).