Lists and Tuples

What we needed to do:

Show your ability and knowledge of the use of Lists and Tuples in Python. When should you use one and when should you use the other?

 

What are lists?

In Python 3 there are many built-in data structures. The simplest one of them are lists. It is used to store a list of values. In other words, lists are a collection of items like strings, integers or lists.

How are lists used?

In a list every item has a index value.

When we use them, we need to enclose them with []. Unlike tuples, lists can be changed.

There are many functions that are already built in allowing to work with lists.

For example with the function length we can get the length of a list:

list = ["1", "hello", "Miri, "4"]
len(list)
>>4

 

What are tuples?

Tuples are a sequence of Python objects that are unable to be changed. So they can not be modified.

How are tuples used?

Unlike lists which use square brackets, tuples use parentheses.

house = (‚wall‘, ‚door‘, ‚window‘, ‚roof‘, ‚floor‘)

The output would look like this:

print(house)

(‚wall‘, ‚door‘, ‚window‘, ‚roof‘, ‚floor‘)

Hinterlasse einen Kommentar