0% found this document useful (0 votes)
3 views

Python assignment 2

The document provides a series of Python code examples demonstrating various operations on arrays, including creation, printing, inserting, appending, removing elements, slicing, searching, updating, reversing, counting occurrences, extending, and converting arrays to lists. Each operation is accompanied by input code and expected output. The examples utilize the 'array' module in Python to illustrate these functionalities.

Uploaded by

Zaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python assignment 2

The document provides a series of Python code examples demonstrating various operations on arrays, including creation, printing, inserting, appending, removing elements, slicing, searching, updating, reversing, counting occurrences, extending, and converting arrays to lists. Each operation is accompanied by input code and expected output. The examples utilize the 'array' module in Python to illustrate these functionalities.

Uploaded by

Zaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT NO.

6
Q.1] Creating an array
Input:
import array
arr = array.array('i', [55, 56 ,57,58,59])
print(arr)

Output:
array('i', [55, 56 ,57,58,59])

Q.2] Printing array and its types


Input:
import array
arr = array.array('i', [55, 56 ,57,58,59])
print("Array:", arr)
print("Type:", type(arr))

Output:
Array: array('i', [55, 56 ,57,58,59])

Type: <class 'array.array'>


Q.3] Printing array elements
Input:
import array
arr = array.array('i', [55, 56 ,57,58,59])
print("Array elements:")
for element in arr:
print(element)

Output:
Array elements:
55
56
57
58
59

Q.4] Inserting and appending elements


Input:
import array
arr = array.array('i', [55, 56 ,57,58,59])
arr.append(60)
print("Array after appending 60:", arr)
arr.insert(0, 54)
print("Array after inserting 54 at index 0:", arr)
Output:
Array after appending 60: array('i', [55, 56 ,57,58,59,60])

Array after inserting 54 at index 0: array('i', [54,55,


56 ,57,58,59])

Q.5] Python array supports negative index


Input:
import array
arr = array.array('i', [55, 56 ,57,58,59])
print("Last element:", arr[-1])
print("Second last element:", arr[-2])

Output:
Last element: 59
Second last element: 58

Q.6] Removing array elements


Input:
import array
arr = array.array('i', [55, 56 ,57,58,59])
arr.remove(55)
print("Array after removing 55:", arr)

Output:
Array after removing 26: array('i', [ 56 ,57,58,59])

Q.7] Slicing an array


Input:
import array
arr = array.array('i', [55, 56 ,57,58,59])
slice1 = arr[1:5]
print("Sliced array from index 1 to 4:", slice1)

Output:
Sliced array from index 1 to 4: array('i', [56, 57,58,59])

Q.8] Searching an element in the array


Input:
import array
arr = array.array('i', [import array
arr = array.array('i', [55, 56 ,57,58,59])
slice1 = arr[1:5]
print("Sliced array from index 1 to 4:", slice1)
])
search_element = 55
if search_element in arr:
print(f"Element {search_element} is found in the array.")
else:
print(f"Element {search_element} is not found in the array.")

Output:
Element 55 is found in the array.

Q.9] Updating value of specified index


Input:
my_list = [55, 56, 57, 58, 59]
index_to_update = 2
new_value = 54
if 0 <= index_to_update < len(my_list):
my_list[index_to_update] = new_value
else:
print("Index out of range")

print(my_list)
Output:
[55, 56, 54, 58, 59]
Q.10] Reversing an array
Input:
import array = [55, 56, 57, 58, 59]
print("Original array:", arr)
arr.reverse()
print("Reversed array:", arr)

Output:
Original array: array('i', [55, 56 ,57,58,59])
Reversed array: array('i', [59,58,57,56,55])

Q.11] Count of occurrence of an element

Input:
import array
arr = array.array('i', [55, 56, 57, 55, 55, 58, 55])
element_to_count = 55
count = arr.count(element_to_count)
print(f"Element {element_to_count} occurs {count} times in the
array.")

Output:
Element 55 occurs 4 times in the array.
Q.12] Extending an array by appending an iterate
Input:
import array
arr = array.array('i', [55, 56, 57])
print("Original array:", arr)
new_elements = array.array('i', [58, 59, 60])
arr.extend(new_elements)
print("Array after extending:", arr)

Output:
Original array: array('i', [55, 56, 57])
Array after extending: array('i', [55, 56, 57, 58, 59, 60])

Q.13] Converting array to list


Input:
import array
arr = array.array('i', [55, 56, 57, 58, 59])
print("Original array:", arr)
arr_list = list(arr)
print("Converted list:", arr_list)

Output:
Original array: array('i', [55, 56, 57, 58, 59])
Converted list: [55, 56, 57, 58, 59]

You might also like