01. Basic Data Types
Unlike languages such as Java, C++, or JavaScript, Python does not have primitive data types. Instead, everything in Python is an object, including numbers, strings, and booleans. In this article, I will refer to the most common built-in data types that beginners usually learn first when starting to study any language.
Numeric Types
Integers
age = 30
If we check its type, Python returns: <class 'int'>. What does it mean? First, it confirms what I said in our introduction. Everything in Python is an object, and in this case, the value assigned to age is an instance of the int class.
Float
height = 1.73
If we check its type, Python returns: <class 'float'>. What does it mean? Python's float type is implemented using the IEEE 754 double-precision floating-point format on most systems. In practice, this means Python's float has the same precision as the double type in languages such as C++ and Java.
String
name = "John"
If we check its type, Python returns: <class 'str'>. What does it mean? Unlike languages such as Java or C++, Python does not have a separate char type; instead, a single character is simply a string with a length of one.
Boolean
is_logged_in = True
If we check its type, Python returns: <class 'bool'>. What does it mean? A boolean can have one of two values: True or False. Booleans are commonly used in conditions and comparisons.
Now that we’ve learned about the most common built-in data types in Python, let’s see some operations that we can use to manipulate those data types. Python provides several functions for working with data types:
- type(): allows us to check the type of any object; in the example below, we check the type of age, the first variable that we defined:
- type(age) => <class 'int'>
- float(): with this function, we can convert numeric values to floats:
- float(age) => float(30) => 30.0
- int(): with this function, we can convert numeric values to integers:
- int(height) => int(1.73) => 1
- str(): with this function, we can convert values to strings:
- str(height) => str(1.73) => "1.73"
So, when we assign a value to a variable, Python creates an object of the appropriate type and stores a reference to that object in the variable. This reinforces one of Python's core concepts: everything is an object.
Now that you understand the most common built-in data types in Python, you're ready to learn how to combine them. In the next articles, we'll explore variables in more detail and introduce data structures such as lists, tuples, dictionaries, and sets, which allow us to organize and manipulate collections of data efficiently.