Python: Arrays

# Python: Arrays - Basics of Python - Software Development

 Table of contents Introduction Creating Arrays Accessing and Modifying Elements Array Operations Sample Problems ## Introduction

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.

## Creating Arrays

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 define an array called 'my_array' that contains the elements 1, 2, 3, 4, and 5.
• We use the 'print()' function to display the contents of the array.

## Accessing and Modifying Elements

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)

print("Element at index 2:", my_array)

Output:

Element at index 0: 1

Element at index 2: 3

Explanation:

• We access the elements of the array 'my_array' using their index inside square brackets.
• The first 'print()' statement retrieves and displays the element at index 0 (which is 1).
• The second 'print()' statement retrieves and displays the element at index 2 (which is 3).

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 = 10

print("Modified array:", my_array)

Output:

Modified array: [1, 10, 3, 4, 5]

Explanation:

• We modify the value of the element at index 1 from 2 to 10.
• The modified array is then printed using the 'print()' function.

## Array Operations

Arrays support various operations in Python. Here are a few common ones:

• Finding the length of an array using the 'len()' function:

my_array = [1, 2, 3, 4, 5]

length = len(my_array)

print("Length of the array:", length)

Output:

Length of the array: 5

• Adding elements to an array using the 'append()' method:

my_array = [1, 2, 3]

my_array.append(4)

print("Updated array:", my_array)

Output:

Updated array: [1, 2, 3, 4]

• Removing elements from an array using the remove() method:

my_array = [1, 2, 3, 4]

my_array.remove(2)

print("Updated array:", my_array)

Output:

Updated array: [1, 3, 4]

• Checking if an element exists in an array using the 'in' keyword:

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

## Sample Problems

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

### Conclusion

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.

The document Python: Arrays | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development

## Basics of Python

49 videos|38 docs|18 tests

## Basics of Python

49 videos|38 docs|18 tests Explore Courses for Software Development exam Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;