Course Nine - Use numbers with Python
In Python, numeric data types are used to store numeric values. These values are immutable, meaning that changing a value creates a new object in memory rather than modifying the existing one.
Creating Numeric Objects
Numeric objects are created when a value is assigned to a variable:
var1 = 1
var2 = 10
You can also delete numeric objects using the del
statement:
del var1
del var_a, var_b
Numeric Data Types in Python
Python supports four primary numeric types:
-
Integer (
int
): Whole numbers, positive or negative, without a decimal point. -
Long integer (
long
): Integers of unlimited size. Denoted withL
at the end (preferably uppercase to avoid confusion with the digit1
). -
Floating point (
float
): Real numbers with a decimal point. Can also be expressed using scientific notation (5e2 = 5 × 10²
). -
Complex numbers (
complex
): Numbers in the forma + bj
, wherea
is the real part,b
is the imaginary part, andj
represents the square root of -1.
Examples of numbers:
Complex | Float | Long | Int |
---|---|---|---|
3.14j | 0.0 | 51924361L | 10 |
45.j | 15.20 | -0x19323L | 100 |
9.322e-36j | -21.9 | 0122L | 786 |
.876j | 32.3 + e18 | 0xDEFABCECBDAECBFBAEL | 080 |
-6.545+0J | -90.5346…L | -0490 | … |
3e+26J | -32.54e100 | -052318172735L | -0x260 |
4.53e-7j | 70.2-E12 | -7421885298529L | 0x69 |
Converting Between Number Types
Python allows conversion between numeric types using:
int(x) # Convert x to integer
long(x) # Convert x to long integer
float(x) # Convert x to floating point
complex(x,y) # Convert x and y to a complex number (real=x, imaginary=y)
Common Mathematical Functions
Python provides built-in functions for performing various calculations:
Function | Description |
---|---|
abs(x) |
Returns the absolute value of x . |
ceil(x) |
Returns the smallest integer greater than or equal to x . |
cmp(x, y) |
Returns -1 if x<y, 0 if x==y, 1 if x>y. |
exp(x) |
Returns e^x. |
fabs(x) |
Returns the absolute value of x as a float. |
floor(x) |
Returns the largest integer less than or equal to x. |
log(x) |
Natural logarithm of x. |
log10(x) |
Base-10 logarithm of x. |
max(x1, x2, …) |
Returns the largest value. |
min(x1, x2, …) |
Returns the smallest value. |
modf(x) |
Returns fractional and integer parts of x as a tuple. |
pow(x, y) |
Returns x raised to the power y. |
round(x [,n]) |
Rounds x to n decimal places. |
sqrt(x) |
Returns the square root of x. |
Working with Random Numbers
Python’s random
module provides tools for generating random numbers:
Function | Description |
---|---|
choice(seq) |
Returns a random element from a sequence. |
randrange([start,] stop [, step]) |
Returns a randomly selected element from a range. |
random() |
Returns a random float in [0.0, 1.0). |
seed([x]) |
Initializes the random number generator. |
shuffle(lst) |
Shuffles a list in place. |
uniform(x, y) |
Returns a random float r such that x ≤ r < y. |
Trigonometric Functions
Python provides several functions to handle trigonometric calculations:
Function | Description |
---|---|
acos(x) |
Returns the inverse cosine of x (in radians). |
asin(x) |
Returns the inverse sine of x (in radians). |
atan(x) |
Returns the inverse tangent of x (in radians). |
atan2(y, x) |
Returns atan(y/x) in radians. |
cos(x) |
Returns the cosine of x (in radians). |
hypot(x, y) |
Returns sqrt(xx + yy). |
sin(x) |
Returns the sine of x (in radians). |
tan(x) |
Returns the tangent of x (in radians). |
degrees(x) |
Converts radians to degrees. |
radians(x) |
Converts degrees to radians. |
Mathematical Constants
Python provides important mathematical constants:
-
pi
: The value of π. -
e
: The base of natural logarithms.
Comments
Post a Comment