local (1)
local (1)
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.
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:
x = students[0]
print(x)
students[2] = “Kristina”
print(students)
n = len(students)
print(n)
students.append(“Nadezda”)
print(students)
To remove one item from an array by it’s index:
students.pop(2)
print(students)
students.remove(“Artem”)
print(students)
for x in students:
print(x)
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.