Data Types
Data types define what kind of value a variable stores. Python provides several built-in data types that are used to work with numbers, text, collections, and logical values.
What is a Data Type?
A data type tells Python what kind of data a value represents and what operations can be performed on it.
For example, 25 is an integer, 25.5 is a floating-point number, and "Python" is a string.
Python Data Type Concept
25
int
Addition, comparison, conversion, etc.
Built-in Data Types in Python
Python has several built-in data types. Some of the most commonly used ones are:
| Data Type | Example | Description |
|---|---|---|
| int | 25 | Whole numbers |
| float | 25.5 | Decimal numbers |
| complex | 3 + 4j | Complex numbers |
| str | "Python" | Text |
| bool | True | True or False values |
| list | [10, 20, 30] | Ordered, changeable collection |
| tuple | (10, 20, 30) | Ordered, unchangeable collection |
| set | {10, 20, 30} | Unordered collection of unique values |
| dict | {"name": "Samadhan"} | Key-value collection |
| NoneType | None | Represents no value |
1. Integer (int)
The int data type is used to store whole numbers without decimal points.
age = 25
students = 50
marks = -10
print(age)
print(type(age))
Output
25
<class 'int'>
2. Float (float)
The float data type is used for numbers containing decimal values.
price = 499.99
percentage = 85.5
print(price)
print(type(price))
Output
499.99
<class 'float'>
3. Complex (complex)
Python supports complex numbers using the j notation for the imaginary part.
number = 3 + 4j
print(number)
print(type(number))
Output
(3+4j)
<class 'complex'>
4. String (str)
A string is a sequence of characters used to represent text. Strings can be written using single or double quotes.
name = "Samadhan"
course = 'Python'
print(name)
print(type(name))
Output
Samadhan
<class 'str'>
5. Boolean (bool)
The bool data type represents one of two values: True or False.
is_student = True
is_completed = False
print(is_student)
print(type(is_student))
Output
True
<class 'bool'>
6. List (list)
A list is an ordered and changeable collection. Lists can contain multiple values.
courses = ["Python", "C#", "Java"]
print(courses)
print(type(courses))
Output
['Python', 'C#', 'Java']
<class 'list'>
7. Tuple (tuple)
A tuple is an ordered collection that cannot be changed after it is created.
languages = ("Python", "Java", "C#")
print(languages)
print(type(languages))
Output:
('Python', 'Java', 'C#')
<class 'tuple'>
8. Set (set)
A set is a collection of unique values. Duplicate values are automatically removed.
numbers = {10, 20, 30, 20}
print(numbers)
print(type(numbers))
Output:
{10, 20, 30}
<class 'set'>
Notice that the duplicate value 20 is stored only once.
9. Dictionary (dict)
A dictionary stores data in key-value pairs.
student = {
"name": "Samadhan",
"course": "Python",
"institute": "CIIT Training Institute 🫠👀.."
}
print(student)
print(type(student))
Output:
{'name': 'Samadhan', 'course': 'Python', 'institute': 'CIIT Training Institute 🫠👀..'}
<class 'dict'>
10. NoneType
None represents the absence of a value. It is commonly used when a variable currently has no meaningful value.
result = None
print(result)
print(type(result))
Output
None
<class 'NoneType'>
Checking Data Type using type()
The type() function tells us the type of a value or variable.
name = "Samadhan"
age = 25
salary = 45000.50
is_active = True
print(type(name))
print(type(age))
print(type(salary))
print(type(is_active))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Data Type Conversion
Python provides functions such as int(), float(), and str() to convert values from one data type to another.
age = "25"
age_number = int(age)
print(age_number)
print(type(age_number))
Output:
25
<class 'int'>
Detailed type conversion will be covered in the next topic: Type Conversion.
CIIT Learning Point
Before performing an operation, understand the type of data you are working with. Using type() is a simple way to check the type of a variable while learning and debugging Python programs.
CIIT Practice Task
Create one variable for each of the following data types and print both its value and type:
- Integer
- Float
- String
- Boolean
- List
- Dictionary
age = 25
percentage = 85.5
name = "Samadhan"
is_student = True
courses = ["Python", "C#", "Java"]
student = {"name": "Samadhan", "age": 25}
print(age, type(age))
print(percentage, type(percentage))
print(name, type(name))
print(is_student, type(is_student))
print(courses, type(courses))
print(student, type(student))
Output:
25 <class 'int'>
85.5 <class 'float'>
Samadhan <class 'str'>
True <class 'bool'>
['Python', 'C#', 'Java'] <class 'list'>
{'name': 'Samadhan', 'age': 25} <class 'dict'>
Summary :
Python provides different data types such as int, float, complex, string, boolean, list, tuple, set, dictionary, and NoneType. Data types help Python understand what kind of value is being stored and how that value can be used in a program.