In this tutorial, you will learn about different data types in Python, including integers, floats, strings, Booleans, lists, tuples, sets, dictionaries, and None. Each data type is explained with simple examples to help you understand how it works.
What Are Data Types in Python?
Data types define the type of value that a variable can store.
For example, a number, text, and a decimal value are different types of data. Python provides several built-in data types to work with different kinds of information.
Example:
name = "Rahul" age = 25 salary = 25000.50 is_student = True
Here, each variable stores a different type of value.
Types of Data Types in Python
Python provides several built-in data types. These can be grouped into different categories.
1. Numeric Data Types
Numeric data types are used to store numbers. Python provides three main numeric types.
Integer (int)
The integer data type stores whole numbers without decimal points.
age = 25 marks = 90 print(type(age))
Float (float)
The float data type stores numbers with decimal points.
price = 99.50 temperature = 36.5 print(type(price))
Complex (complex)
The complex data type stores numbers containing real and imaginary parts.
number = 3 + 5j print(type(number))
2. String Data Type
A string is used to store text or a sequence of characters. Strings can be created using single quotes, double quotes, or triple quotes.
name = "Python" city = 'London' print(name) print(city)
You can combine strings using the + operator.
first_name = "Shouvik"
last_name = "Banik"
print(first_name + " " + last_name)3. Boolean Data Type
The Boolean data type represents only two values: True and False.
It is commonly used in conditional statements and logical operations.
Example:
is_active = True is_completed = False print(type(is_active))
4. List Data Type
A list is used to store multiple values in a single variable. Lists are ordered, changeable, and allow duplicate values.
Example:
fruits = ["Apple", "Banana", "Mango"] print(fruits) print(fruits[0])
You can modify list values.
fruits[1] = "Orange"
print(fruits)5. Tuple Data Type
A tuple is similar to a list, but it cannot be modified after creation. Tuples are ordered and allow duplicate values.
Example:
colors = ("Red", "Green", "Blue")
print(colors)
print(colors[0])6. Set Data Type
A set stores unique values and does not maintain a guaranteed element order. Sets do not allow duplicate elements.
Example:
numbers = {10, 20, 30, 10}
print(numbers)Output:
{10, 20, 30}
The duplicate value 10 is automatically removed.
7. Dictionary Data Type
A dictionary stores data in key-value pairs. Dictionaries are useful when you want to associate information with specific keys.
Example:
student = {
"name": "Amit",
"age": 21,
"city": "Kolkata"
}
print(student["name"])
print(student["age"])Output:
Amit
21
8. None Data Type
Python provides a special value called None, which represents the absence of a value.
Example:
result = None print(result) print(type(result))
Checking Data Types in Python
You can use the built-in type() function to identify the data type of a variable.
Example:
a = 100 b = 10.5 c = "Hello" d = True print(type(a)) print(type(b)) print(type(c)) print(type(d))
Type Conversion in Python
Type conversion means changing a value from one data type to another. Python provides built-in functions to convert values between compatible data types.
Example:
age = "25" age = int(age) print(age) print(type(age))
Common Type Conversion Functions
int()– Converts a compatible value to an integer.float()– Converts a compatible value to a float.str()– Converts a value to a string.list()– Creates a list.tuple()– Creates a tuple.set()– Creates a set.
Conclusion
Data types are an important part of Python programming. They define how data is stored and what operations can be performed on it.
Understanding integers, floats, strings, Booleans, lists, tuples, sets, and dictionaries will help you work with different types of information and build Python applications more effectively.
If you are learning Python, practice using each data type with simple programs. This will help you build a strong foundation in Python programming.
