Python's Not (Just) For Unicorns

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

Chapter 7

Functions and floats

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.

  • Hint: Remember, you need to put quotes around words for Python to understand them. This is called a string.

Not too crazy! How many letters are in the string Hello, my name is Daphne?

  • Hint: Make sure you put quotes around your string so Python knows you’re talking about text.
  • Hint: Put your function with parentheses — len( and ) — around your entire string.
  • Hint: Type 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?

  • Hint: Type 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?

  • Hint: Remember the quotes around your 3 or else Python will think you mean the integer
  • Hint: Type len("3") and press enter

What 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.


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:

  • What’s the data type of "submarine"?
  • What’s the data type of 10000?
  • What’s the data type of 3.14?

  • Hint: Remember to put quotes around submarine, or Python won’t know it’s a string and will get confused/panic
  • Hint: Run type("submarine") to get the data type of "submarine"
  • Hint: If you type 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.
  • Hint: Run type(10000) to get the data type of 10000
  • Hint: Run 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 (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)
  • “years old” (a string)

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!

  • Hint: You’ll be using print with "I ate" and 55 and "bananas"
  • Hint: Type print("I ate", 55, "bananas") and press return

Is 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.

  • Hint: You’ll be using abs and round
  • Hint: You can put functions inside functions! Like print(len("hello"))
  • Hint: Type round(abs(-5.6)) - it takes the absolute value of -5.6 (which is 5.6), then rounds 5.6 up to 6.

Chapter summary

We learned about functions, which are… things that do things. And use parentheses! Our favorites are

  • len, which tells you the length of a string
  • type, which tells you the type of some data
  • print, which prints out the data we want to look at

We also learned about another data type with decimals called floating point numbers. And that 0.1 + 0.2 is a real weird math problem.