Function Practice
Function Practice
ipynb - Colaboratory
greet()
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 + "!")
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
def find_square(x):
return x**2
def even_or_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
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.
24
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"
Negative
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)
13
# 11. Write a function that calculates the perimeter of a rectangle given its length and width.
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
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