PSPP Two Marks Faq-First-Two-Units-2022
PSPP Two Marks Faq-First-Two-Units-2022
PSPP Two Marks Faq-First-Two-Units-2022
An algorithm helps human to understand how Program helps Computer to understand how
to solve the problem in computer to solve the problem
Algorithm cannot be directly executed in the Program can be executed in the computer
computer
Algorithms can be written using flowchart and Programs are written using different
pseudo code programming language
5. Whether following function definition is correct? If not correct then explain the reason:
def def show(a=10,b):
c=a*b print(c)
#main program
mul(10,20)
Answer:
The above function definition is not correct. The reason given below:
Syntax Error: Non-default argument cannot appear after default-argument
mutable object
Mutable object can be modified or altered, after its creation
Example of mutable type includes:
o list
o set
o dictionary
executing rest of the iteration of loop place, without executing rest of the instructions in
the the body of loop
Example: Example:
for i in for i in range(1,10):
range(1,10): if i==5:
if i==5: continue
break print(i,end=”\t”)
print(i,end=”\t”) output:
output: 1 2 3 4 6 7 8 9
1 2 3 4
Answer:
None
Reason: All non-fruitful functions return special value called None to the calling program
12. Find the output of following python expression using operator precedence:
>>>100//5+2-2*min(10,20,5,3)**2
Answer:
4
Keywords are the reserved words in Python that have specific meanings and
purposes
We cannot use a keyword as a variable name, function name or any other
identifier.
In Python, keywords are case sensitive
What are the rules and guidelines used for writing pseudo code?
i) Write only one statement per line: Each stmt in your pseudo code should express just one
action for the computer
ii) Capitalize initial keyword: The following are just a few keywords we will use in pseudo code:
READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT, UNTIL . In the following pseudo
code, READ and WRITE are in capital letters:
READ name, hourlyRate, hoursWorked, deductionPercent
iii) indent the statements to show hierarchy: This will improve the readability of pseudo code to
a great extent
SEQUENCE: keep all statements in the same column
SELECTION: indent the statements that fall inside the selection structure
ITERATION: indent the statements that fall inside the loop
iv) End multiline structures: Multiline structures like control structures and looping structure
must end properly. For example IF structure should ends with END IF
Example:
• IF...END IF
• WHILE ... END WHILE
• FOR ... NEXT
v) Keep statement independent of programming language
14. Write an algorithm to swap value of two variables without using third variable
Step1: START
Step2: READ x, y
Step3: SET x := x + y
Step4: SET y := x - y
Step5: SET x := x - y
Step6: PRINT “After swap x=”, x , “y=”, y
Step7: STOP
• A module is a python file containing definitions of functions, classes, and variables and
executable statements, that can be reused from other programs
• The file name and the module name must be same, extension of file must be .py
• Grouping related code into a module makes the code easier to understand and reuse.
• It also makes the code logically organized
• Example:
>>>import math
>>>print(math.pow(2,5))
32
16. Why string data type is called immutable data type ? Justify your answer?
String is immutable, since once object of string is created, it value cannot be further modified.
Consider the following example:
>>> s="hello"
>>> print(s[0])
h
>>> s[0]='C'
TypeError: 'str' object does not support item assignment
>>>
The above statement will produce TypeError, string is immutable.
result=add(10,20)
print(“10+20=”,result)
output: 10+20=30
21. How to create a list in python? Illustrate the use of negative indexing of list with an example
List is one of the sequence data-type, which is used store more than one value
of different types
Individual elements of list are enclosed in square bracket([]) and separated by
comma.
An example of list object given below:
lst=[10,True, "python”,12.3]
An internal representation of List lst is given below:
Negative Index: -4 -3 -2 -1
Lst 10 True Python 12.3
Positive indexing: 0 1 2 3
Negative indexing are used to individual elements of List from last element to element to first
element:
>>> print(lst[-1])
12.3
>>> print(lst[-4])
10
22. Differentiate list and tuple?
List Tuple
Lists are mutable sequence type tuples are immutable sequence
type
Created using square bracket([]) Created using parenthesis(())
Example Example
lst=[23,True,’test’] record=(23,True,’test’)
Addition, deletion and insertion of Addition, deletion and insertion of
element possible in list element not possible in tuple
List Set
storing of duplicate elements is Element in the set are unique,
possible in list duplicate elements are not allowed
Created using square bracket([]) Created using curly bracket({})
Example: Example:
lst=[23,True,’test’] set1={23,True,’test’}
Order of elements is well defined The order of element is unknown
List can contain any types of object Can contain only hashable object
such as number, Boolean, string,
tuple, but not list object
Object of list are indexable Object of list are not indexeable
PART –B QUESTIONS
1. Steps involved in algorithmic problem solving techniques
2. Explain building blocks of an algorithm?
3. Explain the features of a Python.
4. Outline with an example the assignment operators
5. List various symbols used for drawing flowchar?
6. Explain various data types supported by Python?
7. Explain various operator supported by python?
8. What is the use of comment? What are the various way of writing comments in python
9. What is function? What is function proto types? Classify functions based on its prototypes?
10. Explain various parameter and argument types with suitable example
11. Explain various iterative statements supported by python
12. Explain various control statements supported by python
13. Explain various string handling methods supported by python?
14. Explain methods of List object in python?
IMPORTANT PROGRAMS
1. Write the python program to find the distance between two given points?
PROGRAM
import math
x1=int(input("enter x1? "))
y1=int(input("enter y1? "))
x2=int(input("enter x2? "))
y2=int(input("enter y2? "))
xdist=math.fabs( x2 - x1 )
ydist=math.fabs( y2 - y1)
dist =math.sqrt( xdist ** 2 + ydist ** 2 )
print("The distance is ",dist)
OUTPUT
enter x1? 10
enter y1? 20
enter x2? 10
enter y2? 40
The distance is 20.0
OUTPUT
Enter value for a:10
Enter value for b:20
Before swapping:
a= 10 b= 20
After swapping:
a= 20 b= 10
3. Write a Python program using function to find the sum of first ‘n’ even numbers and
print the result
PROGRAM:
n=eval(input("Enter the value for N:"))
total=0
for i in range(0,(n*2)+1):
total=total+i
OUTPUT:
Enter the value for N:5
4. Write a Python program to find the factorial of a given number without recursion and
with recursion.
PROGRAM USING NON-RECURSION:
def factorial(x):
f=1
for i in range(2,x+1):
f=f*i
return f
#MAIN
n=eval(input("Enter the value for N:"))
print(n,"!=",factorial(n))
OUTPUT:
Enter the value for N:5
5 != 120
#MAIN
n=eval(input("Enter the value for N:"))
print(n,"!=",factorial(n))
OUTPUT:
Enter the value for N:4
4 != 24
OUTPUT:
Enter the value for N:5
the first 5 fibo series are:
0 1 1 2 3
6. Write a python program to find minimum and maximum value in the given array
PROGRAM
lst=eval(input('Enter array?\n'))
minimum=maximum=lst[0]
for x in lst:
if x < minimum:
minimum=x
if x > maximum:
maximum=x
print('The minimum value is',minimum)
print('The maximum value is',maximum)
OUTPUT
Enter array?
[10,1,-3,4,100,45]
The minimum value is -3
The maximum value is 100
7. Write a python program to print all prime numbers between two given numbers
PROGRAM:
n1=eval(input("Enter the value for N1:"))
n2=eval(input("Enter the value for N2:"))
print("All prime numbers between",n1,"and",n2,"are:")
for x in range(n1,n2+1):
for i in range(2,x):
if x%2==0:
break
else:
print(x,end="\t")
OUTPUT:
Enter the value for N1:10
Enter the value for N2:20
All prime numbers between 10 and 20 are:
11 13 15 17 19