$$ \newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor} \newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil} \renewcommand{\mod}{\,\mathrm{mod}\,} \renewcommand{\div}{\,\mathrm{div}\,} \newcommand{\metar}{\,\mathrm{m}} \newcommand{\cm}{\,\mathrm{cm}} \newcommand{\dm}{\,\mathrm{dm}} \newcommand{\litar}{\,\mathrm{l}} \newcommand{\km}{\,\mathrm{km}} \newcommand{\s}{\,\mathrm{s}} \newcommand{\h}{\,\mathrm{h}} \newcommand{\minut}{\,\mathrm{min}} \newcommand{\kmh}{\,\mathrm{\frac{km}{h}}} \newcommand{\ms}{\,\mathrm{\frac{m}{s}}} \newcommand{\mss}{\,\mathrm{\frac{m}{s^2}}} \newcommand{\mmin}{\,\mathrm{\frac{m}{min}}} \newcommand{\smin}{\,\mathrm{\frac{s}{min}}} $$

Prijavi problem

Obeleži sve kategorije koje odgovaraju problemu

Još detalja - opišite nam problem

Uspešno ste prijavili problem!
Status problema i sve dodatne informacije možete pratiti klikom na link.
Nažalost nismo trenutno u mogućnosti da obradimo vaš zahtev.
Molimo vas da pokušate kasnije.

Data collections

In the previous lesson, we used a tuple to perform some commands (computing and printing) over each value from the tuple using the for loop.

Torques are also a type of data in Python, such as numbers, strings, or logical values. The types int (integer), float (real number), str (string) and bool (logical value) are basic types. The difference between tuples and basic types is that the value of a tuple consists of more than one value of a simpler type.

Each value that consists of multiple values of a simpler type will be called a collection. The data the collection consists of are called collection elements.

We can say that tuple is one kind of collection, and it is the first kind of collection we encounter.

Tuple and its elements

Packing and unpacking tuples

We can fit the whole tuples into a variable, as we do with values of a simpler type. In the following example, the temperatures variable contains the entire tuple as its value.

This kind of value assignment (as in the first line of the program) is also called tuple packing. The reverse assignment is also possible: when we know how many elements are there in a tuple, we can assign the tuple elements to the corresponding number of variables:

Statements like first_name, middle_name, last_name = full name are called tuple unpacking.

The same effect is achieved by a related statement

first_name, middle_name, last_name = "Arthur", "Conan", "Doyle"

Tuples do not appear in this statement, which is called multiple value assignment.

Tuple elements and indices

We can also get the elements of a tuple by writing the name of the tuple, and behind it in square brackets the sequence number of the element we want. It should be noted here that counting the elements of any collection starts from zero. For example:

The sequence number of an element is also called the index of the element. if the tuple has n elements, we can use the numbers 0, 1, 2, … n-1 as indices. In the example above, n = 3, so indices 0, 1, and 2 are allowed. Trying to use an index outside these bounds causes an error (try it).

Tuple length

The number of elements of a tuple can be obtained by using the len function.

or shorter:

Note the double brackets (one for function and the other for tuple).

Through these examples we have seen that tuple elements can be numbers or strings. In fact, the tuple elements can be of any type, basic or complex.

For example, it is possible to create a tuple of tuples:

Tuple t contains two simpler tuples, therefore the number of its elements is 2.

In Python, the elements of a tuple can be of different types, and we will soon see such examples as well.


Range is another type of collection. Unlike tuple, the elements of this collection are always integers.

Range can be defined in several ways.

Range with one argument

The simplest form of specifying a range is range(n), where n is a positive integer. The range(n) range contains integers from 0 to n, not including n. For example, range(5) contains values 0, 1, 2, 3, 4.

We see that in the for statement, we can use range in the same way as the tuple. In fact, any collection may be in place of the tuple or range.

Since the range(n) range contains a total of n values, this range is often used when a command only needs to be repeated n times in the same way:

The print function was executed for each value i of sequence 0, 1, 2, 3, 4, but in this example, those values are not used in the loop body. Thus, we achieved that the print function was executed 5 times in exactly the same way, that is, it was repeated 5 times.

Another common use of this type of range is to get through all the elements of a tuple. In such case, the loop variable serves as an index. This way of passing through the values of the tuple is suitable when besides these tuple values in the loop we also need their sequence numbers (this way of passing through the collection is more common in other programming languages than Python).

Range with two arguments

When we need a sequence of consecutive integers that does not start at zero, we set the range as range(a, b), where a and b are integers such that: math:a<b. Then the sequence is made up of integers from a to b, not including b. For example, the range range(1, 6) gives the sequence of numbers 1, 2, 3, 4, 5:

Range with three arguments

The third form of specifying a range has three arguments:

The range values given by range(a, b, c) go from a to b (not including b) with the step c, i.e. values change by c. Step c can also be negative:

We can convert a range into a tuple (the opposite is not possible, nor is it ever needed):

String as a collection

We have used strings as the basic type so far, but strings can also be used as collections of individual characters. We can traverse string characters using a loop and retrieve individual characters using indices:

Functions of collections

There are many functions in Python that accept the collection as an argument. One of them is the len function, which we have already met. Some other commonly used functions that apply to collections are:

  • sum, a function that gives the sum of the elements of a collection

  • min, a function that gives the smallest element of a collection

  • max, a function that gives the largest element of a collection

The values of the functions len, sum, min, max for the range can also be determined from the parameters of the range, but here we wanted to point out that these functions accept different collections as their argument.


    Q-20: What does the following program print?

    t = (32, 41, 20, 17)
    a, b, c, d = t
  • a program error occurs
  • Try again
  • 2
  • Try again
  • 20
  • Correct
  • 3
  • Try again

    Q-21: What does the following program print?

    a = (1, 2, 3)
  • 1
  • Try again
  • 2
  • Correct
  • a program error occurs
  • Try again
  • 3
  • Try again

    Q-22: Which range contains values 1, 2, 3?

  • range(4)
  • Try again
  • range(1, 4)
  • Correct
  • range(3)
  • Try again
  • range(1, 3)
  • Try again

    Q-23: How many values does range(1, 10, 2) contain?

  • 5
  • Correct
  • 6
  • Try again
  • 9
  • Try again
  • 10
  • Try again
    Pair ranges with the number of elements. Try again!
  • 5
  • range(5)
  • 0
  • range(3, 3)
  • 3
  • range(1, 4)
  • 1
  • range(3, 6, 3)
    Pair ranges with values. Try again!
  • 3, 4, 5
  • range(3, 6)
  • 0, 1, 2
  • range(3)
  • 3, 1
  • range(3, -1, -2)
  • 3, 2, 1, 0, -1
  • range(3, -2, -1)
  • 3
  • range(3, 6, 3)