A data type defines the kind of value that a variable can store.
Python provides several built-in data types that are commonly used when developing applications.
The most important beginner-level data types are:
- Integer (
int) - Float (
float) - String (
str) - List (
list) - Tuple (
tuple) - Dictionary (
dict) - Boolean (
bool) - Set (
set)
Integer (int)
An integer represents a whole number without a decimal point.
Examples
12
20
-20
0
-5
Example in Python
age = 20
marks = 85
print(age)
print(marks)Float (float)
A float represents a number containing a decimal point.
It is commonly used for values such as prices, measurements, percentages, and calculations.
Examples
12.5
5.6
3.445
-6.7
Example
price = 99.50
temperature = 25.5
print(price)
print(temperature)String (str)
A string is a sequence of characters enclosed inside single (' ') or double (" ") quotation marks.
Strings can contain letters, numbers, spaces, and special characters.
Examples
"abc"
"endpointtech"
"12"
'Hello World'
Although "12" contains numbers, it is a string because it is enclosed in quotation marks.
Example
first_name = "Ram"
website = "skillmantra"
phone = "9841234567"
print(first_name)
print(website)
print(phone)List (list)
A list is a collection of multiple elements.
Lists can contain different types of data and are written using square brackets [].
Example
students = ["Ram", "Shyam", "Hari"]A list can also contain different data types:
data = ["a", "b", 12, 20.5]Lists are Mutable
Mutable means that we can modify the contents of a list after it has been created.
For example:
fruits = ["Apple", "Banana", "Mango"]
fruits.append("Orange")
print(fruits)Output:
['Apple', 'Banana', 'Mango', 'Orange']Tuple (tuple)
A tuple is also a collection of elements, but unlike a list, a tuple is immutable.
Immutable means that its elements cannot be changed after the tuple has been created.
Tuples are written using parentheses ().
Example
data = (12, 15, "a", "b", "apple")
print(data)Tuple Example
colors = ("Red", "Blue", "Green")
print(colors)Trying to change an element will result in an error:
colors[0] = "Yellow"Therefore, use a tuple when you want a collection of values that should not be modified.
Dictionary (dict)
A dictionary stores data in key-value pairs.
Dictionaries are written using curly brackets {}.
Example
student = {
"firstName": "Ram",
"lastName": "Sharma",
"age": 20
}Here:
firstNameis a key.Ramis its value.lastNameis a key.Sharmais its value.ageis a key.20is its value.
We can access a value using its key:
print(student["firstName"])
print(student["age"])Output:
Ram
20Boolean (bool)
A Boolean represents one of two possible values:
True
FalseBoolean values are commonly used in conditions and decision-making.
Example
is_logged_in = True
is_admin = False
print(is_logged_in)
print(is_admin)Boolean values are especially useful with if statements:
is_logged_in = True
if is_logged_in:
print("Welcome to the dashboard")Set (set)
A set is an unordered collection of unique elements.
Sets are written using curly brackets {}.
Example
numbers = {1, 2, 5, 3}
print(numbers)A major feature of a set is that it does not store duplicate values.
Example
numbers = {1, 2, 2, 3, 3, 4}
print(numbers)The duplicate values are automatically removed.
The resulting set contains only unique values:
{1, 2, 3, 4}Quick Comparison
| Data Type | Example | Mutable? | Description |
|---|---|---|---|
int | 20 | No | Whole number |
float | 20.5 | No | Decimal number |
str | "Hello" | No | Text/characters |
list | [1, 2, 3] | Yes | Ordered collection |
tuple | (1, 2, 3) | No | Immutable collection |
dict | {"name": "Ram"} | Yes | Key-value collection |
bool | True | No | True or False |
set | {1, 2, 3} | Yes | Unique unordered collection |