Problem

1/11

Arrays (lists). Introduction

Theory Click to read/hide

Data storage

Let's say you've developed a computer game called "User Hostile" in which the players compete against an intricate and unfriendly computer interface. Now you need to write a program that tracks the monthly sales of this game over a five year period. Or let's say you need to inventory Hacker Hero Trading Cards.
Very soon you will come to the conclusion that you need more than simple basic data types to store and process information.
 

Lists (arrays)

In order to make it convenient to work with a large amount of data, a group of cells is given a common name. Such a group of cells is called an array.
 
Array – it is a group of memory cells of the same type, located side by side and having a common name. Each cell in the group has a unique number.

When working with arrays, you need to learn how to solve three tasks:
x allocate memory of the required size for the array;
x write data to the desired cell;
x read data from a cell.

 

Arrays in Python

There are no such arrays in Python. Instead, lists are used to store a group of objects of the same type (but not only of the same type) - objects of the list type. The difference between lists and arrays is that a list is a dynamic structure, the size of which can be changed during program execution (deleting, adding elements) without thinking about memory management operations (this is done by the compiler).
In the future, when analyzing work with lists, we will use the word "array", since most often lists are used precisely in the role of an array (they store data of the same type).
 
Creating an array
When an array is created, space is allocated in memory (a certain number of cells). 1) Arrays can be created by simply enumerating elements: A = [1, 4, 7, 12, 5] print(type(A)) The program will output
<class 'list'>
That is, an array is an object of type list (translated from English list).

2) Arrays can be composed of data of any type - integers or real numbers, character strings  A = ["Vasya", "Petya", "Fedya"]

Some ways to work with arrays
3) Arrays can be "added". A = [1, 5] + [25, 7]
4) Addition of identical arrays can be replaced by multiplication. So it's easy to create an array filled with the same values, like so: A = [0]*10 # created an array of 10 elements and filled it with zeros
5) An array always "knows" your size. The  len() function is used to determine the size of an array. Often the size of the array is stored in a separate variable so that the program can be easily changed to work with a different array size.

Example
N = 10 # store the size of the array in variable N A = [0] * N # create an array of size N print(len(A)) # print the size of the array The size of an array can be set from the keyboard.

Problem

Modify the program in such a way that the value of the variable N is entered from the keyboard in the first line, and an array of size N is created in the second line (the values ​​of the array elements can be any).