Python Lesson: Data Types

1. What Is a Data Type?

A data type tells Python what kind of value a variable holds. Different data types allow different operations.

x = 10
y = "Hello"
z = True
    

2. Integers (int)

Integers are whole numbers (positive, negative, or zero).

age = 25
temperature = -5
count = 0
    

Common operations:

print(10 + 5)
print(10 - 3)
print(4 * 2)
print(10 // 3)
    

3. Floating-Point Numbers (float)

Floats are numbers with decimal points.

price = 19.99
pi = 3.14
average = 7.5
    
print(5 / 2)
print(2.5 + 1.5)
    

4. Strings (str)

Strings store text and must be inside quotes.

name = "Alice"
greeting = 'Hello'
sentence = "Python is fun!"
    

String operations:

print("Hello" + " World")
print("Hi" * 3)
    

5. Booleans (bool)

Booleans represent True or False. They are often used in conditions.

is_logged_in = True
has_permission = False
    
print(5 > 3)
print(10 == 5)
    

6. Checking Data Types

Use the type() function to check a value’s data type.

print(type(10))
print(type(3.14))
print(type("Hello"))
print(type(True))
    

7. Type Conversion (Casting)

You can convert between data types using built-in functions.

x = "10"
y = int(x)
z = float(y)
s = str(z)
    
⚠️ Not all conversions are valid. For example, int("abc") will cause an error.

8. Mixing Data Types

Mixing incompatible data types can cause errors.

print(5 + 3)        # Works
print("5" + "3")    # Works
print(5 + "3")      # Error
    

9. Summary

10. Practice Exercises

  1. Create a variable for your age and print its type.
  2. Add two floats and print the result.
  3. Convert a number to a string and print it.
  4. Check if 10 is greater than 5.
Quiz