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

local (1)

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)
4 views

local (1)

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/ 3

9th and 10th lessons of the

ICT module for the grade 8


Andrius Romaška
https://www.vienozmogausrespublika.lt

Arrays

An array is a collection of items of the same variable type that are stored at contiguous memory locations.

Therefore, once you define the type of values that your array will store, all its elements must be of that same.

Each item in an array is indexed starting with 0. Each element in an array is accessed through its index.

If your array is stored in array named ‘fruits’ and you want to access the first element of it (at index 0), you
would use fruits[0].
Types of arrays

There are two main types of arrays: one-dimensional arrays - arrays that store a single row of elements (we’ve
already seen examples of it above) and multidimensional arrays - arrays that store multiple rows of elements.

Column 0 Column 1 Column 2


Row 0 5 2 3
Row 1 7 6 4

Key principles of arrays in Python

Python is one the most popular programming languages in the world. Let’s have a look what key actions we can
do with arrays in Python programming language (we are able to test all of these actions and experiment on
https://www.programiz.com/python-programming/online-compiler/):

To create an array:

students = [“Artem”, “Dovydas”, “Kristupas”]


print(students)

To access particular item of array:

x = students[0]
print(x)

To change a particular item value of array:

students[2] = “Kristina”
print(students)

To get to know how many elements in array we have:

n = len(students)
print(n)

To add element to an array:

students.append(“Nadezda”)
print(students)
To remove one item from an array by it’s index:

students.pop(2)
print(students)

To remove one item from an array that matches a value:

students.remove(“Artem”)
print(students)

To loop through each item of an array and do some action:

for x in students:
print(x)

To sort our array in an order we want:

students.sort(reverse=True)
print(students)

Exercises:

1. Write a Python program to create an array of 6 integers. Print the 2nd, 4th and 6th elements of this array.
2. Write a Python program to create an array of 5 fruits. Remove the 3rd and the 5th fruits from it and print
the final array.
3. Write a Python program to create an array of these numbers: 23, 11, 45, 67, 8, 92, 4, 56. Find and print the
largest number of this array, so that your program would be universal for any type of numbers’ list.
4. Create an array of any 12 integers in Python. Write an algorithm to calculate a sum of these integers and
print it.
5. Create an array of any 10 integers in Python. Write an algorithm to find and print only odd numbers of the
mentioned array.
6. Write a Python program to create an array of these numbers: 81, 22, 35, 41, 100, 6, 5, 97. Find and print all
the numbers that end with 5 or 0, so that your program would be universal for any type of numbers’ list.

You might also like