Page 442 - CITS - Computer Software Application -TT
P. 442
COMPUTER SOFTWARE APPLICATION - CITS
Example:
Get the value of the first array item:
x = cars[0]
Example:
Modify the value of the first array item:
cars[0] = “Toyota”
OUTPUT
VE
The Length of an Array
Use the len() method to return the length of an array (the number of elements in an array).
Example:
Return the number of elements in the cars array:
x = len(cars)
Looping Array Elements
You can use the for in loop to loop through all the elements of an array.
Example:
Print each item in the cars array:
for x in cars:
print(x)
Adding Array Elements
You can use the append() method to add an element to an array.
Example:
Add one more element to the cars array:
cars.append(“Honda”)
Removing Array Elements
You can use the pop() method to remove an element from the array.
Example:
Delete the second element of the cars array:
cars.pop(1)
429
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137 CITS : IT&ITES - Computer Software Application - Lesson 120 - 137