Built-In Functions
Built-In Functions
References
• Introduction to computing and
problem solving using python by
Balaguruswamy
Rincy T A
Asst Professor in Computer Science
Prajyoti Niketan College, Pudukad
Built-in functions
• Functions that are already defined in
Python Programming Language
• We can directly call them to perform a
specific task
• Example: Math module has some
mathematical built-in functions that
perform taks related to mathematics.
Type conversions
• int function can take any number value and convert it into
an integer
• Example:
>>>int(5.5)
5
>>>int(‘5’)
5
>>>float(45)
45.0
>>>float(‘5’)
5.0
>>>str(67)
‘67’
>>>print(‘Python Version’ + str(2.7))
Python Version2.7
Type coercion
• Implicit conversion
• Automatically done by the interpreter
• It is a process through which the python
interpreter automatically converts a value
of one type into a value of another type
according to the requirement.
• Example
• Suppose we want to calculate an elapsed
fraction of an hour. The expression
minutes/60 does integer arithmetic and
gives result, even 59 minutes past hour.
• One Solution:
>>>minute=59
>>>float(minute)/60
0.983333333
• Another Solution(Implicit)
>>>minute=59
>>>minute/60.0
0.983333333
Mathematical Functions
• First these functions solve the innermost part
of the parenthesis, and then move on to the
outer functions.
• Python provides us a Math module that
contains most of the familiar and important
mathematical functions.
• A module is a file that contains some
predefined Python codes.
• A module can define functions, classes and
variables.
• It is a collection of related functions grouped
together.
• Before using a module, we have to import
it
>>>import math
• This statement creates an object of
module named math.
• If we try to print this object, the
interpreter will give some information
about it.
>>>print math
<module ‘math’ (built-in)>
• There are many predefined functions and
variables that reside under the module
object
• To access these functions:
>>>decibel=10* math.log10(18.0)
>>>angle=2.5
>>>height= math.sin(angle)
>>> print(decibel)
12.552725051033061
>>> print(angle)
2.5
>>> print(height)
0.5984721441039564
>>> d=math.log(18.0)
>>> print(d)
2.8903717578961645
• We have other trignometric functions like
cos, tan, cosec, etc.
• These take values of angle in radians as
arguments
• To convert degrees to radians:
• Find the cos of 45 degrees
>>>degree=45
>>>angle=degree*2*math.pi/360.0
>>>math.cos(angle)
0.7071067811865476
Date and Time
• Built-in modules time and calendar
• Getting current date and time
>>>import time
>>> localt=time.localtime(time.time())
>>> print(localt)
time.struct_time(tm_year=2020, tm_mon=7,
tm_mday=22, tm_hour=8, tm_min=44,
tm_sec=3, tm_wday=2, tm_yday=204,
tm_isdst=0)
Getting formatted
>>>
l=time.asctime(time.localtime(time.time()))
>>> print(l)
Wed Jul 22 08:51:43 2020
Getting calendar for a month
>>> import calendar
>>> c=calendar.month(2015,10)
>>> print(c)
October 2015
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
dir()
• dir() takes an object as an argument.
• It returns a list of strings which are nmaes of members
of that object. If the object is a module, it will list
sub-modules, functions provided by, variables,
constants etc.
• Example:
>>>import math
>>>list=dir(math)
>>>print(list)
['__doc__', '__loader__', '__name__', '__package__', '__spec__',
'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil',
'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc',
'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum',
'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan',
'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf',
'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin',
'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
help()
• help() is a built-in function in Python which is
used to invoke the help system. It takes an
object as an argument.
• Example:
>>>import math
>>>help(math.sin)
Help on built-in function sin in module
math:
sin(x, /)
Return the sine of x (measured in radians).
Thank You