0% found this document useful (0 votes)
9 views4 pages

Arrays Py

The document provides a Python script demonstrating various operations on arrays, including accessing, modifying, adding, and removing elements. It shows how to determine the length of an array and loop through its elements. Additionally, it includes examples of error handling when attempting to run a non-existent file.

Uploaded by

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

Arrays Py

The document provides a Python script demonstrating various operations on arrays, including accessing, modifying, adding, and removing elements. It shows how to determine the length of an array and loop through its elements. Additionally, it includes examples of error handling when attempting to run a non-existent file.

Uploaded by

mohamedabdi45352
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

colours = ["black", "white", "red", "white", "green"]

#Excessing elements of an array


x = colours[3]
print(x)
x = colours[0]
print(x)
#Modify the value of the first array item
colours[0] = "purple"
print(colours)
colours [3] = "yellow"
print(colours)
#Determining the number of elements in an array/length of an array
x = len(colours)
print(x)
#Looping Array Elements
#You can use the for in loop to loop through all the elements of an array
for y in colours:
print(y)
#Adding Array Elements
#You can use the append() method to add an element to an array.
[Link]("brown")
print(colours)
#Removing Array Elements
#You can use the pop() method to remove an element from the array.
[Link](3)
print(colours)
#You can also use the remove() method to remove an element from the
array.
[Link]("red")
print(colours)
PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

black

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

black

['purple', 'white', 'red', 'white', 'green']

white

black
['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

C:\Users\HP\AppData\Local\Microsoft\WindowsApps\[Link]: can't open file 'C:\\Users\\HP\\


OneDrive\\Desktop\\MY PROJECTS\\PYTHON\\[Link]': [Errno 2] No such file or directory

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

black

['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

purple

red

yellow

green

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

black

['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

purple

white

yellow
green

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

black

['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

purple

white

red

yellow

green

['purple', 'white', 'red', 'yellow', 'green', 'brown']

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

purple

white

red

yellow

green

['purple', 'white', 'red', 'yellow', 'green', 'brown']

Traceback (most recent call last):

File "C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON\[Link]", line 25, in <module>

TypeError: 'str' object cannot be interpreted as an integer

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white
black

['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

purple

white

red

yellow

green

['purple', 'white', 'red', 'yellow', 'green', 'brown']

['purple', 'white', 'red', 'green', 'brown']

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON> python [Link]

white

black

['purple', 'white', 'red', 'white', 'green']

['purple', 'white', 'red', 'yellow', 'green']

purple

white

red

yellow

green

['purple', 'white', 'red', 'yellow', 'green', 'brown']

['purple', 'white', 'red', 'green', 'brown']

['purple', 'white', 'green', 'brown']

PS C:\Users\HP\OneDrive\Desktop\MY PROJECTS\PYTHON>

You might also like