Python
  Home arrow Python arrow Page 4 - Python: Count on It
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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

Python: Count on It
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-19

    Table of Contents:
  • Python: Count on It
  • Decimals, Division, and Floats
  • Floating Points
  • Built-in Math Functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Python: Count on It - Built-in Math Functions
    (Page 4 of 4 )

    There are several built-in functions in Python. We will discuss some of them below.

    Working those Abs

    The abs function returns the absolute value of a number. Try the following examples:


    >>> print abs(9000)

    9000

    >>> print abs(-9000)

    9000

    >>> print abs(12.6)

    12.6

    >>> print abs(-12.6)

    12.6

    Min and Max...Always Comparing

    If you need to find the largest or smallest number in a list, you can count on these two fellas to help you do so. Here they both are at work:


    >>> print max([100, 200, 2, 4, 900, 6, 8000])

    8000

    >>> print min(1000, 2000, 2, 1, 19, 80)

    1

    Summing Up the Range

    If you ever find yourself wanting to sum up a range of numbers, you can do so by combining sum and range, like in the following program, where we calculate 1+2+3+4+5+6+7+8+9:


    >>> print sum(range(1, 10))

    45

    In addition to the built-in functions, you can also work with modules, some of which we have already been using, like the math module and the decimal module. Here are some of the uses of the math module:

    Square Root


    >>> import math

    >>> math.sqrt(3)

    1.7320508075688772

    >>> math. sqrt(9)

    3.0

    >>> math.sqrt(32)

    5.6568542494923806

    >>> math.sqrt(1200)

    34.641016151377549

    >>>

    Remember when using a module to import it first.

    Exponentiation


    >>> math.exp(12)

    162754.79141900392

    >>> math.exp(1)

    2.7182818284590451

    >>> math.exp(50)

    5.184705528587072e+021

    Math, unfortunately, ended for me in the ninth grade, so I can't really tell you what all of these functions do, but here is a list of the ones you will find in the math module:

    • sqrt: square root
    • exp: exponentiation
    • log/log10: natural logarithm/base 10 logarithm
    • sin/cos/tan: trigonometric functions
    • sinh/cosh/tanh: hyperbolic functions
    • asin/acos/atan: inverse trigonometric functions
    • hypot: equal to sqrt(x**2+y**2)
    • atan2: similar to atan but gets the quadrant right and handles a 0 denominator
    • floor/ceil: seen earlier in the article
    • pi and e: Something you can eat, or math that I don't understand.

    Other modules include the cmath, which is used for complex numbers and has many of the same functions as math, yet returns results as a complex number type; the decimal module, that works with decimal numbers as opposed to floating points; and the random module, which has functions that allow you to generate random numbers and choose randomly from different elements.

    Conclusion

    Well that's it for this article. Now go forth and do brilliant math stuff with Python. I'll just be slaving away here, hard at work on the next programming tutorial. Check back frequently for another dazzling display from my massive stores of knowledge.

    Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hey, thanks for stopping by. In this article we cover math in Python, including such...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway