Data types
Different kinds of information in Python
In Python, data types are super important! They tell Python what kind of information you are working with.
What are data types?
Think of data types as categories for different kinds of information. Is it a number? Some text? Python needs to know so it can handle it correctly.
For example, you can do math with numbers, but you can't multiply words together like numbers.
The main types
Python has several built-in data types. Here are some of the most common ones you'll see often:
int: For whole numbers (like 5, -10, 0)float: For numbers with decimal points (like 3.14, -0.5)str: For text (like "hello", "Python is fun!")bool: For True or False values
my_integer = 10
my_float = 2.5
my_string = "Welcome!"
is_active = TrueWhy we need data types
Data types help Python understand what operations are allowed. Can you add "apple" and "banana"? Python can, but it joins them, not like adding numbers.
num1 = 5
num2 = 3
print(num1 + num2) # Output: 8
word1 = "Hello"
word2 = "World"
print(word1 + word2) # Output: HelloWorldChecking types
You can always check the data type of a variable using the type() function. This is very handy when you're not sure what kind of data you're dealing with.
age = 25
name = "Alice"
salary = 50000.75
print(type(age))
print(type(name))
print(type(salary))Start learning
Understanding data types is a core concept in programming. You'll use them all the time. Don't worry if it feels a bit abstract at first; it will become second nature as you write more code.
Practice
Try these exercises to lock in what you just learned.