Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20
Built-in Functions
• A function is a named sequence of statement(s) that
performs a computation. • It contains line of code(s) that are executed sequentially from top to bottom by Python interpreter. • They are the most important building blocks for any software development in Python. • Built in functions are the functions that are built into Python Standard Library and can be accessed directly. • Functions usually have arguments and return a value. Built-in Functions • Some built in functions are given below with examples. • abs(x) returns the absolute value of x . • abs(-45) will return 45 • max(x,y,z) returns the maximum of x,y,z . • max(10,20,30) will return 30 • min(x,y,z) returns the minimum of x,y,z . • min(10,20,30) will return 10 • divmod(x,y) returns both the quotient and remainder . • divmod(14,5) will return (2,4) • round(x,n) round x to n digits. • round(3.14567,2) will return 3.15 Built-in Functions • Some built in functions are given below with examples. • range(start,stop,step) will return a list from start to stop-1 with an increment of step. • range(10) will return [0,1,2,…9] • range(1,10,2) will return [1,3,5,7,9] • type(x) will return the type of the variable object x. • dir(x) will display the details of the object x. • len(x) will return the length of the object. • int(),float(),str(),bool(), these functions can be used for type conversions. • bin(x), oct(x), hex(x) these functions will convert the decimal number x into corresponding base. Python’s Modules What are modules? • A module is a file consisting of Python code. • It can define functions, classes, and variables, and can also include runnable code. • Any Python file can be referenced as a module. • Definitions of functions and variables are not saved when interpreter is exited • Modules allow definitions to be saved for later access • Modules also allow statements to be run as executable scripts • Modules are files with .py extension Purpose of Modules • As our program grows more in the size we may want to split it into several files for easier maintenance as well as re-usability of the code. The solution to this is Modules. • We can define your most used functions in a module and import it, instead of copying their definitions into different programs. • A module can be imported by another program to make use of its functionality. • This is how you can use the Python standard library as well. Types of Modules • Python provides us with some built-in modules, which can be imported by using the "import" keyword. • Python also allows us to create your own modules and use them in your programs.
How to use Modules
• There is a Python Standard Library with dozens of built- in modules. • Example for modules : random, statistics, math, datetime, csv How to use Modules • Python math module, This contains factorial, power, and logarithmic functions, but also some trigonometry and constants. How to use Modules How to use Modules How to use Modules
5. math.pi Returns the value of PI How to use Modules How to use Modules
TRIGONOMETRIC AND ANGULAR FUNCTION
sin(), cos(), and tan() functions return the sine, cosine, and tangent of value passed as the argument. Importing modules • Modules are imported by using built-in import command, without the .py extension >>> import modulename
• We've just imported the math module.
• We can use the things within this module by looking up attributes on the math module object. Importing modules • To make access easier, individual definitions within a module may be imported >>> from modulename import func1, func2 • When you import modules this way, you can refer to the functions by name rather than through dot notation • When modules are imported, all statements and definitions will be executed. • We can import specific names from a module without importing the module as a whole. Here is an example. # import only pi from math module from math import pi print("The value of pi is", pi) OUTPUT The value of pi is 3.141592653589793 Importing modules Here, we first call the from keyword, then random for the module. Next, we use the import keyword and call the specific function we would like to use.
Now, when we implement this function within our program,
we will no longer write the function in dot notation as random.randint() but instead will only write randint():
from random import randint
for i in range(10): print(randint(1, 25)) Importing modules from random import randint for i in range(10): print(randint(1, 25)) OUTPUT 6 25 4 18 15 11 4 9 25 7 Importing modules
We can also import multiple attributes as follows:
>>> from math import pi, e >>> pi 3.141592653589793 >>> e 2.718281828459045
•Using the from … import construction allows us to
reference the defined elements of a module within our program’s namespace, letting us avoid dot notation. Importing modules
We can import all names(definitions) from a module using the
following construct: # import all names from the standard module math
>>>from math import *
>>>pow(2,3) 8 Aliasing Modules • It is possible to modify the names of modules and their functions within Python by using the as keyword. • The construction of this statement looks like the following:
import [modulename] as [another_name]
Example code : import math as m print(m.pi) print(m.e)
Within the program, we now refer to the pi constant as m.pi
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More