In Python, a data type tells us what kind of value a variable contains. Python has several built-in data types such as numbers, strings, lists, tuples, sets, dictionaries and Boolean values.
A data type defines the kind of data stored in a variable. For example:
name = "Rahul"
age = 20
price = 99.50
is_student = True
Here, each variable contains a different type of data.
Python provides several built-in data types. The commonly used types are:
The str data type is used to store text. Strings can be written using single or double quotation marks.
name = "Rahul"
city = 'Patna'
print(name)
print(city)
The int data type is used to store whole numbers without decimal points.
age = 25
marks = 90
number = -10
print(age)
print(marks)
print(number)
The float data type is used to store numbers containing decimal values.
price = 99.50
height = 5.8
temperature = -2.5
print(price)
print(height)
print(temperature)
The complex data type is used to represent complex numbers. A complex number contains a real part and an imaginary part.
number = 3 + 4j
print(number)
In Python, j represents the imaginary part of a complex number.
The bool data type represents one of two values: True or False.
is_student = True
is_teacher = False
print(is_student)
print(is_teacher)
A list is used to store multiple values in a single variable. Lists are written using square brackets [ ].
fruits = ["Apple", "Banana", "Mango"]
print(fruits)
Lists can contain different types of values.
data = ["Rahul", 20, 85.5, True]
print(data)
A tuple is similar to a list, but tuples are immutable. Tuples are usually written using parentheses ( ).
colors = ("Red", "Green", "Blue")
print(colors)
A set is an unordered collection of unique values. Sets are written using curly brackets { }.
numbers = {10, 20, 30, 40}
print(numbers)
A set automatically removes duplicate values.
numbers = {10, 20, 20, 30}
print(numbers)
A dictionary stores data in key-value pairs. Dictionaries are written using curly brackets.
student = {
"name": "Rahul",
"age": 20,
"marks": 85
}
print(student)
The None value represents the absence of a value. Its data type is called NoneType.
result = None
print(result)
print(type(result))
The type() function is used to determine the data type of a value or variable.
name = "Rahul"
age = 20
price = 99.50
print(type(name))
print(type(age))
print(type(price))
name = "Amit"
age = 21
height = 5.7
student = True
marks = [80, 85, 90]
print(type(name))
print(type(age))
print(type(height))
print(type(student))
print(type(marks))
A variable can be assigned a value of a different data type later.
x = 10
print(type(x))
x = "Hello"
print(type(x))
This is possible because Python uses dynamic typing.
Some Python data types are used to store collections of values. Common sequence types include:
name = "Python"
numbers = [10, 20, 30]
values = (1, 2, 3)
Python data types can also be understood as mutable or immutable.
Mutable objects can be changed after they are created. For example, lists are mutable.
numbers = [10, 20, 30]
numbers[0] = 100
print(numbers)
Tuples and strings, on the other hand, are immutable.
name = "Rahul"
age = 22
salary = 25000.50
is_employee = True
skills = ["Python", "SQL", "HTML"]
address = ("Patna", "Bihar")
print("Name:", name)
print("Age:", age)
print("Salary:", salary)
print("Employee:", is_employee)
print("Skills:", skills)
print("Address:", address)
Question: Which data type is used to store a whole number in Python?