Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
90 views

Arrays Programs Python

Uploaded by

ekta sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Arrays Programs Python

Uploaded by

ekta sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programs Using Arrays

Introduction to Arrays in Python:

An array is a collection of items stored at contiguous memory locations. Python does not have a

native array

data structure but provides arrays through libraries like 'array' and 'numpy'. Arrays are useful for

storing

multiple items of the same data type.

Examples of Array Programs:

1. Program to Create and Display an Array:

------------------------------------------

from array import array

# Create an array

arr = array('i', [1, 2, 3, 4, 5])

# Display the array

print("Array elements:", arr)

2. Program to Append Elements to an Array:

------------------------------------------

from array import array

# Create an array
arr = array('i', [1, 2, 3])

# Append elements

arr.append(4)

arr.append(5)

# Display updated array

print("Updated array:", arr)

3. Program to Insert an Element at a Specific Position:

-------------------------------------------------------

from array import array

# Create an array

arr = array('i', [1, 3, 4, 5])

# Insert 2 at index 1

arr.insert(1, 2)

# Display updated array

print("Array after insertion:", arr)

4. Program to Remove an Element from an Array:

----------------------------------------------

from array import array

# Create an array
arr = array('i', [1, 2, 3, 4, 5])

# Remove the element '3'

arr.remove(3)

# Display updated array

print("Array after removal:", arr)

5. Program to Traverse an Array:

--------------------------------

from array import array

# Create an array

arr = array('i', [10, 20, 30, 40, 50])

# Traverse and display each element

for element in arr:

print("Element:", element)

6. Program to Find the Maximum Element in an Array:

---------------------------------------------------

from array import array

# Create an array

arr = array('i', [5, 12, 7, 10, 15])

# Find the maximum element


max_value = max(arr)

print("Maximum value in the array:", max_value)

7. Program to Reverse an Array:

-------------------------------

from array import array

# Create an array

arr = array('i', [1, 2, 3, 4, 5])

# Reverse the array

arr.reverse()

print("Reversed array:", arr)

Conclusion:

Arrays are a basic data structure that helps manage and manipulate collections of data effectively.

Understanding their usage through Python programs provides a strong foundation for more

advanced concepts.

You might also like