0% found this document useful (0 votes)
9 views3 pages

Python Number Data Types Explained

The document provides an overview of number data types in Python, including int, long, float, and complex, along with their characteristics and examples. It explains how to create, delete, and convert number objects, as well as lists various mathematical, random, and trigonometric functions available in Python. Additionally, it mentions two mathematical constants, pi and e.

Uploaded by

kimaniixv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Python Number Data Types Explained

The document provides an overview of number data types in Python, including int, long, float, and complex, along with their characteristics and examples. It explains how to create, delete, and convert number objects, as well as lists various mathematical, random, and trigonometric functions available in Python. Additionally, it mentions two mathematical constants, pi and e.

Uploaded by

kimaniixv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON NUMBERS

[Link] rialspo [Link] m/pytho n/pytho n_numbe [Link] Co pyrig ht © tuto rials po [Link] m

Number data types store numeric values. T hey are immutable data types, which means that chang ing the value of
a number data type results in a newly allocated object.

Number objects are created when you assig n a value to them. For example:

var1 = 1
var2 = 10

You can also delete the reference to a number object by using the del statement. T he syntax of the del statement
is:

del var1[,var2[,var3[....,varN]]]]

You can delete a sing le object or multiple objects by using the del statement. For example:

del var
del var_a, var_b

Python supports four different numerical types:

int (sig ned integ ers): often called just integ ers or ints, are positive or neg ative whole numbers with
no decimal point.

long (long integ ers ): or long s, are integ ers of unlimited size, written like integ ers and followed by an
uppercase or lowercase L.

float (floating point real values) : or floats, represent real numbers and are written with a decimal
point dividing the integ er and fractional parts. Floats may also be in scientific notation, with E or e indicating
the power of 10 (2.5e2 = 2.5 x 10 2 = 250).

c omplex (c omplex numbers) : are of the form a + bJ, where a and b are floats and J (or j)
represents the square root of -1 (which is an imag inary number). a is the real part of the number, and b is
the imag inary part. Complex numbers are not used much in Python prog ramming .

Examples:
Here are some examples of numbers:

int long float c omplex

10 51924361L 0.0 3.14j

100 -0x19323L 15.20 45.j

-786 0122L -21.9 9.322e-36j

080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j

-0490 535633629843L -90. -.6545+0J

-0x260 -052318172735L -32.54e100 3e+26J

0x69 -4721885298529L 70.2-E12 4.53e-7j

Python allows you to use a lowercase L with long , but it is recommended that you use only an uppercase L
to avoid confusion with the number 1. Python displays long integ ers with an uppercase L.
A complex number consists of an ordered pair of real floating point numbers denoted by a + bj, where a is
the real part and b is the imag inary part of the complex number.

Number Type Conversion:


Python converts numbers internally in an expression containing mixed types to a common type for evaluation. But
sometimes, you'll need to coerce a number explicitly from one type to another to satisfy the requirements of an
operator or function parameter.

T ype int(x)to convert x to a plain integ er.

T ype long (x) to convert x to a long integ er.

T ype float(x) to convert x to a floating -point number.

T ype c omplex(x) to convert x to a complex number with real part x and imag inary part zero.

T ype c omplex(x, y) to convert x and y to a complex number with real part x and imag inary part y. x and
y are numeric expressions

Mathematical Functions:
Python includes following functions that perform mathematical calculations.

Func tion Returns ( desc ription )

abs(x) T he absolute value of x: the (positive) distance between x and zero.

ceil(x) T he ceiling of x: the smallest integ er not less than x

cmp(x, y) -1 if x < y, 0 if x == y, or 1 if x > y

exp(x) T he exponential of x: e x

fabs(x) T he absolute value of x.

floor(x) T he floor of x: the larg est integ er not g reater than x

log (x) T he natural log arithm of x, for x> 0

log 10(x) T he base-10 log arithm of x for x> 0 .

max(x1, x2,...) T he larg est of its arg uments: the value closest to positive infinity

min(x1, x2,...) T he smallest of its arg uments: the value closest to neg ative infinity

modf(x) T he fractional and integ er parts of x in a two-item tuple. Both parts have the same
sig n as x. T he integ er part is returned as a float.

pow(x, y) T he value of x**y.

round(x [,n]) x rounded to n dig its from the decimal point. Python rounds away from zero as a
tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.

sqrt(x) T he square root of x for x > 0

Random Number Functions:


Random numbers are used for g ames, simulations, testing , security, and privacy applications. Python includes
following functions that are commonly used.
Func tion Desc ription

choice(seq) A random item from a list, tuple, or string .

randrang e ([start,] stop A randomly selected element from rang e(start, stop, step)
[,step])

random() A random float r, such that 0 is less than or equal to r and r is less than 1

seed([x]) Sets the integ er starting value used in g enerating random numbers. Call this
function before calling any other random module function. Returns None.

shuffle(lst) Randomizes the items of a list in place. Returns None.

uniform(x, y) A random float r, such that x is less than or equal to r and r is less than y

Trig onometric Functions:


Python includes following functions that perform trig onometric calculations.

Func tion Desc ription

acos(x) Return the arc cosine of x, in radians.

asin(x) Return the arc sine of x, in radians.

atan(x) Return the arc tang ent of x, in radians.

atan2(y, x) Return atan(y / x), in radians.

cos(x) Return the cosine of x radians.

hypot(x, y) Return the Euclidean norm, sqrt(x*x + y*y).

sin(x) Return the sine of x radians.

tan(x) Return the tang ent of x radians.

deg rees(x) Converts ang le x from radians to deg rees.

radians(x) Converts ang le x from deg rees to radians.

Mathematical Constants:
T he module also defines two mathematical constants:

Constants Desc ription

pi T he mathematical constant pi.

e T he mathematical constant e.

You might also like