Python
  Home arrow Python arrow Page 4 - Python: Count on It
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? 
Google.com  
PYTHON

Python: Count on It
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    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:
      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


    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...



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek