0% found this document useful (0 votes)
2 views2 pages

Python Functions (Buit-In, Module)

The document explains the concept of functions in Python, describing them as reusable code blocks that perform specific tasks. It categorizes functions into three types: built-in functions, module functions, and user-defined functions. Examples of using the math and random modules are provided to demonstrate how these functions can be utilized in code.

Uploaded by

Rana Naveed
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)
2 views2 pages

Python Functions (Buit-In, Module)

The document explains the concept of functions in Python, describing them as reusable code blocks that perform specific tasks. It categorizes functions into three types: built-in functions, module functions, and user-defined functions. Examples of using the math and random modules are provided to demonstrate how these functions can be utilized in code.

Uploaded by

Rana Naveed
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

Functions

Function is a piece of code that performs a specific task whenever it is called.


(In a tv remote, each button performs a functions, so a function is like that
button in code) A function is a block of organized, reusable code that is used
to performa single, related action. There are three types of functions in
python;

1. Buit-in Function: int( ) str( ) float( ) range( )


2. Module Function: Module is a file that contains some functions & variables which can
be imported for use in other files.
3. User Defined Function (UDF): Created by user where required during code

Module Function

In [1]: import math


print(dir(math))

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'a


sin', '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', 'isi
nf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'm
odf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'si
n', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

In [2]: [Link](4)

Out[2]: 2.0

In [3]: [Link](2, 8)

Out[3]: 256.0

In [4]: [Link](5)

Out[4]: 120

In [5]: [Link]([Link](90))

Out[5]: 1.0

In [6]: [Link]([Link]/2)

Out[6]: 1.0

In [7]: import random


print(dir(random))
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRand
om', 'TWOPI', '_ONE', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__',
'__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumu
late', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_floor', '_index', '_ins
t', '_isfinite', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sq
rt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choi
ces', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvar
iate', 'normalvariate', 'paretovariate', 'randbytes', 'randint', 'random', 'randrang
e', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvaria
te', 'weibullvariate']

In [8]: # Program to generate a random number between 0 and 6


[Link](0,6)

Out[8]: 0

In [9]: # Declare a list


i = [1, 2, 3, 4, 5, 6]

print("Original list : ", i)

# After shuffle
[Link](i)
print("\nAfter shuffle : ", i)

Original list : [1, 2, 3, 4, 5, 6]

After shuffle : [4, 6, 3, 1, 2, 5]

You might also like