Data Types
A data type simply tells us what kind of data a variable is designed to hold. When we create variables in our program, the type of data we assign to it determines the variable’s type.
- If we write a = 10, it means the variable a carries numeric or integer data (a whole number).
- If we assign f = 10.23, then f holds floating-point or decimal data.
- If we assign a set of characters within double quotes to a variable called s (e.g., s = “Hello”), then s is of type string—it holds a sequence of characters.
In Python, the data types are primarily organized into five main categories:
| Category | Description | Examples |
|---|---|---|
| 1. None Type | This is an object that does not contain any value. | None |
| 2. Numeric Types | Used for working with numbers. | integer, floating point (decimals), complex |
| 3. Sequence Types | These hold an ordered sequence of other data types. | String (sequence of characters), bytes, bytearray, list, tuple, range |
| 4. Set Types | These are collections that do not allow duplicate values. | set, frozenset |
| 5. Mapping Type | Used for storing data in key-value pairs (like a dictionary). | dict (dictionary) |
- int: Integer data type represents whole numbers without a fractional part.
- float: Floating-point data type represents real numbers with a fractional part.
- list: List is an ordered, mutable collection of items that can contain duplicates.
- tuple: Tuple is an ordered, immutable collection of items that can contain duplicates.
- range: Range represents an immutable sequence of numbers, commonly used for looping a specific number of times.
- str: String is a sequence of characters used to represent text.
- set: Set is an unordered collection of unique items.
- frozenset: Frozenset is an immutable version of a set, which means its elements cannot be changed after creation.
- dict: Dictionary is an unordered collection of key-value pairs, where each key is unique.
- bool: Boolean data type represents one of two values: True or False.
Numeric Types Examples
a=13 b=100 c=1234 print(a,b,c) #printing values print(type(a),type(b),type(c)) Â #int x=23.54 y=0.001 z=3.14159 print(x,y,z) #printing values print(type(x),type(y),type(z)) #float # Output: # 13 100 1234 # <class 'int'> <class 'int'> <class 'int'> # 23.54 0.001 3.14159 # <class 'float'> <class 'float'> <class 'float'>
lst=[1,2,3,4,5] #list
tup=(10,20,30,40,50) #tuple
rng=range(1,11) #range
st="Hello, World!" #string
print(lst) #printing list
print(tup) #printing tuple
print(rng) #printing range
print(st) #printing string
print(type(lst)) #list
print(type(tup)) #tuple
print(type(rng)) #range
print(type(st)) #string
# Output:
# [1, 2, 3, 4, 5]
# (10, 20, 30, 40, 50)
# range(1, 11)
# Hello, World!
# <class 'list'>
# <class 'tuple'>
# <class 'range'>
# <class 'str'>
st_set={1,2,3,4,5} #set
frz_set=frozenset([10,20,30,40,50]) #frozenset
print(st_set) #printing set
print(frz_set) #printing frozenset
print(type(st_set)) #set
print(type(frz_set)) #frozenset
# Output:
# {1, 2, 3, 4, 5}
# frozenset({10, 20, 30, 40, 50})
# <class 'set'>
# <class 'frozenset'>
dct={"a":1,"b":2,"c":3} #dictionary
bl_true=True #boolean true
bl_false=False #boolean false
print(dct) #printing dictionary
print(bl_true) #printing boolean true
print(bl_false) #printing boolean false
print(type(dct)) #dictionary
print(type(bl_true)) #boolean
print(type(bl_false)) #boolean
# Output:
# {'a': 1, 'b': 2, 'c': 3}
# True
# False
# <class 'dict'>
# <class 'bool'>
| Immutable (Cannot be changed) | Mutable (Can be changed) |
|---|---|
int, float, complex, bool |
list |
str, bytes |
bytearray |
tuple, range |
set |
frozenset |
dict |
