Table of contents | |
Introduction | |
Creating Arrays | |
Accessing and Modifying Elements | |
Array Operations | |
Sample Problems |
An array is a data structure that stores a collection of elements, where each element is identified by its index. Arrays are useful for efficiently storing and accessing a large number of elements.
In Python, arrays are implemented using lists. Lists can store elements of different data types and can grow or shrink dynamically.
We can create an array in Python by defining a list of elements. Let's see an example:
# Creating an array
my_array = [1, 2, 3, 4, 5]
# Printing the array
print(my_array)
Output:
[1, 2, 3, 4, 5]
Explanation:
We can access individual elements of an array using their index. The index starts from 0 for the first element and increments by 1 for each subsequent element. Let's see an example:
# Accessing elements
my_array = [1, 2, 3, 4, 5]
print("Element at index 0:", my_array[0])
print("Element at index 2:", my_array[2])
Output:
Element at index 0: 1
Element at index 2: 3
Explanation:
To modify the value of an element, we can assign a new value to the corresponding index:
# Modifying elements
my_array = [1, 2, 3, 4, 5]
my_array[1] = 10
print("Modified array:", my_array)
Output:
Modified array: [1, 10, 3, 4, 5]
Explanation:
Arrays support various operations in Python. Here are a few common ones:
my_array = [1, 2, 3, 4, 5]
length = len(my_array)
print("Length of the array:", length)
Output:
Length of the array: 5
my_array = [1, 2, 3]
my_array.append(4)
print("Updated array:", my_array)
Output:
Updated array: [1, 2, 3, 4]
my_array = [1, 2, 3, 4]
my_array.remove(2)
print("Updated array:", my_array)
Output:
Updated array: [1, 3, 4]
my_array = [1, 2, 3, 4, 5]
if 3 in my_array:
print("3 exists in the array")
else:
print("3 does not exist in the array")
Output:
3 exists in the array
Let's solve some sample problems to apply our knowledge of arrays in Python.
Problem 1: Find the Maximum Element in an Array
Write a function named find_maximum() that takes an array as an argument and returns the maximum element.
def find_maximum(arr):
max_element = max(arr)
return max_element
# Calling the find_maximum function
my_array = [10, 5, 20, 8, 15]
max_num = find_maximum(my_array)
print("The maximum element is:", max_num)
Output:
The maximum element is: 20
Problem 2: Reverse an Array
Write a function named reverse_array() that takes an array as an argument and returns the reversed array.
def reverse_array(arr):
reversed_arr = arr[::-1]
return reversed_arr
# Calling the reverse_array function
my_array = [1, 2, 3, 4, 5]
reversed_array = reverse_array(my_array)
print("Original array:", my_array)
print("Reversed array:", reversed_array)
Output:
Original array: [1, 2, 3, 4, 5]
Reversed array: [5, 4, 3, 2, 1]
Problem 3: Check if an Array is Sorted
Write a function named 'is_sorted()' that takes an array as an argument and returns 'True' if the array is sorted in non-decreasing order, and 'False' otherwise.
def is_sorted(arr):
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
return False
return True
# Calling the is_sorted function
my_array = [1, 3, 5, 4, 6]
if is_sorted(my_array):
print("The array is sorted")
else:
print("The array is not sorted")
Output:
The array is not sorted
Arrays are versatile data structures that allow us to store and manipulate collections of elements efficiently. In Python, arrays are implemented using lists, which provide various operations for accessing, modifying, and manipulating elements. In this article, we learned how to create arrays, access and modify their elements, perform common array operations, and solve sample problems using arrays. By practicing and applying these concepts, we can leverage the power of arrays to solve real-world programming challenges.
49 videos|38 docs|18 tests
|
|
Explore Courses for Software Development exam
|