You are on page 1of 5

Practical No.

10
Q.1. Python maths built-in functions.
import math
num1=int(input("Enter number1:"))
num2=int(input("Enter number2:"))

print("Python Built-in Math Functions:-")


print("Square root of num1:",math.sqrt(num1))
print("Copysign:",math.copysign(num1,num2))
print("Cosine of num2:",math.cos(num2))
print("e**x:",math.exp(num1))
print("Factorial of num1:",math.factorial(num1))
print("Power of num1 to num2:",math.pow(num1,num2))
print("Greatest Common Divisor:",math.gcd(num1,num2))
print("Floor:",math.floor(num1))
print("Log:",math.log(num1,3))
print("Radians:",math.radians(num1))
print("Remainder:",math.remainder(num1,num2))
O/P:-

Q.2. Python string built-in functions.


str="welcome to python programming"
print("String built-in functions:")
print("Converting first character to
uppercase:",str.capitalize())
print("Count number of values in string:",str.count('a'))
print("Splitting the string:",str.split())
print("Replacing the text:",str.replace("python","php"))
str1="GAYATRI"
print("String in lowercase:",str1.lower())
print("String in uppercase:",str.upper())
print("Check whitespaces:",str.isspace())
print("Check string is in lowercase:",str.islower())
print("Check string is in uppercase:",str.isupper())
print("Check if all characters are aplhabets:",str.isalpha())
print("Check if all characters are numeric:",str.isnumeric())
print("Search for the y",str.rindex('y'))
print("Ends with:",str1.endswith('I'))
O/P:-

Exercise Questions:-
Q.1. Write a Python function that accepts a string and calculate the number of upper case letters
and lowercase letters.
str="WelcomeToPythonProgramming"
lower=0
upper=0
for i in str:
if(i.islower()):
lower+=1
else:
upper+=1
print("Total uppercase letters:",upper)
print("Total lowercase letters:",lower)
O/P:-

Q.2. Write a Python program to generate a random float where the value is between 5 and 50
using Python math module.
import random
res=random.uniform(5,50)
print(res)
O/P:-
Practical No.11
Q.1. Write a Python function that takes a number as a parameter and check the number is
prime or not.
def primeNum(num):
if num > 1:
for i in range(2, int(num/2) + 1):
if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")

num = int(input("Enter a number: "))


primeNum(num)
O/P:-

Q.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(num):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while n > 1:
fact *= n
n -= 1
return fact

num = int(input("Enter a number: "))


print("Factorial of", num, "is", factorial(num))
O/P:-

Q.3. Write a Python function that accepts a string and calculate the number of upper case
letters and lower case letters.
def calculateUpperLower(str):
lower = 0
upper = 0
for i in str:
if(i.islower()):
lower+=1
else:
upper+=1
print("Total uppercase letters:",upper)
print("Total lowercase letters:",lower)

str=input("Enter a string:")
calculateUpperLower(str)
O/P:-

You might also like