08Python Programming

Lists

Easy10MIN

What are arrays used for?

Let's say we have an application for creating a shopping list. We could do something like this:

PY
1purchase_1 = input("What is the first purchase?: ")
2purchase_2 = input("What is the second purchase?: ")
3purchase_3 = input("What is the third purchase?: ")
4
5print("Shopping list: ", purchase_1, purchase_2, purchase_3)
BASH
1python3 ./app.py
2What's the first purchase?: Ketchup
3What's the second purchase?: Cucumber
4What is the third purchase?: Tomato
5Shopping list: Ketchup Cucumber Tomato

The program works, but it has quite a few limitations. The most significant one is that there can be exactly three purchases, no more and no less. We can solve this problem with lists.

1 / 5
Hakatemia Pro

Learn to hack — start here

Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.