An interactive introduction to programming in Python, for human beings and whoever else
Chapter 7
The little bit of magic we did at the end of last chapter - str(2)
to convert an integer into a string - the str
part is called a function. Functions are… things that do things. And have parentheses! Honestly, that might be the best explanation you’re going to get.
There are a lot of functions out there! For example, len
can be tell you how many letters is in a string (the length, in other words). If you type len("blah")
, it will give you 4
.
len("blah")
Not too crazy! How many letters are in the string Hello, my name is Daphne
?
len(
and )
— around your entire string.len("Hello, my name is Daphne")
and press enter.Now, try to get the len
of 3
(the number). It won’t work. Can you understand any part of the error message?
len(3)
and press enter.Trying to use len
with 3
gives you the error object of type int has no len()
, which seems to mean integers don’t have a length. Makes sense!
But what if you try to get the len
of "3"
(the string version, with quotes!). Will that work?
3
or else Python will think you mean the integerlen("3")
and press enterWhat do the parentheses do? Why are they attached to the function? Well, like usual: it’s just how Python works. Most programming languages have functions, and most use parentheses just like this, so at least you only have to learn about them once in your entire life.
Another useful function is type
. We can use type
to tell us whether our data is a string or an integer or whatever other data type it might be.
type("hello") type(2)
That class
thing is just Python’s fancy way of saying type. We can also say data type if we want to be long-winded and fancy and wearing tuxedos and nice shoes.
A few questions we can answer now:
"submarine"
?submarine
, or Python won’t know it’s a string and will get confused/panictype("submarine")
to get the data type of "submarine"
type(10,000)
you’ll break our fake Python console and have to refresh the page! So only do it if you’re an anarchist.type(10000)
to get the data type of 10000
type(3.14)
to get the data type of 3.14
Wait, 3.14
is a float? What in the world is a float?
A float is like an integer, but it has a decimal. It isn’t called a decimal
because, well, that would be too easy! And because, technically speaking, it’s something called a “floating point number”. You can read the Wikipedia page if you want to know more.
What’s a floating point number? I dunno, math stuff, not important. But here’s something that is important: if you want to feel smarter than a computer, ask it to add together 0.1
and 0.2
.
Weird, right?!
Mostly you can trust computers to do math for you, except for this one time. Oh, and that time Intel screwed up floats bigtime in 1994. But mostly, yes, trust your computer. This is just a fun trick to use at really boring parties.
If you’re curious about why Python can’t add those two numbers together, you can read a whole web page about it at 0.30000000000000004.com (the explanation is in the small text up at the top).
Besides type
, the other most important function ever is called print
, and it… prints stuff out (yes, on your screen, not on a printer).
print(0.1 + 0.2)
It seems pretty boring and not any different than what we’ve done already, but we can actually print multiple things on the same line. Are you excited yet?!
print(1, 2, 3, 4) print("Hello", "world!")
Just remember you use commas to separate the things you’re printing. We can use that to print whole sentences that use both strings and integers!
print("I am", 100, "years old")
In that case, we printed:
"I am"
(a string)100
(an integer)Now it’s your turn. Let’s make Python say I ate 55 bananas
. It’s a lot of bananas, right? I’d brag about it, too. And don’t just print "I ate 55 bananas"
, that’s cheating!
print
with "I ate"
and 55
and "bananas"
print("I ate", 55, "bananas")
and press returnIs your mind blown yet?
print
also becomes useful when we stop using the console and start using the editor, which is a tool that allows us to edit multiple lines of code at once. We aren’t going to do that for a little while, but just so you know, try clicking run on the editor below.
"This doesn't get displayed" print("This gets displayed")
Notice how only the line with print
actually gets printed when we run the code? Once we stop using the console we have to say print
to get anything to show up. Remember that for a future chapter!
Some other functions you can try out if you need help going to sleep tonight:
Function | Output | Description |
---|---|---|
round(3.14) |
3 |
Rounds decimals to integers |
abs(-100) |
100 |
Absolute value (distance from zero) |
len("hello") |
5 |
String length (number of characters) |
str(9) |
'9' |
Convert data to string |
int('9') |
9 |
Convert data to integer |
float(3) |
3.0 |
Convert data to float |
print("hello") |
"hello" |
Print things out |
If you’d like, feel free to test them out before we start the next chapter. If you’d like a mission, what is the absolute value of -5.6, rounded to an integer.
abs
and round
print(len("hello"))
round(abs(-5.6))
- it takes the absolute value of -5.6
(which is 5.6
), then rounds 5.6
up to 6
.We learned about functions, which are… things that do things. And use parentheses! Our favorites are
len
, which tells you the length of a stringtype
, which tells you the type of some dataprint
, which prints out the data we want to look atWe also learned about another data type with decimals called floating point numbers. And that 0.1 + 0.2
is a real weird math problem.