Skip to main content

Python Data Types

Introduction​

Before writing meaningful programs, it is essential to understand how Python handles different kinds of data and how variables store that data internally.

In this tutorial, we will explore:

  • What data types are
  • Static vs dynamic typing
  • Built-in data types in Python
  • Everything as an object
  • Important built-in functions (type(), id(), print())

What is a Data Type?​

A data type represents the type of data that is stored in a variable or used in a program.

For example:

  • 10 → Integer (int)
  • 10.5 → Float (float)
  • True → Boolean (bool)

Each value belongs to a specific category, and that category is called its data type.

Dynamic Typing​

Python is a dynamically typed programming language, meaning the type of a variable is determined at runtime based on the assigned value.

In Python, there is no need to declare types explicitly. Python automatically determines the type.

a = 10
b = 10.5
c = True

Built-in Data Types in Python​

Python provides a rich set of built-in data types.

Let us look at the major categories.

1. Numeric Types​

  • int → Integer values
  • float → Decimal values
  • complex → Complex numbers
a = 10 # int
b = 10.5 # float
c = 3 + 4j # complex

2. Boolean Type​

bool → Represents True or False

flag = True

3. String Type​

str → Sequence of characters

name = "Python"

4. Sequence Types​

  • list → Mutable collection
  • tuple → Immutable collection
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)

5. Set Types​

  • set → Unordered unique elements
  • frozenset → Immutable set

6. Mapping Type​

  • dict → Key-value pairs
data = {"name": "Samir", "age": 30}

7. Binary Types​

  • bytes
  • bytearray

8. Range Type​

range → Sequence of numbers

r = range(5)

9. None Type​

None → Represents absence of value

Everything in Python is an Object​

This is a very important concept.

In Python, everything is treated as an object.

a = 10
  • 10 is not just a number
  • It is an object of type int
  • a is a reference pointing to that object

Unlike some languages that differentiate between primitive and object types, Python treats everything uniformly as an object.

Important Built-in Functions​

1. type() – To Check Data Type​

a = 10
print(type(a)) # <class 'int'>

2. id() – To Get Memory Address​

a = 10
print(id(a)) # 2275190964752

Returns the memory address of the object

3. print() – To Display Value​

a = 10
print(a)