Programming:Python Using variables and math - eMyTextBooks

Search:  

Free online books - just read it!

See live article   •   Programming:Python Using variables and math
 

Programming:Python Using variables and math

Previous: Creating Python programs Index Next: Strings and arrays
Table of contents

Using a variable

A variable is something with a value that may change. In Python, variables are strongly typed, meaning that if a variable has a number, it can't 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:

m_{stone} = \frac{m_{kg} \times 2.2}{14}

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 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 a number. 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 a number.
  • 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.
Previous: Creating Python programs Index Next: Strings and arrays


Also helps finding: ProgrammingPythonUsingvariablesandmath, ProgrammingPython, PythonUsing, Usingvariables, variablesand, andmath, programing, pithon, meth, programmin, pyton, hath, progamming, pyhton, ath

   
 
  
Add to bookmarks
Related Articles
 
Programming:Python Strings and arrays
Programming:Python Creating Python programs
Programming:Python
Computer programming
Top Articles
 
Ada Programming
Ada Programming/Keywords/and
Ada Programming/Keywords/begin
Ada Programming/Keywords/constant
Ada Programming/Keywords/for
Ada Programming/Keywords/of
Ada Programming/Keywords/pragma
Ada Programming/Keywords/private
Ada Programming/Keywords/record
Cookbook
Cookbook:Butter
Cookbook:Pepper
Cookbook:Salt
Cookbook:Tomato
Electronics
Programming:C
SA CUR Info
SA NCS:Computer Applications Technology
SA NCS:Consumer Studies
SA NCS:Hospitality Studies
SA NCS:Visual Arts
Search LiveJournal blogs for Programming:Python Using variables and math
 

Credit Consolidation  •  Secured Loans  •  Shops  •  Credit Consolidation •  Credit Consolidation

Copyright @ 2005 eMyTextBooks.com
This article is from Wikibooks. All text is available under the terms of the GNU Free Documentation License.