What Are Data Types in Python? See Examples

Data types in Python define the kind of data a variable can store. Every value in Python has a data type, and it tells Python how to store, process, and use that data in memory.

Since Python is a dynamically typed language, you do not need to declare data types explicitly. Python automatically assigns a data type based on the value.


Why Data Types Are Important

Data types help Python:

  • Decide how much memory to allocate
  • Perform valid operations on data
  • Prevent unexpected errors
  • Improve program readability and logic

Example:

x = 10
y = "10"

Although both look similar, x is an integer and y is a string.


Built-in Data Types in Python

Python provides several built-in data types, grouped into categories.


Numeric Data Types

Used to store numbers.

1. int (Integer)

Stores whole numbers.

age = 25
marks = 90

2. float

Stores decimal numbers.

price = 99.99
temperature = 36.5

3. complex

Stores complex numbers.

num = 3 + 5j

Text Data Type

str (String)

Used to store text or characters.

name = "Python"
message = 'Hello World'

Strings can be created using single or double quotes.


Boolean Data Type

bool

Stores only two values: True or False.

is_logged_in = True
is_admin = False

Used mainly in conditions and decision-making.


Sequence Data Types

1. list

Stores ordered, changeable collection of items.

languages = ["Python", "Java", "C"]

2. tuple

Stores ordered but unchangeable collection.

coordinates = (10, 20)

3. range

Represents a sequence of numbers.

numbers = range(1, 5)

Set Data Type

set

Stores unordered, unique values.

unique_numbers = {1, 2, 3, 4}

Duplicates are automatically removed.


Mapping Data Type

dict (Dictionary)

Stores data in key–value pairs.

student = {
    "name": "Aman",
    "age": 21,
    "course": "Python"
}

Dictionaries are widely used in real-world applications.


None Data Type

NoneType

Represents the absence of a value.

result = None

Often used as a placeholder.


Checking the Data Type

Use the type() function to identify the data type.

x = 100
print(type(x))

Output:

<class 'int'>

Type Conversion (Type Casting)

Python allows converting one data type into another.

x = "10"
y = int(x)

Common conversions:

  • int()
  • float()
  • str()
  • bool()

Conclusion

Data types are essential in Python because they define how data is stored and processed. Understanding data types helps you write correct logic, avoid errors, and build efficient programs.

Mastering data types is a key step before learning conditions, loops, and functions.


References

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *