Python 101 - Parte 8 - Data Structures - Lists
Posted on 08-13-2022 in Tutoriais • 3 min read
Hi!
Today I’m going to talk about Lists in Python.
In case you haven’t seen the previous tutorials, we already have:
- Introduction to Python
- Python Code Blocks
- Conditional Structures in Python - 1
- Conditional Structures in Python - 2
- Repetition Structures - while
- Repetition Structures - for
- Data Structures in Python - 1
Lists are ordered sets of data. In python, these sets can contain many mixed data types, since everything in Python is an object.
So, in addition to basic data types like integers, real numbers, and text, lists can contain any other language object.
Let’s see how lists are created in python:
1 2 3 4 5 6 7 8 |
|
In the first command, we create an empty list, which can be filled in the future. Next, we create a list, already assigning values ​​at the time of creation.
Note that when creating the list, integer values, letters and the list that was created empty previously are passed. Thus, we demonstrate that a list can receive any type of object, and that it can have objects of different types at the same time!
In the third list creation, we use list()
to create a list
object from a string
. This is possible because both lists and strings are sequences.
But, Python lists are not static. We can insert and remove values ​​as we please at any time during program execution. Let’s see:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Now it’s time to exercise!
Open the terminal, and start playing with the lists! See how the commands work!
A tip! The
help
command shows information about a command! try it after creating a list l, the command: >>> help(l.sort)
In the next post we will talk about an operation that has everything to do with lists: List Comprehension.
A challenge!
Who can imagine how to make a matrix in Python using what we saw today?
Leave your answer in the comments!
Be sure to post questions and opinions in the comments below!
See you in the the next post!