What are Data Types?
Data types define the type of data that a variable can store.
Python provides different built-in data types to store numbers, text, collections, and logical values.
Python is a dynamically typed language, so there is no need to declare the data type explicitly. The data type is automatically determined based on the assigned value.
Types of Data Types in Python
Python has several built-in data types.
- Numeric Data Types
- String Data Type
- Boolean Data Type
- Sequence Data Types
- Set Data Type
- Dictionary Data Type
- None Data Type
1. Numeric Data Types
Numeric data types are used to store numerical values.
Python provides three main numeric data types.
a) int (Integer)
The int data type is used to store whole numbers without decimal points.
Example:
age = 24
salary = 25000
print(age)
print(salary)
print(type(age))
Output:
24
25000
<class 'int'>
b) float
The float data type is used to store numbers with decimal points.
Example:
price = 99.50
percentage = 85.5
print(price)
print(percentage)
print(type(price))
Output:
99.5
85.5
<class 'float'>
c) complex
The complex data type is used to store complex numbers. It contains a real part and an imaginary part.
Example:
number = 5 + 3j
print(number)
print(type(number))
Output:
(5+3j)
<class 'complex'>
2. String Data Type (str)
A string is a collection of characters enclosed within single quotes, double quotes, or triple quotes.
Strings are used to store text values.
Example:
name = "Mahalakshmi"
language = 'Python'
print(name)
print(language)
print(type(name))
Output:
Mahalakshmi
Python
<class 'str'>
3. Boolean Data Type (bool)
The Boolean data type represents only two values: True and False.
It is mainly used in conditional statements and logical operations.
Example:
is_student = True
is_working = False
print(is_student)
print(is_working)
print(type(is_student))
Output:
True
False
<class 'bool'>
4. Sequence Data Types
Sequence data types are used to store multiple values in an ordered collection.
Python provides three important sequence data types.
a) List
- A list is an ordered and mutable collection of elements.
- It allows duplicate values and can store different data types.
Example:
fruits = ["Apple", "Mango", "Orange"]
print(fruits)
print(type(fruits))
Output:
['Apple', 'Mango', 'Orange']
<class 'list'>
b) Tuple
A tuple is an ordered and immutable collection of elements. Once created, its elements cannot be changed.
Example:
numbers = (10, 20, 30)
print(numbers)
print(type(numbers))
Output:
(10, 20, 30)
<class 'tuple'>
c) Range
The range data type represents a sequence of numbers. It is commonly used in loops.
Example:
numbers = range(1, 6)
print(list(numbers))
print(type(numbers))
Output:
[1, 2, 3, 4, 5]
<class 'range'>
5. Set Data Type
- A set is an unordered collection of unique elements.
- It does not allow duplicate values.
Example:
numbers = {10, 20, 30, 10}
print(numbers)
print(type(numbers))
Output:
{10, 20, 30}
<class 'set'>
6. Dictionary Data Type (dict)
- A dictionary is used to store data in key-value pairs.
- Each key is unique, and values can be of different data types.
Example:
student = {
"name": "John",
"age": 22,
"course": "Python"
}
print(student)
print(student["name"])
print(type(student))
Output:
{'name': 'John', 'age': 22, 'course': 'Python'}
John
<class 'dict'>
7. None Data Type
The None data type represents the absence of a value or a null value.
Example:
result = None
print(result)
print(type(result))
Output:
None
<class 'NoneType'>
Top comments (0)