Home arrow Python arrow Page 3 - Introduction to Python Programming

Working with Numbers - Python

Python has very little to do with Eric Idle or John Cleese. It's an interpreted, object-oriented scripting language. Designed for clarity and simplicity, it's useful for creating large programs and for gluing pieces of other programs together. Keep reading to add Python to your personal programmer's toolkit.

TABLE OF CONTENTS:
  1. Introduction to Python Programming
  2. No one expects the Spanish Inquisition!
  3. Working with Numbers
  4. Python Mathematical Operators
By: James Payne
Rating: starstarstarstarstar / 10
November 12, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

You can also work with numbers in Python. If you want to do some simple math (or even complicated math) try this:


#!/usr/local/bin/python #this line must be included in every program


print 1 + 1

print 4 * 4

print 6 – 2

print 8 / 2

print 10 ** 2

print (1 + 1) * 2

The above code demonstrates a list of expressions. It would print out the following:

  2

  16

  4

  4

  100

  4

If you want to mix numbers and text together, you could do this:


#!/usr/local/bin/python #this line must be included in every program


print “Spam costs two dollars.”

print “If you bought two spams it would cost”, 2 + 2

print “If you order two spams but one is bad”

print “Your refund would be”, 4 - 2

print “Of course we do catering as well”

print “We charge 1.25 cents for more than 100 spams.”

print “So a thousand spams would cost you”, 1.25 * 1000

print “Spam to the third power is”, 2 * 2 * 2

This prints out the following:

  Spam costs two dollars.

  If you bought two spams it would cost 4

  If you order two spams but one is bad

  Your refund would be 2

  Of course we do catering as well

  We charge 1.25 cents for more than 100 spams.

  So a thousand spams would cost you 1250

  Spam to the third power is 8

You will notice that some of the above statements have two arguments. A good example is the second line: “If you bought two spams it would cost”, 2 + 2. The text part is one argument, and then the math (or expression) is a second. When you use more than one argument, you must separate them using a comma. You will also note that the expression 2 + 2 is not surrounded by quotations. If it were, then the program would have printed: If you bought two spams it would cost 2+2. Since there are no quotes, it sees it as a mathematical expression and prints the result instead.



 
 
>>> More Python Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: