In programming, the data type of a variable defines what kind of value it can hold and what operations can be performed on it. Python, being a dynamically-typed language, automatically assigns a data type to variables at runtime based on their value. However, understanding Python’s data types is crucial for writing efficient, error-free code and making the most of Python’s capabilities.
Python offers a rich set of data types that cater to various needs, from handling simple text to managing complex structures. These data types include:
str
for storing strings.int
, float
, and complex
for numeric computations.list
, tuple
, and range
for managing ordered collections of data.dict
for key-value pair storage.set
and frozenset
for collections of unique elements.bool
for logical values (True
or False
).bytes
, bytearray
, and memoryview
for handling binary data.NoneType
for representing the absence of a value.It represents type of data passing to the computer.(it was a numeric , sequence, mapping, set, Boolean, binary)
Data types specify the size, range, and operations that can be performed on the variable.
Datatypes in python:
Text Type: | Str |
Numeric Types: | int, float, complex |
Sequence Types: | list, tuple, range |
Mapping Type: | Dict |
Set Types: | set, frozenset |
Boolean Type: | Bool |
Binary Types: | bytes, bytearray, memoryview |
None Type: | NoneType
|
Text type: its a type of data that is used to store the characters and group of characters
Str=’a’(this is for storing the characters)
Str=”thenmozhi”(this is storing the group of characters”); address = “””1234 Elm Street
Springfield
IL 62701″””
Numeric Types: | int, float, complex |
I=5;
f=12.22
num1 = 2 + 3j # A complex number with real part 2 and imaginary part 3
print(type(num1)) # Output: <class ‘complex’>
A list is a mutable (modifiable) sequence type that can store a collection of items, including mixed data types.
Insertion and deletion is very difficult in case of array because we have to shift all the elements to right.
# Empty list
empty_list = []
# List with integers
int_list = [1, 2, 3, 4]
# List with mixed data types
mixed_list = [1, “hello”, 3.14, True]
# Nested list
nested_list = [[3,4], [5,2], [6,3]]
Mylist=[1,2,3,4,5]
Print(Mylist[0])
Printf(Mylist[1])
Mylist[0]=12;
Mylist.append(50) # Add to the end
Mylist.insert(1, 15) # Insert at index 1
my_list.pop() # Removes the last element
my_list.remove(25)
del my_list[1]
print(my_list) # Output: [10, 30, 40]
slicing the element:
sub_list = my_list[1:3]
print(sub_list) # Output: [30, 40]
Useful list methods:
my_list = [3, 1, 4, 1, 5]
# Length of the list
print(len(my_list)) # Output: 5
Tuple:
my_tuple = (1, 2, 3)empty_tuple = ()single_element_tuple = (42,) # A comma is required for single-element tuples
coordinates = (10.5, 20.3)print(coordinates[0]) # Access first element
range(stop) # From 0 to stop-1range(start, stop) # From start to stop-1range(start, stop, step)
my_dict = {‘name’: ‘Alice’, ‘age’: 25, ‘city’: ‘New York’}print(my_dict[‘name’]) # Output: Alice
my_set = {1, 2, 3, 4}my_set.add(5) # Adds an elementprint(my_set) # Output: {1, 2, 3, 4, 5}
my_frozenset = frozenset([1, 2, 3])# my_frozenset.add(4) # This will raise an error because frozensets are immutableprint(my_frozenset) # Output: frozenset({1, 2, 3})
Indian Institute of Embedded Systems – IIES