Practical No 11
X. Exercise
Que 1 - Write a Python function that takes a number as a parameter and check the number
is prime or not.
def test_prime(n):
if(n == 1):
return False
elif(n == 2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True
print(test_prime(int(input("Enter number to check - "))))
Output –
Que 2 - Write a Python function to calculate the factorial of a number (a non-negative
integer). The function accepts the number as an argument.
def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n-1)
n = int(input("Input a number to compute the factorial = "))
print(factorial(n))
Output –
Que 3 - Write a Python function that accepts a string and calculate the number of upper
case letters and lower case letters.
def string_test(s):
d = {"UPPER_CASE":0,"LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print("Original String :- ",s)
print("No. of Upper case Chars :- ",d["UPPER_CASE"])
print("No. of Lower case Chars :- ",d["LOWER_CASE"])
string_test("Shantanu Anant Gaikwad")
Output –