welcome to my channel we know that in this video how to create an array in python, access elements,add elements and remove elements
Create an array in python 💻#to access #to add#to remove#python #shorts #viral
@ApnaCollegeOfficial
@CodeWithHarry
@CoderMala
This Python code demonstrates the usage of arrays using the `array` module. Here's a breakdown of what each part does:
1. `import array as ar`: Imports the `array` module and renames it as `ar` for easier reference.
2. `arr = ar.array('i', [1, 2, 3, 4, 5, 6])`: Creates an array named `arr` of type 'i' (integer) containing the elements 1, 2, 3, 4, 5, and 6.
3. `print(arr)`: Prints the entire array `arr`.
4. `print(arr[2])`: Accesses and prints the element at index 2 of the array `arr`, which is 3.
5. `print(arr[1])`: Accesses and prints the element at index 1 of the array `arr`, which is 2.
6. `print(arr[1] + arr[2])`: Accesses elements at index 1 and index 2 of the array `arr`, adds them together, and prints the result.
7. `arr.append(7)`: Appends the integer 7 to the end of the array `arr`.
8. `arr2 = ar.array('i', [8, 9, 10, 11, 12, 13, 14, 15])`: Creates another array named `arr2` with integers from 8 to 15.
9. `arr.extend(arr2)`: Extends the array `arr` by appending all elements from array `arr2` to it.
10. `arr2.remove(15)`: Removes the element 15 from the array `arr2`.
11. `arr2.pop()`: Removes and returns the last element from the array `arr2`.
This code illustrates various operations such as creating arrays, accessing elements by index, appending elements, extending arrays, and removing elements.
import array as ar
arr=ar.array('i',[1,2,3,4,5,6])#To create an array
print(arr)#To print an array
print(arr[2])#To access element 3 using index
print(arr[1])#To access element 2 ising index printed
#tTo add elements 2 and 3 using index and printed
print(arr[1]+arr[2])
print("Adding element in existing array :")
#To add single element 7 in existing attay arr
arr.append(7)
print(arr)#To print array
#To create arr2
arr2=ar.array ('i',[8,9,10,11,12,13,14,15])
#To add all elements of arr in existing array arr
arr.extend(arr2)
#To print array
print(arr)
print()
print("Delete element in existing array.")
#To remove single element from existing array aar2
arr2.remove(15)
print(arr2) # to print arr2
#To remove last element of array arr2
arr2.pop()
print(arr2)#To print arr2
Информация по комментариям в разработке