Comp Science HHW Phython

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Computer Science HHW

Assignment 1 By Arnav Gupta XII-A


Roll no. 8
Q1. Write a python function to multiply all the numbers in a list.
Ans 1. def mul(list):
length=len(list)
prod=1
for i in range(0,length):
prod=prod*list[i]
print("Result= ", prod)
l=eval(input("Enter list : "))
mul(l)

Q2. Write a python program to reverse a string using functions.


Ans 2. def reverse(str):
length=len(str)
for i in range(-1,-length-1,-1):
print(str[i],end="")
a=input("Enter string: ")
reverse(a)

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")

Q4. WAP function to check if a number is in given range.


Ans 4. def function(x,y,z):
if x>=a and y<=b:
print("Yes",x,"is in range")
else :
print("No",x,"is not in range")
a=int(input("Enter any number: "))
b=int(input("Enter lower limit: "))
c=int(input("Enter upper limit: "))
function(a,b,c)

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)

Q10. Explain difference between formal and actual parameter.


Ans 10.

Actual Parameter Formal Parameter


The arguments that are passed in a function call The formal arguments are the
are called actual arguments. parameters/arguments in a function declaration.
Data type not required. But data type should Data types needs to be mentioned.
match with corresponding data type of formal
parameters.
Actual parameters are the parameters which you A formal parameter is a parameter which you
specify when you call the Functions or specify when you define the function.
Procedures.
The actual parameters are passed by the calling The formal parameters are in the called function.
function.

Q11. Write a program using recursion


Ans 11. • Factorial of a number
def factorial(n):
if n==1:
return n
else:
return n*factorial(n-1)
num=int(input("Enter a number: "))
if num<0:
print("Enter positive number")
elif num==0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",factorial(num))

• Fibonacci series till N


def fibonacci(n):
if n<=1:
return n
else:
return (fibonacci(n-1)+fibonacci(n-2))
num=int(input("Enter number of terms: "))
for i in range(num):
print(fibonacci(i),end=" ")

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))

You might also like