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.
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.
def sample(): #function definition
print ("Hello world")
sample() #function calling
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.
def welcome(name):
print("function with one parameter")
print("welcome : ",name)
welcome("Guest")
function with one parameter
Welcome : Guest
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))
Enter a: 10
Enter b: 15
Sum = 25
The Python supports several built-in functions, those are
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)
x = 14
y = bin(x)
print (y)
0b1110
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)
x = 14
y = hex(x)
print (y)
0xe
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)
x = 14
y = oct(x)
print (y)
0o16
In python ord() function is used to return the ASCII( American Standard Code for Information Interchange) value of a specified character.
Syntax:ord (character)
print("ASCII value of A :",ord('A'))
print("ASCII value of a :",ord('a'))
print("ASCII value of 1 :",ord('1'))
ASCII value of A : 65
ASCII value of a : 97
ASCII value of 1 : 49
In python chr() function is used to return the character of a specified ASCII( American Standard Code for Information Interchange) value.
Syntax:chr (num)
print("Character value of 65 :",chr(65))
print("Character value of 97 :",chr(97))
print("Character value of 50 :",chr(50))
Character value of 65 : A
Character value of 97 : a
Character value of 50 : 2
In python eval() function is used to evaluate given expression. The expression must in quotes.
Syntax:eval(expression
x=int(input("Enter x :"))
y=int(input("Enter y :"))
print(eval('x+y'))
Enter x :13
Enter y :33
46
In python abs() function is used to return absolute value of given number.
Syntax:abs(num)
x=-13
y=-22.15
print("Absolute value of x :",abs(x))
print("Absolute value of y :",abs(y))
Absolute value of x : 13
Absolute value of y : 22.15