In Python, arrays are collections that store multiple items of the same/different data type depending on the array type used in Python.
Features
Unlike C or Java, Python has no built-in array data type. Instead, Python provides alternatives like lists and the array module for arrays.
Types of Array in Python
(A) Array Module in Python
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).
In Python, the array module does not support storing strings directly because it is designed for storing numeric values or characters (single-character Unicode).
For creating 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
double
Common Operations with Arrays
There are the following types of common operations in the Array Module –
print(list1[1:4]) # [20, 30, 40](B) Array Module in Python
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 –
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
Example : A Function program in Python to show Default Parameter. Example : A Function program in Python to show the Addition result of two user-accepted values. Example : A Function program in Python to Read more…
Definition of Function in Python A function is a block of reusable code that performs a specific task. Functions are essential for writing efficient, modular, and maintainable Python code. They make programs more structured, easier Read more…
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…
0 Comments