Python Menu

Functions in Python


A function can be defined as the organized block of reusable code which can be called whenever required. In other words, Python allows us to divide a large program into the basic building blocks known as function.

Python provide us various inbuilt functions like range() or print(). Although, the user can able to create functions which can be called user-defined functions.

Creating Function

In python, a function can be created by using def keyword.

Syntax:
def my_function():  
    Statements   
    return statement

The function block is started with the colon (:) and all the same level block statements remain at the same indentation.

Example:
def sample():  		#function definition
	print ("Hello world")

sample()  	#function calling
Output:
Hello world

The information into the functions can be passed as the parameters. The parameters are specified in the parentheses. We can give any number of parameters, but we have to separate them with a comma.


Example:
def welcome(name):
	print("function with one parameter")
	print("welcome : ",name)
welcome("Guest")
Output:
function with one parameter
Welcome : Guest
Example:
def sum (a,b):  
    return a+b;  
  
#taking values from the user  
a = int(input("Enter a: "))  
b = int(input("Enter b: "))  
#printing the sum of a and b  
print("Sum = ",sum(a,b))
Output:
Enter a: 10
Enter b: 15
Sum = 25

Built-in Functions in Python

The Python supports several built-in functions, those are

  • bin()
  • hex()
  • oct()
  • ord()
  • eval()
  • chr()
  • abs()
  • int()
  • float()
  • str()
  • list()
  • tuple()
  • set()
  • len()
  • max()
  • min()
  • sum()
  • sorted()
  • input()
  • range()
  • type()

☞ bin()

In python bin() function is used to return the binary representation of a specified integer. A result always starts with the prefix 0b.

Syntax:
bin (num)

Example:
x =  14  
y =  bin(x)  
print (y)
Output:
0b1110

☞ hex()

In python hex() function is used to return the hexadecimal representation of a specified integer. A result always starts with the prefix 0x.

Syntax:
hex (num)

Example:
x =  14  
y =  hex(x)  
print (y)
Output:
0xe

☞ oct()

In python oct() function is used to return the octal representation of a specified integer. A result always starts with the prefix 0o

. Syntax:
oct (num)

Example:
x =  14  
y =  oct(x)  
print (y)
Output:
0o16

☞ ord()

In python ord() function is used to return the ASCII( American Standard Code for Information Interchange) value of a specified character.

Syntax:
ord (character)

Example:
print("ASCII value of A :",ord('A'))
print("ASCII value of a :",ord('a'))
print("ASCII value of 1 :",ord('1')) 
Output:
ASCII value of A : 65
ASCII value of a : 97
ASCII value of 1 : 49

☞ chr()

In python chr() function is used to return the character of a specified ASCII( American Standard Code for Information Interchange) value.

Syntax:
chr (num)

Example:
print("Character value of 65 :",chr(65))
print("Character value of 97 :",chr(97))
print("Character value of 50 :",chr(50))
Output:
Character value of 65 : A
Character value of 97 : a
Character value of 50 : 2

☞ eval()

In python eval() function is used to evaluate given expression. The expression must in quotes.

Syntax:
eval(expression

Example:
x=int(input("Enter x :"))
y=int(input("Enter y :"))
print(eval('x+y'))
Output:
Enter x :13
Enter y :33
46

☞ abs()

In python abs() function is used to return absolute value of given number.

Syntax:
abs(num)

Example:
x=-13
y=-22.15
print("Absolute value of x :",abs(x))
print("Absolute value of y :",abs(y))
Output:
Absolute value of x : 13
Absolute value of y : 22.15

Next Topic :Modules in Python