Array Module is a basic array that is more memory efficient than lists but requires homogeneous data.
If we need an actual array in Python, the array module provides an array object that only stores elements of the same data type (e.g., integers, floats).
For importing the array Module, we use – import array.
For Examples –
import array
# To create an array of integers
array1 = array.array(‘i’, [1, 2, 3, 4, 5])
print(array1[0]) # Output: 1
Features of array Module
Arrays are more efficient than lists when working with large collections of the same data type.
It requires specifying the type of the array elements using type codes (e.g., ‘i’for integers, ‘f’for floats).
Common Type Codes in Array
Type Code
C Type
Python Type
'b'
signed char
int
'B'
unsigned char
int
'i'
signed int
int
'f'
float
float
'd'
double
float
Common Operations with Arrays
There are the following types of common operations in the Array Module –
Reshaping arrays: We can convert 1D into 2D as needed using NumPy Arrays.
array2 = arr.reshape(5, 1) # Convert 1D array into 2D array with 5 rows and 1 column
Matrix multiplication using NumPy Array
import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
result = np.dot(matrix1, matrix2)
print(result) # Output: [[19 22] [43 50]]
(D) Tuple in Python
Definition
Tuples in Python are immutable(not change), ordered collections of data items of the same/different type used when the user wants to group related data that should not be modified.
Characteristics
They are Indexed, i.e. Like lists, tuple elements are accessed using indices starting from 0.
They are similar to lists, but unlike lists, tuples cannot be modified after they are created.
They are faster in operations((because of immutability) than Lists.
They are mainly used when we don’t want data to change in an application.
They offer efficient access to data and are often used in cases where immutability is essential, such as returning multiple values from functions or using them as dictionary keys.
Advantages
Since Tuple is the ordered collection of items hence maintains the order of elements.
They are Heterogeneous i.e., Tuples can store elements of different data types (e.g., integers, strings, floats) as needed.
Tuples are often used to group related data, and their immutability can be useful when we want to ensure that certain data remains unchanged throughout a program.
Disadvantages
They are immutable i.e., once a tuple is created, its contents cannot be changed (no adding, removing, or modifying elements). It may be advantageous in a few cases also.
To Create Tuples
We can create a tuple by enclosing values in parentheses `()` and separating them with commas.
For example –
x = (10, 20, 30, 40, 50) # Tuple with same data types.
y= (10, “Hello India”, 3.141, True) # Tuple with different data types/mix tuple/hybrid tuple.
z= (50,) # Single element tuple (requires a trailing comma), without the comma, it’s just an integer.
k = () #Empty tuple
Accessing Tuple Elements
We can access individual elements of a tuple using indexing, or retrieve multiple elements using slicing.
For Example:
x = (10, 20, 30, 40, 50, 60, 70, 80, 90)
# Accessing an element
print(x[0]) # Output: 10 (first element)
# Negative indexing
print(x[-1]) # Output: 90 (last element)
# Slicing a tuple
print(x[1:5]) # Output: (20, 30, 40, 50)
print(x[3:6]) # Output: (40, 50, 60)
Tuple is Immutable: Since tuples are immutable, we cannot change their content after creation. For example, trying to assign a new value to an index will raise an error. For example:-
x = (1, 2, 3)
x[0] = 10 # This will raise a TypeError: ‘tuple’ object does not support item assignment
Link for Control Flow Statements Programs in Python Control flow statements in Python allow us to manage the order in which our code is executed. Types of Control Flow Statements in Python They are of Read more…
In Python, operators are special symbols or keywords that perform operations on variables or values. They are used to carry out tasks like arithmetic calculations, comparisons, logical operations, and more. Types of Operators in Python Read more…
Variables in Python A variable is a container that reserves memory to hold values of a specific data type. The value in a variable may change during the life of the program-hence the name “variable”. Variable Read more…
0 Comments