PPS Unit 1 & 2 QB With Solution
PPS Unit 1 & 2 QB With Solution
LOW Level
4. Select the best way to solve problem from the list to alternative solution
5. List instructions that enable you to solve the problem using selected solution
Comment-
Comments are not executable statement in a program. They are just added to describe
the statement in the program code.
In python, any statement written along with # (hash) symbol is
known as a comment. The interpreter does not interpret the comment.
Mutable Data Type – A mutable data type is those whose values can be changed.
Immutable Data Type – An immutable data type is one in which the values can’t be
changed or altered.
• To take input from users, Python makes use of input() function. The input() prompts
the user to provide some information on which the program can work and give the
result.
• However, we must always remember that the input() function takes input as a string.
So whether you input a number or a string, it is treated as a string only.
Enter an value
Example-1 String input
name = input("Enter your name: ")
print("Hello, " + name)
Output Operation:
Python provides the print() function to display output to the standard output devices.
Example-2 integer input
num = int(input("Enter a number: "))
add = num + 1
print(add)
Q.9 Define Algorithm. Explain different features of algorithm.
Answer:
1. The algorithm gives the logic of the program, that is, a step-by-step description of
how to arrive at a solution.
2. Algorithm provides a blueprint to writing a program to solve a particular problem.
3. It is considered to be an effective procedure for solving a problem in a finite
number of steps.
4. That is, a well-defined algorithm always provides an answer, and is guaranteed
to terminate.
Features of Algorithm-
• Features of Python:
• Easy to learn- Structure of program is simple, uses few keywords and clearly defined
syntax.
• Free and open source- Anyone freely distribute it, read the source code, edit, and use
the code to write new program
• High-level language- Programmer does not need to worry about low level details like
managing memory.
• Interactive- It provide interactive shell or environment where you can execute code
interactively and see immediate result.
• Portable- Program behaves same on the wide variety of hardware platform, Program
work on any of the operating system like Windows, Linux, Solaris etc.
• Function oriented and Object oriented- Support both object oriented (Encapsulate
data and functionalities within object) as well as procedure oriented (use functions)
style of programming.
• Dynamic- Python executes dynamically. Program can copy and used for flexible
development of application. If there are any error it is reported at run time.
History of Python:
It has been derived from many languages such as ABC, Modula-3, C, C++, Algol-68,
SmallTalk, Unix shell, and other scripting languages.
When he began implementing Python, Guido van Rossum was also reading the
published scripts from “Monty Python’s Flying Circus”, a BBC comedy series from
the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly
mysterious, so he decided to call the language Python.
Bitwise Operators- It is applicable to int and Boolean type only. Bitwise AND,
Bitwise OR, Bitwise XOR, Bitwise NOT
• Bitwise AND- Ex- 1111 & 0010=0010-> 15 & 2->2
• Bitwise OR- Ex- 1111 | 0010=1111=-> 15 | 2-> 15
• and- Logical and- (a>b) and (b>c)- If whole expression is true then only result is true.
• or- Logical or- (a>b) or (b>c)- Whole expression is true when any one condition is
true
• not- Logical not- Logical not take single expression and negates the value of the
expression.
• in Operator- The operator returns true if a variable found in the specified sequence
otherwise false. Ex s="Python“ -> 'y' in s,->True
• not in Operator- The operator return True if variable not found in specified sequence
otherwise false
Identity Operator- Identity operator compare the memory locations of two objects.
To check two objects are same or not.
• is Operator- Returns True if operands or values on both sides or the operator point to
the same object otherwise false.
• is not Operator: Return True if operands or values on both sides of the operator does
not point to the same object otherwise false.
•
Q.15 Explain Top Down design approach.
• Top-down design is a method of breaking a problem down into smaller, less complex
pieces from the initial overall problem. Most “good” problems are too complex to
solve in just one step, so we divide the problem up into smaller manageable pieces,
solve each one of them and then bring everything back together again.
Numbers- (float)These are real numbers having both integer and fraction parts.
-Ex- 1.4, -5.9, 3.1423
- These may be written in one of the two forms:
1. Fraction form: ex 2.5
2. Exponent form: In exponent form consist of two parts:
1. Mantissa
2. Exponent
-Ex- 2E3, 2 is mantissa, 3 is Exponent
2000.0 can be written as 2 × 103 Mantissa part is 2 and exponent part is 3
Example :
Output-
Example:
i=1
while i <= 3:
i= i + 1
print(“welcome to Python”)
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
• break statement in Python is used to bring the control out of the loop when some
external condition is triggered.
• break statement is put inside the loop body (generally after if condition).
• It terminates the current loop, i.e., the loop in which it appears, and resumes execution
at the next statement immediately after the end of that loop.
• If the break statement is inside a nested loop, the break will terminate the innermost
loop.
• Syntax:
break
• Example
for i in range(10):
print(i)
if i == 2:
break
b)continue
• It returns the control to the beginning of the loop.
• The continue statement rejects all the remaining statements in the current iteration of
the loop and moves the control back to the top of the loop.
• The continue statement can be used in both while and for loops.
Syntax:
continue
• Example 1
var = 5
while var > 0:
var = var -1
if var == 3:
continue
print ('Current variable value :‘,var )
c) pass
• When the user does not know what code to write, So user simply places a pass at that
line.
• If we do not use pass or simply enter a comment or a blank here, we will receive
an IndentationError error message.
Example : 1
n = 26
if n > 26:
# write your code here
pass
print("Python")
O/P:
Second element in list is : 20
O/P:
a is 10
Q.7 What is range() function? Explain with example
Answer:
1. It returns a sequence of numbers, starting from 0 by default and increments by 1 and stops
before a specified number.
2. Syntax:
range(start, stop, step)
3. Start is optional parameter. It specify start position. By default it is 0.
4. Stop is required parameter. It specify the position to stop.
5. Step is optional parameter. It specify increment and by default it will increment by 1.
Example:
x = range(3, 6)
for n in x:
print(n)
O/P
3
4
5
Q.8 Explain different Dictionary methods.
Answer:
1. clear() : It removes all the elements from the dictionary.
e.g.
car = {‘brand’ : ’ford’, ’model’ : ’top’, ‘year’ : 2022}
print(car)
car.clear()
print(car)
O/P :
{'brand': 'ford', 'model': 'top', 'year': 2022}
{}
O/P:
{'brand': 'ford', 'model': 'top', 'year': 2022}
O/P:
{'key1': 0, 'key2': 0, 'key3': 0}
O/P
Top
O/P:
{'brand': 'ford', 'model': 'top', 'year': 2022, 'colour': 'White'}
O/P:
{'brand': 'ford', 'year': 2022}
7. popitem() : It removes last item from dictionary.
e.g.
car = {‘brand’ : ’ford’, ’model’ : ’top’, ‘year’ : 2022}
car.popitem()
print(car)
O/P:
{'brand': 'ford', 'model': 'top'}
Q.9 Explain following data types in python
a)List
b) Tuple
c) Dictionary
Ans-
List :
• If we want group of items in single entity.
• Mutable.
• List is an ordered sequence of items.
• Insertion order is preserved and duplicates is allowed
• e. g. list = [10,20,30,’Python’,10]
Tuple :
• Tuple is an ordered sequence of items same as list. The only difference is that tuples
are immutable. Tuples once created cannot be modified
• e. g. tuple = (1,2,3,1,2,”Python”)
Python Dictionary :
• Mutable
• Dictionary is an unordered collection of key-value pairs.
• If we trying to insert an entry with duplicate key then old value will be replaced with
new value
• dict = {1: ‘Python’, 2:’Java’, 3:’c’}
• print("Complete Dictionary:”, dict)
Q.10 Write a python program to chech whether number is positive, negative or zero.
Q.11 Write a python program to print factorial of a number.
Output-
num1 = 10
num2 = 14
num3 = 12
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3