Data Types In Python
Data Type is basically the type of data a variable holds. For example if the variable store a number then the variable data type will be a integer.
Python has many built-in data types, Following is the list of all data types in python:
Data Type | Example | Description |
int | 30 | Integer (whole number) |
float | 3.15 | Floating-point number (decimal) |
str | “Hello” | String (sequence of characters) |
bool | True | Boolean (True or False) |
list | [1, “apple”, True] | List (ordered collection) |
tuple | (2, “banana”, False) | Tuple (immutable ordered list) |
dict | {“name”: “Alice”, 28} | Dictionary (key-value pairs) |
set | {1, 2, 3} | Set (unordered collection of unique elements) |
NoneType | None | Represents absence of a value |
Now let’s see each data type one by one.
1. Integer
age = 25
print(age)
Integer type represents whole numbers.
2. Float
size = 2.5
print(size)
Float type represents decimal or floating-point numbers
3. String
name = "John Doe"
print(name)
String type represents sequences of characters enclosed in single or double quotes
4. Boolean
is_adult = True
is_teen = False
print(is_adult)
print(is_teen)
Boolean type represents either True or False.
5. List
grades = [90, 85, 92, 88]
print(grades)
A list is an ordered collection that can contain elements of different data types.
6. Tuple
coordinates = (3, 5)
print(coordinates)
Similar to a list but immutable (cannot be modified after creation).
7. Dictionary
person = {"name": "Alice", "age": 28, "is_student": False}
print(person)
Dictionary is an unordered collection of key-value pairs.
8. Set
unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers)
Unordered collection of unique elements.
9. None
no_value = None
print(no_value)
Represents the absence of a value or a null value.
Checking Data Type Of A Variable
score = 90
name = "Jason"
is_injured = False
print(type(score))
print(type(name))
print(type(is_injured))
In python we can check the data type of a variable using the type() function, In the function you need to pass the variable you want to check and the pass the type() function in print() function to print the data type.
So this was everything you needed to know about variable data types in python.