0% found this document useful (0 votes)
4 views

Function Practice

Ibmputhon document

Uploaded by

Shaik Sabeha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Function Practice

Ibmputhon document

Uploaded by

Shaik Sabeha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

11/20/23, 9:47 AM Function.

ipynb - Colaboratory

# 1. Create a function that prints "Hello, World!" when


called. def greet():
print("Hello, World!")

greet()

# 2. Write a function that takes two parameters and returns their


sum. def add(a, b):
return a + b

result = add(3, 5)
print(result)

# 3. Define a function that greets a person by name, with a default name of "Guest."

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

# Call the function

greet()
greet("Alice")

Hello, Guest!
Hello, Alice!

https://colab.research.google.com/drive/1tHLCGC-JNHZtHTreKR7-O3Q264-QlqVe#scrollTo=5YI1ft9d6lMa&printMode=true 1/7
11/20/23, 9:47 AM Function.ipynb - Colaboratory

# 4. Create a function that returns both the square of a given number.

def find_square(x):
return x**2

# Call the function


square = find_square(2)
print(square)

# 5. Create a function that checks if a number is even or odd.

def even_or_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"

# Call the function


result = even_or_odd(7)
print(result)

https://colab.research.google.com/drive/1tHLCGC-JNHZtHTreKR7-O3Q264-QlqVe#scrollTo=5YI1ft9d6lMa&printMode=true 2/7
11/20/23, 9:47 AM Function.ipynb - Colaboratory
# 6. Write a function that calculates the area of a rectangle given its length and width.

def calculate_rectangle_area(length, width):


return length * width

# Call the function


area = calculate_rectangle_area(4, 6)
print(area)

24

# 7. Write a function that raises a number to a given power.

def power(base, exponent):


return base ** exponent

# Call the function


result = power(3, 3)
print(result)

27

https://colab.research.google.com/drive/1tHLCGC-JNHZtHTreKR7-O3Q264-QlqVe#scrollTo=5YI1ft9d6lMa&printMode=true 3/7
11/20/23, 9:47 AM Function.ipynb - Colaboratory
# 8. Create a function that checks if a number is positive, negative, or zero.

def check_number(value):
if value > 0:
return "Positive"
elif value < 0:
return "Negative"
else:
return "Zero"

# Call the function


result = check_number(-5)
print(result)

Negative

# 9. Define a function that finds the maximum of three numbers.

def find_max(a, b, c):


return max(a, b, c)

# Call the function


result = find_max(8, 3, 11)
print(result)

https://colab.research.google.com/drive/1tHLCGC-JNHZtHTreKR7-O3Q264-QlqVe#scrollTo=5YI1ft9d6lMa&printMode=true 4/7
11/20/23, 9:47 AM Function.ipynb - Colaboratory
# 10. Write a function that returns the length of a given string.

def string_length(a):
return len(a)

# Call the function


length = string_length("Hello, World!")
print(length)

13

# 11. Write a function that calculates the perimeter of a rectangle given its length and width.

def calculate_rectangle_perimeter(length, width):


return 2 * (length + width)

# Call the function


perimeter = calculate_rectangle_perimeter(4, 6)
print(perimeter)

20

https://colab.research.google.com/drive/1tHLCGC-JNHZtHTreKR7-O3Q264-QlqVe#scrollTo=5YI1ft9d6lMa&printMode=true 5/7
11/20/23, 9:47 AM Function.ipynb - Colaboratory
# 12. Create a function that checks if a number is prime.

def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True

# Call the function


result = is_prime(11)
print(result)

# 13. Define a function that finds the minimum of three numbers.

def find_min(a, b, c):


return min(a, b, c)

# Call the function


result = find_min(8, 3, 11)
print(result)

https://colab.research.google.com/drive/1tHLCGC-JNHZtHTreKR7-O3Q264-QlqVe#scrollTo=5YI1ft9d6lMa&printMode=true 6/7
11/20/23, 9:47 AM Function.ipynb - Colaboratory
# 14. Create a function that calculates the factorial of a given number.

def factorial(n):
print("""
if n == 0 or n == 1:
Press 1: Add No.
return 1
Press 2: Delete No.
else:
Press 3: Sum of all No.s
return n * factorial(n-1)
Press 4: Exit
""")
# Call the function
data=[]
result = factorial(5)
s=0
print(result)
while True:
ch=input("Enter your choice: ")
if ch=="1":
num=int(input("Enter no. to add : "))
data.append(num)

elif ch=="2":
num1=int(input("Enter no. to delete: "))
if num1 in data:
data.remove(num1)
print(num1," is deleted sucessfully!!")
print("our database: ",data)
else:
print(num1," is not present in our database??")
elif ch=="3":
for i in data:
s+=i
https://colab.research.google.com/drive/1tHLCGC-JNHZtHTreKR7-O3Q264-QlqVe#scrollTo=5YI1ft9d6lMa&printMode=true 7/7

You might also like