Python Functions

Functions can reduce the program smaller by eliminating repetitive code. Any point of time, if you make a change, you just change it in one place. SO, creating function allows to name a group of statements, which makes your program easier to read, understand, and debug.  Basically, when you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name. We have already seen one example of a function call:

>> type(10)
<class 'int'>

Here the name of the function is type. The expression in parentheses is called the argument of the function. The argument is a value or variable that we are passing into the function as input to the function. The result, for the type function, is the type of the argument. A function may “returns” a result. The result is called the return value.

Define  function and function call:

def is a keyword that indicates that this is a function definition. A function definition specifies the name of a new function and the sequence of statements that execute when the function is called. Here is an example:

def print_myname():
    print("I'm Mr. K.")
    print('I am from city Y.')

Here is the syntax for calling them:


Parameters and arguments:

Inside the function, the arguments are assigned to variables called parameters. Here is an example of a user-defined function that takes an argument:

def add(a, b):
    add1 = a + b
    return add1
x = add(3, 5)
Try it Yourself

Built-in functions in Python:

Python provides a number of important built-in functions. Those built-in functions can be used without providing the function definition. Few examples are given below.

print(len("Hi! I am Mr. K"))

Type conversion functions:

Sometimes you need to convert values from one type to another. Python also provides built-in functions for that. For example, The int function takes any value and converts it to an integer, if it can, or give errors otherwise:


print(int("Hello! I am Mr. K")) # You will get ValueError for this.


float converts integers and strings to floating-point numbers:



Similarly, str converts its argument to a string:



Try it Yourself

Random numbers:

To create random numbers in python you can use random() function which returns a random float between 0.0 and 1.0 (including 0.0 but not 1.0). Each time you call random, you get the next number in a long series.
Check the following example to produce  5 random numbers.

import random
for i in range(5):
    x = random.random()
randint() Function in Python:

There is another function called randint which takes the parameters low and high, and returns an integer between low and high (including both).

random.randint(5, 10)

random.randint(15, 20)

choice() Function in Python:

To choose an element from a sequence at random, you can use choice:

l= [1, 2, 3,4,5]


Try it Yourself

Math functions in Python:

Python provides math module for mathematical functions. Before you can use the module, you have to import it:

import math
print(math.sqrt(16) / 4.0)

Try it Yourself

Conditional Execution