Skip to Content
Python in a Nutshell, 2nd Edition
book

Python in a Nutshell, 2nd Edition

by Alex Martelli
July 2006
Beginner to intermediate
734 pages
20h 6m
English
O'Reilly Media, Inc.
Content preview from Python in a Nutshell, 2nd Edition

The math and cmath Modules

The math module supplies mathematical functions on floating-point numbers, while the cmath module supplies equivalent functions on complex numbers. For example, math.sqrt(-1) raises an exception, but cmath.sqrt(-1) returns 1j.

Each module exposes two attributes of type float bound to the values of fundamental mathematical constants, pi and e, and the following functions.

acos

acos(x)

Returns the arccosine of x in radians.

math and cmath

acosh

acosh(x)

Returns the arc hyperbolic cosine of x in radians.

cmath only

asin

asin(x)

Returns the arcsine of x in radians.

math and cmath

asinh

asinh(x)

Returns the arc hyperbolic sine of x in radians.

cmath only

atan

atan(x)

Returns the arctangent of x in radians.

math and cmath

atanh

atanh(x)

Returns the arc hyperbolic tangent of x in radians.

cmath only

atan2

atan2(y,x)

Like atan(y/x), except that atan2 properly takes into account the signs of both arguments. For example:

>>> import math
>>> math.atan(-1./-1.)
0.78539816339744828
>>> math.atan2(-1., -1.)
-2.3561944901923448

Also, when x equals 0, atan2 returns pi/2, while dividing by x would raise ZeroDivisionError.

math only

ceil

ceil(x)

Returns float(i), where i is the lowest integer such that i >= x.

math only

cos

cos(x)

Returns the cosine of x in radians.

math and cmath

cosh

cosh(x)

Returns the hyperbolic cosine of x in radians.

math and cmath

e

The mathematical constant e.

math and cmath

exp

exp(x)

Returns e**x.

math and cmath

fabs

fabs(x)

Returns the absolute value of x.

math only

floor

floor(x)

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python in a Nutshell, 3rd Edition

Python in a Nutshell, 3rd Edition

Alex Martelli, Anna Ravenscroft, Steve Holden
Python in a Nutshell, 4th Edition

Python in a Nutshell, 4th Edition

Alex Martelli, Anna Martelli Ravenscroft, Steve Holden, Paul McGuire
Python One-Liners

Python One-Liners

Christian Mayer

Publisher Resources

ISBN: 0596100469Errata Page