Understanding Data Types in Python


 


In the previous blog, we explored variables in Python. We learned that variables are simply names given to data and act as references to the data stored in memory. Now, let's move forward and understand data types in Python.

What Are Data Types?

Data types define the kind of values that variables can store. If you have experience with other programming languages like C++ or Java, you may be familiar with data types like:

  • int (integer)

  • float (decimal numbers)

  • char (character)

  • boolean (true/false values)

However, Python handles data types differently compared to these languages.

Python is a Dynamically Typed Language

One of the key features of Python is that it is dynamically typed. This means you don’t need to declare the data type of a variable explicitly. Instead, the type is determined based on the assigned value.

Example:

x = 25  # Integer value
print(type(x))  # Output: <class 'int'>

x = 13.75  # Floating-point value
print(type(x))  # Output: <class 'float'>

x = "A"  # String value
print(type(x))  # Output: <class 'str'>

Observations:

  • The variable x changes its type dynamically when we assign different values to it.

  • Python automatically detects the type of data and assigns it accordingly.

  • Unlike C++ or Java, you do not need to specify the data type before using a variable.

Comparison with C++ and Java:

In C++/Java, you must declare the type explicitly:

int x = 25;
float x = 13.75; // Error: Cannot reassign float to an int variable

However, in Python, the type can change dynamically based on the value assigned.

Checking Data Types in Python

Python provides a built-in function type() to check the data type of a variable.

Example:

x = 100
print(type(x))  # Output: <class 'int'>

x = 99.99
print(type(x))  # Output: <class 'float'>

Everything in Python is an Object

Another crucial point is that everything in Python is an object. Even data types like integers, floats, and strings belong to specific classes.

Example:

x = 25
print(type(x))  # Output: <class 'int'>

Here, 25 is an object of the int class.

More Examples with Different Data Types

Python supports multiple data types. Here are some common ones:

Integer (int)

x = 10
print(type(x))  # Output: <class 'int'>

Floating Point (float)

x = 12.5
print(type(x))  # Output: <class 'float'>

String (str)

x = "Hello, Python!"
print(type(x))  # Output: <class 'str'>

List

A list is a collection of multiple values:

x = [1, 2, 3, 4, 5]
print(type(x))  # Output: <class 'list'>

Lists allow storing multiple values inside a single variable.

Multiple Variable Assignments

Python allows multiple assignments in a single line.

Example:

a, b, c = 5, 10, 15
print(a, b, c)  # Output: 5 10 15

You can also assign the same value to multiple variables:

x = y = z = 1
print(x, y, z)  # Output: 1 1 1

Summary

  1. Python is dynamically typed (you don’t need to declare data types explicitly).

  2. The type of a variable depends on the assigned value.

  3. You can check the type of a variable using the type() function.

  4. Everything in Python is an object.

  5. Python supports multiple assignments in a single statement.

What’s Next?

In the next blog, we will explore more complex data types like tuples, dictionaries, and sets in Python. Until then, try experimenting with variables and data types in Python's IDLE or any Python environment.

Happy Coding! 🚀