An interactive introduction to programming in Python, for human beings and whoever else
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
Not too crazy! How many letters are in the string
Hello, my name is Daphne?
)— around your entire string.
len("Hello, my name is Daphne")and press enter.
Now, try to get the
3 (the number). It won’t work. Can you understand any part of the error message?
len(3)and press enter.
Trying to use
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
"3" (the string version, with quotes!). Will that work?
3or else Python will think you mean the integer
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.
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, or Python won’t know it’s a string and will get confused/panic
type("submarine")to get the data type of
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
type(3.14)to get the data type of
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
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).
type, the other most important function ever is called
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)
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("I ate", 55, "bananas")and press return
Is your mind blown yet?
"This doesn't get displayed" print("This gets displayed")
Notice how only the line with
Some other functions you can try out if you need help going to sleep tonight:
||Rounds decimals to integers|
||Absolute value (distance from zero)|
||String length (number of characters)|
||Convert data to
||Convert data to
||Convert data to
||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.
round(abs(-5.6))- it takes the absolute value of
5.6), then rounds
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
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.