DEVOLOGIST</>

youtube

Python Data Types: Knowing Your Numbers from Your Text

Python
06/07/20263 min read

Introduction

In Python, you can't treat every piece of data the same way. You can do math with numbers, but you can't do math with words.

Python uses data types to know exactly what kind of information it is dealing with.

The Big Four Data Types

Here are the four most important types you will use every day:

Let's Look at the Code

You can see these in action by using the type() function. This function tells you what type of data you are using.

age = 25 # Integer
price = 19.99 # Float
name = "Alex" # String
is_active = True # Boolean
print(type(age))
print(type(price))
print(type(name))
print(type(is_active))

When you run this code, Python checks each value and prints its data type.

Why Does This Matter?

If you try to combine the wrong types, Python will stop you.

# This will cause an error!
print("I am " + 25)

Python sees "I am " as a string and 25 as an integer.

You cannot add them directly because they are not the same type. You have to convert them first, which we will cover in a future lesson.

Summary Table

TypePython NameExample
Textstr"Hello"
Whole Numberint10
Decimalfloat10.5
LogicboolTrue

Conclusion

Data types help Python understand what kind of value it is working with.

Once you understand integers, floats, strings, and booleans, you can write programs that handle information more correctly and safely.