Python Programming/Using variables and math

From testwiki
Jump to navigation Jump to search

Template:Python Programming/TopNav

Using a variable

A variable is something with a value that may change. In Python, variables are dynamically typed, meaning that in the same program, a variable can have a number as a value, it can later be treated as a string, or vice versa. Here is a program that uses a variable:

#!/usr/bin/python

name = 'Ada Lovelace'
print "Goodbye, " + name + '!'

(Oops! I used single quotes for Ada's name, then double quotes around Goodbye. That's OK, however, because these two quotes do exactly the same thing in Python. The only thing you can't do is mix them and try to make a string like this: "will not work'.)

This program isn't much use, of course. But what about variables that the program truly can't guess about?

raw_input()

#!/usr/bin/python

print 'Please enter your name.'
name = raw_input()
print 'How are you, ' + name + '?'

(What's raw_input() doing? Evidently, it's getting input from you. See Input and output.)

Of course, with the power of Python at hand, the urge to determine one's mass in stone is nearly irresistible. A concise program can make short work of this task. Since a stone is 14 pounds, and there are about 2.2 pounds in a kilogram, the following formula should do the trick:

mstone=mkg×2.214

Simple math

#!/usr/bin/python

print "What is your mass in kilograms?",
mass_kg = int(raw_input())
mass_stone = mass_kg * 2.2 / 14
print "You weigh " + str(mass_stone) + " stone."

Run this program and get your weight in stone!

This program is starting to get a little bit cluttered. That's because, in addition to all the math, I snuck in some new features.

  • When the previous program asked for your name, you were typing below the question. This time, you're typing at the end of the line that asks, "What is your mass in kilograms?". What's happening here is that, normally, the print statement will add a newline to the end of what you're printing. That's why the cursor went to the next line in the previous program. But in this program, I added a little comma to the end. That makes print omit the newline.
  • int() - this handy function takes a string, and returns an integer. Remember when you read that Python is strongly typed? Python won't allow us to do math on a string. Whatever you type is a string, even if it consists of digits. But int() will recognize a string made of digits and return an integer.
  • The str(mass_stone) in the print statement. It turns out that you can't add together strings and numbers; "You weigh " + mass_stone just wouldn't work. So, we have to take the number and turn it into a string. Incidentally, ` would do the same thing as the str() function, but that is deprecated.

Formatting output

In the previous program, we used this line of code to print the result:

print "You weigh " + str(mass_stone) + " stone."

There are a couple of problems with this. First, it mixes up operators and quotes, and can be a little tough to read. Second, the number won't be printed very nicely, as the following example illustrates:

$ ./kg2stone
What is your mass in kilograms? 65
You weigh 10.214285714285714 stone.

Not only is that much accuracy unjustified, it doesn't look nice. Python's % operator comes to the rescue. It allows printf-like formatting, in the form:

STRING % (arg1, arg2, ...)

The string contains one format code for each argument. There are several types of format codes; see the strings section for a complete list.

To improve our program, we just need the %f format code:

print "You weigh %.1f stone." % (mass_stone)

The %.1f format code causes a floating point number to be printed, with exactly one digit after the decimal. This produces much nicer output:

$ ./kg2stone
What is your mass in kilograms? 65
You weigh 10.2 stone.

Template:Python Programming/Navigation