/    /  Python – Functions

Python – Functions:

There are 2 types of functions.

  1. User-defined functions
  2. Pre-defined functions

User-Defined functions:

In Python, User-defined function is a block of code which can reusable. Once they are defined or written, that can be used multiple times and in other applications too.

Generally, the syntax of user-defined functions is represented by keyword def. Below is the syntax for defining the function without arguments.

Syntax:

def function_name( args ):

            statement 1

            statement 2

            return

Let us create a Function for returning a value of x after processing a loop.

>>> def prim(n):

...     for x in range(2,n):

...             for i in range(2,x):

...                     if x%i==0:

...                             break

...             else:

...                     print(x)

...

Let us call the User-defined Function:

>>>prim(10)

2

3

5

7

Using the argument value as 20:

>>>prim(20)

2

3

5

7

11

13

17

19

Using the argument value as 50:

>>>prim(50)

2

3

5

7

11

13

17

19

23

29

31

37

41

43

47

Pre-defined Functions:

Pre-defined functions are already existing functions which cannot be changed. But still we can make our own custom functions using those pre-defined functions.

abs()

all()

any()

ascii()

bin()

bool()

bytearray()

bytes()

callable()

chr()

classmethod()

compile()

complex()

delattr()

dict()

dir()

divmod()

enumerate()

eval()

exec()

filter()

float()

format()

frozenset()

getattr()

globals()

hasattr()

hash()

help()

hex()

id()

input()

int()

isinstance()

issubclass()

iter()

len()

list()

locals()

map()

max()

memoryview()

min()

next()

object()

oct()

open()

ord()

pow()

print()

property()

range()

repr()

reversed()

round()

set()

setattr()

slice()

sorted()

staticmethod()

str()

sum()

super()

tuple()

type()

vars()

zip()

__import__()