An interactive introduction to programming in Python, for human beings and whoever else
Let’s say we have a number. Like, say,
And then someone walks up to us and gives us another number. Like, say,
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:
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:
[to start and
]to end it
1, 2, 3
[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.
lengives me the length of a string…
Hint: Think about it!
Hint: Think about it!!!
Hint: Think about it!!!!!
Yup, you use
max to get the maximum 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.
[7, 14, 3]. You’ll wrap the function
maxaround it just like you would with something like
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!
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
maxgets the maximum, what gets the minimum?
min! You’ll use
minthe same way as
min([7, 14, 3])and press enter
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!
min, and cut and paste the contents of the list.
[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