Unit-Ii Question Bank
Unit-Ii Question Bank
UNIT II
CONTROL STRUCTURES AND STRINGS
Course Objective – To understand the control structures and string concepts.
Course Outcome – To develop program using control structure and simple string
processing.
OUTPUT
The product of 20 and 10 = 200
3. What is selection structure? List the different selection control K1
statements in Python.
This control structure enables a program to choose between two or more paths
of execution based on specific conditions. Several conditions are tested and
instructions are executed based on which condition is true. The statements
used for selection structure are also called as decision control statements or
branching statements. They are
• if statement
• if-else statement
• if – elif - else ladder statement
2
• nested if statement
4. Write a python program that accepts two numbers, finds and displays K3
the bigger one of them.
A = int(input("Enter the first number"))
B = int(input("Enter the second number"))
if(A>B):
print(A, "is bigger")
else:
print(B, "is bigger")
Output
Enter the first number80
Enter the second number90
90 is bigger
5. Write a Python program that allows a withdrawal transaction. Assume K3
an available balance amount of Rs. 10,000.
bal = 10000
amt = int(input(“Enter the amount to withdraw: ”))
if (amt >= bal):
print(“Insufficient balance. Withdrawal not possible.”)
else:
bal -= amt
print(“Your balance amount = Rs. ”, bal)
Output:
Enter the amount to withdraw: 8000
Your balance amount = Rs. 2000
6. What is iterative structure? List the different iteration statements in K2
Python.
This control structure allows certain blocks of code to be executed repeatedly
as long as a condition remains true. Iteration statements in python include the
following:
• for loop
• while loop
7. Write the syntax and usage of for loop. K2
A for loop is a control flow statement that executes code repeatedly for a
particular number of iterations. In this control flow statement, the keyword
used is for. The ‘for’ loop is used when the number of iterations is already
known. It has three forms of usage.
(i) for var in range([start],end,[step] ):
statement block
else:
print(“PYTHON”)
11. Differentiate between break and continue statements. K3
Break Continue
The break statement is used to The continue statement is used to
terminate the execution of the stop the current iteration of the loop
nearest enclosing loop in which it and continues with the next iteration
appears.
Syntax: break Syntax: continue
It causes a forward jump in program It causes a backward jump in
execution. program execution.
Example Example
i=1 for i in range(1, 10):
while(i <= 10): if (i % 5 = = 0):
print(i, end='') continue
if (i==5): print(i, end='')
break print("Done")
i=i+1
print("Done") Output: 1 2 3 4 6 7 8 9 Done
Output: 1 2 3 4 5 Done
12. State the differences between continue and pass statement with an K3
example.
Continue Pass
The continue statement is used to The pass statement is used when a
ignore the remaining statements statement is required syntactically.
in the current iteration of the loop
and moves the control back to the
start of the loop.
It stops the current iteration. It does nothing.
Syntax: continue Syntax: pass
Example Example
for i in range(1, 10): for i in range(1, 10):
if (i % 5 = = 0): if (i % 5 = = 0):
continue pass
print(i, end='') print(i, end='')
print("Done") print("Done")
Output: 1 2 3 4 6 7 8 9 Done
Output: 1 2 3 4 6 7 8 9 Done
5
Eg.
myStr = 'Python Programming!'
print("String = ", myStr)
print("String after slicing (negative indexing) = ", myStr[-4 : -1])
Output:
String after slicing (negative indexing) = ing
18. What are the two operators that are used in string functions? K2
The 2 operators used in string functions are ‘in’ and ‘not in’. They can be used
to test whether a substring is a member of a string or not.
>>>‘t‘ in ‘Python’
True
>>>‘p‘ in ‘Python’
False
>>>‘thon‘ not in ‘Python’
False
>>>‘then‘ not in ‘Python’
True
19. What is the use of str.upper() and str.lower() functions in string?? K2
The functions str.upper() and str.lower() will return a string with all the letters
of original string converted to upper or lower case letters.
>>>ss = ‘Python Programming’
>>>print(ss.upper())
PYTHON PROGRAMMING
>>>print(ss.lower())
python programming
20. List few string methods and their purpose, used in Python. K3
Method Description
capitalize() Converts the first character to upper case
find() Searches the string for a specified value and returns the
position of where it was found
index() Searches the string for a specified value and returns the
position of where it was found.
isalpha() Returns True if all characters in the string are in the alphabet
isdigit() Returns True if all characters in the string are digits
isnumeric() Returns True if all characters in the string are numeric
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
lower() Converts a string into lower case
upper() Converts a string into upper case
replace() Returns a string where a specified value is replaced with a
specified value
strip() Returns a trimmed version of the string
7
PART B
S. No Question BTL
1. Explain in detail about the different conditional control (16 Marks) K2
statements / Selection Control statements / decision making
with an example.
2. What are the different looping construct available in python? (16 Marks) K2
Explain each of them in detail with relevant example.
3. Write in detail about the different unconditional statements in (16 Marks) K2
Python. Give example program for each.
4. a) Design a simple calculator using if …elif construct. (16 Marks) K3
b) Write a Python program to check the prime number using
while loop.
5. a) Write a python program to print factorial of a given number (16 Marks) K3
b) Implement a Python program to find the sum of n even
natural numbers.
6. a) Develop a program to find the largest among three (16 Marks) K3
numbers.
b) Write a python program to find the given year is leap or
not.
7. Discuss about the following operations related to strings in K2
Python. (8 Marks)
(a) String comparison (8 Marks)
(b) String slicing
(c) String splitting
(d) String stripping
8. Explain the different string manipulation functions available (16 Marks) K2
in Python with example.