0% found this document useful (0 votes)
6 views3 pages

Def Function

The document provides examples of defining and calling functions in Python, including greeting functions and arithmetic operations. It also covers the use of library functions such as print(), sqrt(), and pow() from the math module, demonstrating how to import and use them. Additionally, it shows how to import the math module in various ways to access constants like pi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Def Function

The document provides examples of defining and calling functions in Python, including greeting functions and arithmetic operations. It also covers the use of library functions such as print(), sqrt(), and pow() from the math module, demonstrating how to import and use them. Additionally, it shows how to import the math module in various ways to access constants like pi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like