Question 1: Sum all List elements
Write a Python function to sum all elements in a list which is passed as parameter and return the sum value.
pAnswer
#function to sum list
def Lsum(L):
s=0
for i in L:
s=s+i
return s
#call the function
sales=[15000,20000,3000,12000,5000,8900]
print(Lsum(sales))
Question 2: Find Factorial Value
The factorial of a positive integers n is defined as n x (n-1) x (n-2) x ......x 3 x 2 x1. Write a python function to return the factorial value of a given integer.
Answer
#function to find factorial value to a given number
def fact(n):
Fact=n
while (n>1):
n=n-1
Fact=Fact*n
return Fact
#call the function
N=int(input("Enter a number?"))
print(fact(N))
Question 3: Read a text file process it and write into other text file
Write a Python program to transform the data from in.csv file content to out.csv file. Content of the in.csv file is given below. The out.csv file should have total value as additional data.
Peter,80,90
David,100,75
Black,50,60
Answer
f1=open("in.txt","r")
f2=open("out.csv","w")
for line in f1:
items=line.strip().split(",")
tot=int(int(items[1])+int(items[2]))
print(items[0],items[1],items[2],tot,file=f2)
f1.close()
f2.close()
Question 4: Find out whether a given number is odd / even algorithm, Pseudocode and Python
- Describe how you would determine whether a given positive integer is odd or even
- Develop a flowchart to represent an algorithm, based on the method suggested in (i) above, to decide whether a given positive integer is odd or even.
- Convert the flowchart you have obtained for the above (ii) into a pseudo code.
- Convert the pseudo code obtained in (iii) into a Python program.
Answers i) Divide the given integer by 2 and get the remainder. If the remainder is 0 then the number is even otherwise the number is odd. (note: if we divide any number by 2 then remainder will be 0 or 1 only) ii)
iii)
Pseudo code
Start
input N
R=N%2 #divide the number by 2 and get the remainder to R
if (R=1) then
display "the number is odd"
else
display "The number is even"
endif
End
iv)
#Python Program
N=int(input("Enter a positive Integer?"))
R=N %2
if (R==1):
print("The number is odd")
else:
print("The number is even")
Sample Output
Enter a positive Integer?101
The number is odd
Enter a positive Integer?27
The number is odd
Question 5: Find the grade for a given mark nested if exercise
Write a Python profgram to find a grade for a given mark
pAnswer
x=int(input("Enter a mark?"))
grade=""
if x lt; 35:
grade="W"
elif x lt; 50:
grade="S"
elif x lt;65:
grade="C"
elif x lt;75:
grade="B"
else:
grade="A"
print("Grade=",grade)
Question 7: Print the Fibonacci Series upto a given number
Write a Python program to print a Fibonacci numbers less than a given number
pAnswer
n = int(input("Enter the number of terms: "))
# First two Fibonacci numbers
a = 0
b = 1
print("Fibonacci Series:")
for i in range(n):
print(a, end=" ")
# Calculate next Fibonacci number
c = a + b
a = b
b = c
Question 8: Print all the prime numbers less than 100
Write a Python programn to print prime numbers which are less than 100
Answer
x=2
while x lt;=100:
prime=True
y=2
while y lt lt;x-1:
if x%y == 0:
prime=False
y=y+1
if prime==True:
print(x)
x=x+1
Question 9: Print the numbers which are lesser than tha average of given numbers
Write a Python programn to print the numbers which are lesser than than averages of the given numbers
Answer
N=int(input("Enter no of times?"))
i=0
tot=0
avg=0
L=[]
while (i lt; N):
M=int(input("Enter a Mark?"))
tot=tot+M
L.append(M)
i=i+1
avg=tot/len(L)
print(avg,tot)
i=0
while (i lt; N):
if (L[i] lt; avg):
print(L[i])
i=i+1
No comments:
Post a Comment