What are Data Types?
In Python, data types define the kind of value a variable is storing. This helps Python understand how to handle and process that value in the program.
For example, numbers can be used in mathematical calculations, but text (called strings) cannot be directly added to numbers. So, using the correct data type is important to avoid errors and get the correct result. If the wrong data type is used, Python may give an error or an unexpected output.
Example:
student = "Ram" # String
grade = 12 # Integer
marks = 92.5 # Float
passed = True # Boolean
Main Built-in Data Types in Python:
1. int
(Integer)
Used to store whole numbers, like 1
, -50
, or
1000
. These
numbers do not have a decimal point. In Python 3, there is no limit on how big an integer can be
(only limited by memory).
Example:
age = 18
roll_no = 102
Value Range: No fixed limit in Python 3. You can store very large numbers.
2. float
(Floating-point)
Float is used to store decimal or fractional numbers. For example,
3.14
,
-0.5
, or 100.0
. These values are useful when exact precision is required
like in prices, marks, and measurements.
Example:
height = 5.6
price = 99.99
Value Range: Typically up to 15 decimal digits. Very small or very large float values may be shown in scientific notation.
3. str
(String)
A string stores text values. Anything written between single (' '
) or
double
(" "
) quotes is a string. Strings can contain letters, numbers, and special characters.
Example:
name = "Peehu"
city = 'Panipat'
You can perform many operations on strings like joining, slicing, and changing case.
4. bool
(Boolean)
This data type stores either True
or False
values. These
are often used in
decision-making, such as in if-else conditions or to check logical expressions.
Example:
passed = True
is_admin = False
5. list
A list stores multiple items in one variable. Lists are ordered, changeable (mutable), and allow duplicate values. Lists can also contain different types of data together.
Example:
students = ["Simmi", "Angel", "Vanshu"]
marks = [85, 90, 78]
6. tuple
Like a list, but a tuple is immutable, which means you cannot change, add, or remove items once it's created. Tuples are used when data should not be changed.
Example:
colors = ("red", "green", "blue")
7. dict
(Dictionary)
Used to store data in key-value pairs. The keys are unique and used to access the corresponding values. Very useful for storing structured data.
student = {
"name": "Yash",
"age": 20,
"grade": "A"
}
8. set
Used to store a collection of unique items. Sets are unordered and cannot have duplicate values.
Example:
fruits = {"apple", "banana", "cherry"}
How to Check Data Type
Python provides a built-in function type()
to check the data type of a variable. This
helps in debugging and verifying variable values.
name = "Devanshi"
print(type(name))
Output:
<class 'str'>
Why is Understanding Data Types Important?
- Helps Python understand how to treat the data.
- Makes it easier to apply correct functions and operations.
- Prevents programming errors and bugs.
- Helps in managing memory efficiently.
Quick Summary Table
Data Type | Example | Description |
---|---|---|
int | 10, -5 | Whole numbers |
float | 5.6, 99.99 | Decimal numbers |
str | "Hello" | Text data |
bool | True, False | True/False values |
list | [1,2,3] | Ordered collection, changeable |
tuple | (1,2,3) | Ordered, unchangeable |
dict | {'a':1} | Key-value pairs |
set | {1,2,3} | Unique items |