Professional Documents
Culture Documents
From Statement
It is used to get a specific function in the code instead of the complete module file. If we
know beforehand which function(s), we will be needing, then we may use from. For
modules having large no. of functions, it is recommended to use from instead of import.
Its syntax is
Example
Here, we are importing sqrt function only, instead of the complete math module. Now
sqrt( ) function will be directly referenced to. These two statements are equivalent to
previous example.
Note: You normally put all import statement(s) at the beginning of the Python file but
technically they can be anywhere in program.
129
floor( x ) It returns the largest math.floor(-45.17)
integer not greater than x, -46.0
where x is a numeric
math.floor(100.12)
expression.
100.0
math.floor(100.72)
100.0
130
0.0001
math.pow(2, 4)
16.0
math.pow(3, 0)
1.0
131
degrees (x) It converts angle x from math.degrees(3)
radians to degrees, where x 171.887338539
must be a numeric value.
math.degrees(-3)
-171.887338539
math.degrees(0)
0.0
132
randrange It returns a random item >>>random.randrange(100
([start,] stop [,step]) from the given range ,1000,3)
150
Some of the other modules, which you can explore, are: string, time, date
Built in Function
Built in functions are the function(s) that are built into Python and can be accessed by a
programmer. These are always available and for using them, we don‟t have to import
any module (file). Python has a small set of built-in functions as most of the functions
have been partitioned to modules. This was done to keep core language precise.
min( x, y, z, .... ) It returns the smallest of its >>> min(80, 100, 1000)
arguments; where x, y, and 80
z are numeric
>>> min(-80, -20, -10)
variable/expression.
-80
133
divmod (x,y ) Returns both quotient and >>> divmod (14,5)
remainder by division (2,4)
through a tuple, when x is
>>> divmod (2.7, 1.5)
divided by y; where x & y
are variable/expression. (1.0, 1.20000)
134
round( x [, n] ) It returns float x rounded >>>round(80.23456, 2)
to n digits from the 80.23
decimal point, where x and
>>>round(-100.000056, 3)
n are numeric expressions.
-100.0
If n is not provided then x
is rounded to 0 decimal >>> round (80.23456)
digits. 80.0
Apart from these functions, you have already seen the use of the following functions:
Composition
Composition is an art of combining simple function(s) to build more complicated ones,
i.e., result of one function is used as the input to another.
Example
Suppose we have two functions fn1 & fn2, such that
a= fn2 (x)
b= fn1 (a)
Example
Example
degrees=270
135