Professional Documents
Culture Documents
PQP1
PQP1
BTL4 Analyze
BTL5 Evaluate
BTL1 Remember
BTL4 Analyze
5. Write a Python program to accept two numbers, find the greatest and print the result.
print(“Enter two numbers”)
val1=int(input()) val2=int(input())
else: BTL5 Evaluate
largest=val2
print(“Largest of two numbers is:”,largest)
6. Do loop statements have else clauses? When will it be executed?
Loop statements may have an else clause, it is executed when the loop terminates
through exhaustion of the list (with for) or when the condition becomes false (with BTL1 Remember
while), but not when the loop is terminated by a break statement.
10. Write a Python script to display the current date and time.
import datetime print(“date and time”, datetime.datetime.now()) BTL5 Evaluate
Part B (5 x 16 = 80 Marks)
11. a. (i) Sketch a flow chart to accept three distinct numbers, find the greatest and
print the result. (8)
BTL3
Apply
BTL3
Apply
(ii) Sketch a flow chart to find the sum of the series 1+2+3+……+100. (8)
or
b. Outline the Towers of Hanoi problem. Suggest a solution to the Towers of
Hanoi problem with relevant diagrams. (16)
BTL4
Analyze
Example
# The value of n for rotation position
num = 4
BTL5
# Create a List Evaluate
myList =[5, 20, 34, 67, 89, 94, 98, 110] BTL2
Understand
print("List before rotation = ",myList)
newlist = []
Output
List before rotation = [5, 20, 34, 67, 89, 94, 98, 110]
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]
(ii)Discuss about keyword arguments and default arguments in python with example. (8)
or
b.(i)Write a python program to print the maximum among „n‟ randomly
generate „d‟ numbers by storing them in a list. (10)
import random
Output:
Random integers between 0 and 9:
4
7
2 BTL5
8
4
Evaluate
6 BTL4
2 Analyze
3
1
5
3
13. a. (i) Write a Python program to find the factorial of the given number without
recursion with recursion. (10)
The factorial of a positive integer n is the product of all positive integers less than or equal to n. The factorial
of a number is represented by the symbol "!" . For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
Here is a Python function that calculates the factorial of a given number using a for loop:
def factorial(n):
if n < 0:
return None
if n == 0:
return 1
result = 1
for i in range(1, n+1):
result *= i
return result
This function takes a single argument, n, and returns the factorial of n. It first checks if n is less than 0, if so it
returns None. If n is equal to 0, it returns 1 (since the factorial of 0 is defined to be 1). It then initializes a BTL5
variable named result to 1, then uses a for loop to iterate over the range of integers from 1 to n inclusive and
Evaluate
multiply each number to result variable. Finally, the function returns the value of result, which is the factorial
of n. BTL5
Evaluate
You can call this function and pass in any positive integer to calculate its factorial:
>>> factorial(5)
120
>>> factorial(3)
6
>>> factorial(10)
3628800
Alternatively python has math.factorial function which you can use without writing your own function.
import math
math.factorial(5)
Do note that factorial of number can be very large, even for relatively small numbers and python integers
may not be large enough to store those values.
Output:
(ii)Write a Python program to generate first „N‟ Fibonacci series numbers. (6)
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....
The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This
means to say the nth term is the sum of (n-1)th and (n-2)th term.
Source Code
# Program to display the Fibonacci sequence up to n-th term
or
Downloaded by NADHIYA S (nadhiya.spkd@gmail.com) BTL5
lOMoARcPSD|11981679
b. (i) Write a python code to search a string in the given list. (6) Evaluate
BTL2
When it is required to find the string in a list, a simple „if‟ condition along with „in‟ operator Understand
can be used.
Example
Below is a demonstration of the same
my_list = [4, 3.0, 'python', 'is', 'fun']
print(my_list)
key = 'fun'
print(key)
if key in my_list:
else:
Output
The list is :
[4, 3.0, 'python', 'is', 'fun']
The key is :
fun
The result is :
The key is present in the list
Explanation
A list of integers and strings is defined and is displayed on the console.
A value for key is defined and is displayed on the console.
An „if‟ loop is used to check if the key is present in the list.
If yes, the result is displayed on the console.
14. a. (i) Write a Python program to perform linear search on a list. (7)
BTL2
Understand
BTL3
Apply
(ii) Demonstrate with code the various operations that can be performed on tuples. (8)
or
b.Write a Python program for (i) merge sort (10) (ii) quick sort (6)
BTL5
Evaluate
15. a. (i).Describe how exceptions are handled in python with necessary examples. (10)
BTL2
Understand
(ii) Discuss about the use of format operator in file processing. (6)
or
b. Mention the commands and their syntax for the following: get current directory, changing directory,
list, directories and files, make a new directory, renaming and removing directory. (16)
print(os.getcwd())
# change directory
os.chdir('C:\\Python33')
print(os.getcwd())
BTL2
Output: C:\Python33 Understand
Here, we have used the chdir() method to change the current working directory and passed a new path
as a string to chdir().
print(os.getcwd())
C:\Python33
['$RECYCLE.BIN',
'Movies',
'Music',
'Photos',
'Series',
'System Volume Information']
os.listdir()
['test']
os.listdir()
['test']
# rename a directory
os.rename('test','new_one')
os.listdir()
['new_one']
Here, 'test' directory is renamed to 'new_one' using the rename() method.