Comp Science HHW Phython
Comp Science HHW Phython
Comp Science HHW Phython
Q3. Write a python function to calculate the factorial of a number. The function accepts the
number as an argument.
Ans 3. def factorial(num):
fact=1
for i in range(2,num+1):
fact=fact*i
return fact
n=int(input("Enter a number : "))
if n>0:
print("factorial of",n ,"is", factorial(n))
elif n==0:
print("factorial of 0 is 1")
else :
print("Please enter a positive integer")
Q5. Write a Python function that accepts a string and calculate the number of upper case letters
and lower case letters.
Ans 5. def function(str):
countupper=0
countlower=0
for i in range(0,len(str)):
if str[i].isupper():
countupper+=1
elif str[i].islower():
countlower+=1
print("Number of uppercase letters: ", countupper)
print("Number of lowercase letters: ", countlower)
x=input("Enter a string : ")
function(x)
Q6. Write a Python function that takes a list and returns a new list with unique elements of the
first list.
Ans 6. def function():
list1=eval(input("Enter a list: "))
list2=[]
for i in range(len(list1)):
if list1[i] not in list2:
list2.append(list1[i])
print(list2)
function()
Q7. Write a Python function that takes a number as a parameter and check the number is prime or
not.
Ans 7. def prime(num):
for i in range(2,num):
if n%i==0:
print(n,"is not a prime number")
break
else :
print(n, "is prime")
n=int(input("Enter a number: "))
prime(n)
Q8. Write a Python function that checks whether a passed string is palindrome or not.
Ans 8. def palindrome(str):
length=len(str)
mid=length//2
rev=-1
for a in range(mid):
if str[a]==str[rev]:
a+=1
rev-=1
else:
print(str,"is not a palindrome")
else:
print(str,"is a palindrome")
p=input("Enter a string: ")
palindrome(p)
Q9. Write a Python function that prints out the first n rows of Pascal's triangle.
Ans 9. import math
def pascal(n):
for i in range(0,n):
a=math.factorial(i)
for j in range(i+1):
b=math.factorial(j)
c=math.factorial(i-j)
d=int(a/(b*c))
print(d,end="")
print()
x=int(input("Enter no. of lines: "))
pascal(x)
Assignment 2
Q1. Write a function that takes amount in dollars and dollar to rupee conversion price; it then
returns the amount converted to rupees. Create the function in both void and non- void forms.
Ans 1. VOID FORM
def function(dollar,conversion=70):
rupee=dollar*conversion
return rupee
rs=float(input("Enter amount in US Dollars: "))
print("Amount in Indian rupee = ",function(rs))
NONVOID FORM
def function(dollar,conversion=70):
rupee=dollar*conversion
return
rs=float(input("Enter amount in US Dollars: "))
print("Amount in Indian rupee = ",function(rs))
Q2. Write a function to calculate volume of the box with appropriate default values for its
parameters. Later, test it by writing complete program to invoke it.
Ans 2. def volume(l=5,b=6,h=7):
vol=l*b*h
return vol
print("volume", volume())
def volume1(length,width,height):
vol=length*width*height
return vol
length=int(input("Enter length"))
width=int(input("Enter width"))
height=int(input("Enter height"))
print("Volume", volume1(length,width,height))
Q3. Write a program to have following functions. (i) A function that takes a number as argument
and calculates cube for it. The function does not return a value. If there is no value passed to the
function in function call, function should calculate cube of 2. (ii) A function that takes two char
arguments and returns true if both the arguments are equal, and false otherwise. (iii) Test both
these functions by giving appropriate function call statements.
Ans 3.
#1
def f(x=2):
cube=x**3
return
n=int(input("enter a number: "))
print("The cube of the number is", f(n))
#2
def a(x,y):
if x==y:
return ("True")
else :
return("false")
b=input("Enter: ")
c=input("Enter: ")
print(a(b,c))
Q4. Write a function that receives two numbers and generates a random number from that range.
Using this function, the main program should be able to print three numbers randomly.
Ans 4. import random
def f(x,y):
return(random.randrange(a,b))
a=float(input("Enter lower limit: "))
b=float(input("Enter upper limit: "))
for i in range(3):
print(f(a,b),end=" ")
Q5. Write a function that receives two string arguments and checks whether they are same length
strings. Returns true in this case otherwise false.
Ans 5. def f(a,b):
la=len(a)
lb=len(b)
if la==lb:
return("True")
else :
return("False")
m=input("Enter string: ")
n=input("Enter string: ")
print(f(m,n))