0% found this document useful (0 votes)
2 views4 pages

Ani 9

The document outlines various user-defined functions in Python, demonstrating their creation and usage. It includes examples such as greeting a user, performing arithmetic operations, checking if a number is odd or even, reversing a string, and determining if a number is prime. Each function is illustrated with code snippets and sample outputs.

Uploaded by

anya jadhav
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)
2 views4 pages

Ani 9

The document outlines various user-defined functions in Python, demonstrating their creation and usage. It includes examples such as greeting a user, performing arithmetic operations, checking if a number is odd or even, reversing a string, and determining if a number is prime. Each function is illustrated with code snippets and sample outputs.

Uploaded by

anya jadhav
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/ 4

Experiment No-9

Title - User Define Functions In Python


Name – Anirudha Jadhav
Class - SY Div- B
Batch – S2
Roll No - 42
User-defined functions
# creating a function
def fun():
print("Hello World")

# calling a function
def fun():
print("Hello World")
fun()

Hello World

# function to greet a user


def greet(name):
print("Hello, " + name + "!")
greet("Omkar")

Hello, Omkar!

# arguments it is also known as parameter


# u can pass as many as arguments and separate them by comma here
f_name is
def fun(f_name):
print(f_name + " is good boy")
fun("Rushi")
fun("Ravi")
fun("Omkar")
Rushi is good boy
Ravi is good boy
Omkar is good boy

# function to adding two numbers


def add_num(a,b):
result = a + b
print("The sum is:" ,result)
add_num(10,5)

The sum is: 15

# function to substract two numbers which user enters


def sub_num(a,b):
result = a - b
print("The substraction is:", result)
num1=int(input("Enter a first number:"))
num2=int(input("Enter a second number:"))
sub_num(num1,num2)
Enter a first number: 10
Enter a second number: 5

The substraction is: 5

# function to check number is odd or even


def oddeven(x):
if(x % 2==0):
print("even")
else:
print("odd")
num=int(input("Enter a number:"))
oddeven(num)

Enter a number: 10

even

# function to reverse a string


def reverse_string(s):
print("Reversed string:", s[::-1])
text = input("Enter a string: ")
reverse_string(text)

Enter a string: Omkar

Reversed string: teknaS

# function to check number is prime or not


def is_prime(n):
if n <= 1:
print("Not Prime")
return
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
print("Not Prime")
return
print("Prime Number")
num = int(input("Enter a number: "))
is_prime(num)

Enter a number: 3

Prime Number

Whole Program
# creating a function
def fun():
print("Hello World")
# calling a function
def fun():
print("Hello World")
fun()
# function to greet a user
def greet(name):
print("Hello, " + name + "!")
greet("Omkar")
# arguments it is also known as parameter
# u can pass as many as arguments and separate them by comma here
f_name is
def fun(f_name):
print(f_name + " is good boy")
fun("Rushi")
fun("Ravi")
fun("Omkar")
# function to adding two numbers
def add_num(a,b):
result = a + b
print("The sum is:" ,result)
add_num(10,5)
# function to substract two numbers which user enters
def sub_num(a,b):
result = a - b
print("The substraction is:", result)
num1=int(input("Enter a first number:"))
num2=int(input("Enter a second number:"))
sub_num(num1,num2)
# function to check number is odd or even
def oddeven(x):
if(x % 2==0):
print("even")
else:
print("odd")
num=int(input("Enter a number:"))
oddeven(num)
# function to reverse a string
def reverse_string(s):
print("Reversed string:", s[::-1])
text = input("Enter a string: ")
reverse_string(text)
# function to check number is prime or not
def is_prime(n):
if n <= 1:
print("Not Prime")
return
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
print("Not Prime")
return
print("Prime Number")
num = int(input("Enter a number: "))
is_prime(num)

Hello World
Hello, Omkar!
Rushi is good boy
Ravi is good boy
Omkar is good boy
The sum is: 15

Enter a first number: 10


Enter a second number: 7

The substraction is: 3

Enter a number: 10

even

Enter a string: Omkar

Reversed string: teknaS

Enter a number: 6

Not Prime

You might also like