def greet():
print('Hello World!')
# call the function
greet()
print('Outside function')
def greet(name):
print("Hello", name)
# pass argument
greet("Rahul")
# function with two arguments
def add_numbers(num1, num2):
sum = num1 + num2
print("Sum: ", sum)
# function call with two values
add_numbers(5, 4)
# function definition
def find_square(num):
result = num * num
return result
# function call
square = find_square(3)
print('Square:', square)
# function definition
def find_square(num):
result = num * num
return result
# function call
square = find_square(3)
print('Square:', square)
def future_function():
pass
# this will execute without any action or error
future_function()
Some Python library functions are:
1. print() - prints the string inside the quotation marks
2. sqrt() - returns the square root of a number
3. pow() - returns the power of a number
These library functions are defined inside the module. And to use them, we
must include the module inside our program.
For example, sqrt() is defined inside the math module.
import math
____________________________________________________________________________
# sqrt computes the square root
square_root = math.sqrt(4)
print("Square Root of 4 is",square_root)
# pow() comptes the power
power = pow(2, 3)
print("2 to the power 3 is",power)
import standard math module
import math
# use math.pi to get value of pi
print("The value of pi is", math.pi)
# import module by renaming it
import math as m
print(m.pi)
# Output: 3.141592653589793
import only pi from math module
from math import pi
print(pi)
# Output: 3.141592653589793