Learn to Program

Mixing It Up

Chapter 4

 

We've looked at a few different kinds of objects (numbers and letters), and we made variables to point to them; the next thing we want to do is to get them all to play nicely together.

We've seen that if we want a program to print 25, the following does not work, because you can't add numbers and strings:

var1 = 2
var2 = '5'

print(var1 + var2)

Part of the problem is that your computer doesn't know if you were trying to get 7 (2 + 5), or if you wanted to get 25 ('2' + '5').

Before we can add these together, we need some way of getting the string version of var1, or to get the integer version of var2.

Conversions

To get the string version of an object, we simply wrap it with str(...):

var1 = 2
var2 = '5'

print(str(var1) + var2)
25

Similarly, int(...) gives the integer version of an object, and float(...) gives the float version. Let's look at what these three methods do (and don't do) a little more closely:

var1 = 2
var2 = '5'

print(str(var1) + var2)
print(var1 + int(var2))
25
7

Notice that, even after we got the string version of var1 by calling str(var1), var1 was always pointing at 2, and never at '2'. Unless we explicitly reassign var1 (which requires an = sign), it will point at 2 for the life of the program.

Now let's try some more conversions:

print(float('15'))
print(float('99.999'))
print('')
print(str('stringy'))
print(int(3))
15.0
99.999

stringy
3

The first one is pretty standard, giving 15.0. After that, we converted the string '99.999' to a float. The float did what we expected.

Finally, we saw that our last two conversions did nothing at all, just as we would expect.

Another Look at print

There's something strange about our favorite method... Take a look at this:

print(20)
print(str(20))
print('20')
20
20
20

Why do these three all print the same thing? Well, the last two should, since 20.to_s is '20'. But what about the first one, the integer 20? For that matter, what does it even mean to write out the integer 20? When you write a 2 and then a 0 on a piece of paper, you are writing down a string, not an integer. The integer 20 is the number of fingers and toes I have; it isn't a 2 followed by a 0.

Well, here's the big secret behind our friend, print: Before print tries to write out an object, it uses str to get the string version of that object.

This may not seem too exciting now, but there are many, many kinds of objects in Python (you'll even learn how to make your own!), and it's nice to know what will happen if you try to print a really weird object, like a picture of your grandmother, or a music file or something. But that will come later...

In the meantime, we have a few more methods for you, and they allow us to write all sorts of fun programs...

Getting input

The function input does what it sounds like. And just as print always spits out strings, input will only retrieve strings. And whence does it get them?

From you! Well, from your keyboard, anyway. Since your keyboard only makes strings, that works out beautifully. What actually happens is that input just sits there, reading what you type until you press Enter. Let's try it out:

print(input())
Is there an echo in here?
Is there an echo in here?

Of course, whatever you type in will just get repeated back to you. Run it a few times and try typing in different things.

Now we can make interactive programs! In this one, type in your name and it will greet you:

print('Hello there, and what\'s your name?')
name = input()
print('Your name is ' + name + '?  What a lovely name!')
print('Pleased to meet you, ' + name + '.  :)')

Python politely greets us:

Hello there, and what's your name?
Chris
Your name is Chris?  What a lovely name!
Pleased to meet you, Chris.  :)

A Few Things to Try

Once you have finished those two programs (and any others you would like to try), let's learn some more (and some more about) methods. TODO: note: don't, go straight to chapter 6