An interactive introduction to programming in Python, for human beings and whoever else

*Chapter 14*

Let’s say we have a number. Like, say, `5`

.

And then someone walks up to us and gives us another number. Like, say, `25`

.

And then they give us *ten more numbers!* And then *ten thousand more numbers!!!*

Oof, yeah, that’s a lot of numbers, we’re going to need at least a **small basket.** Let’s make a list of some of the numbers we have:

`5`

`25`

`7`

`19`

If you make a list of numbers using Python, it’s called… **a list**. That’s seriously the data type, the technical name for it! Every other programming language in all of history calls them arrays, but Python wanted to be special, so they’re called **lists**.

To make a **list**, we’ll use square brackets and commas and make something like this:

[5, 25, 7, 19]

Now it’s your turn. Make a list with three numbers: `7`

, `14`

, and `3`

.

- Hint: Square brackets go on the outside of the list
`[`

to start and`]`

to end it - Hint: We’ll use commas to separate the items of our list, like
`1, 2, 3`

- Hint: Type
`[7, 14, 13]`

and press return

What can we do with a list? I love big stuff, so I guess I’d want to know what the biggest number is.

- If
`len`

gives me the**len**gth of a string… - …what function do you think gives me the maximum value in a list?

Hint: Think about it!

Hint: Think about it!!!

Hint: Think about it!!!!!

Yup, you use `max`

to get the **max**imum value in a list. Sometimes programming is predictable! It works just like `len("hello")`

, but with `max`

and a list instead.

Try to use `max`

to find the largest number in `[7, 14, 3]`

. You’ll want to wrap the parentheses that come with `max`

over the whoooole list.

- Hint: Your list should look like
`[7, 14, 3]`

. You’ll wrap the function`max`

around it just like you would with something like`len("hello")`

- Hint: Type
`max([7, 14, 3])`

and press enter

If you didn’t get it, no big deal — it should look like this:

max([7, 14, 3])

^{You can also do max(7, 14, 3) without the square brackets, but let’s keep that a secret!}

If `max`

gets the maximum number in a list of numbers, can you guess the name of the function that finds the **minimum number**? Use it below to find the smallest number from `7`

, `14`

and `3`

.

- Hint: If
`max`

gets the**max**imum, what gets the**min**imum? - Hint: It’s
`min`

! You’ll use`min`

the same way as`max`

- Hint: Type
`min([7, 14, 3])`

and press enter

Yup, it’s `min`

.

In the list `[abs(-100) * 20, 56 * 100, abs(50 - 100) * round(2.1)]`

, each math-y section will be converted into a number. Can you tell me what the smallest number it creates is? You’ll probably want to use cut and paste!

- Hint: You can just paste the list first if you’re curious what numbers the formulas turn into
- Hint: Use
`min`

, and cut and paste the contents of the list. - Hint: Don’t forget the parentheses with
`min`

! - Hint: Type
`min(`

paste`[abs(-100) * 20, 56 * 100, abs(50 - 100) * round(2.1)]`

type`)`

, and then press enter

There are a few more list-friendly functions I want to talk about, but first **I bet you’re pretty tired of typing [7, 14, 3] again and again and again.** We’ll solve that problem in the next chapter!

We learned about the data type **list**, which you can use to make, well, lists of things. Every other language seems to call them arrays.

Have a lot of numbers? You should use a list! We haven’t talked about it yet, but you can also use lists with strings or any other data type.

We also learned a couple useful functions for lists, like `max`

and `min`

.