Python
  Home arrow Python arrow Page 3 - Introduction to Python Programming
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Introduction to Python Programming
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2007-11-12


    Table of Contents:
  • Introduction to Python Programming
  • No one expects the Spanish Inquisition!
  • Working with Numbers
  • Python Mathematical Operators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introduction to Python Programming - Working with Numbers
    ( Page 3 of 4 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT