Unit 2 Question Bank & Answer
Unit 2 Question Bank & Answer
ANSWER:
c. Mixed Inputs
2. Write a Python program that demonstrates the use of if, elif, and else
statements. Explain how each part works and how conditions are
evaluated in a decision-making process.
ANSWER:
Explanation of Components
1. if Statement:
2. elif Statements:
3. else Statement:
o The else block is executed if none of the if or elif
conditions are True.
1. Sequential Evaluation:
3. Logical Structure:
ANSWER:
Key Observations:
2. What happens:
Continue Statement:
Skips the rest of the current iteration and moves to the next
iteration.
ANSWER:
You can access list items using their index. Python uses zero-
based indexing.
You can remove items using the del statement, pop() method, or
remove() method.
ANSWER:
● With the for loop we can execute a set of statements, once for
each item in a list, tuple, set etc.
loop through a range of numbers:
Nested loops are loops inside loops. They are often used to work
with 2D data structures like matrices or to perform repeated
operations.
6. Describe how a for loop can be used to manipulate items in a list. Write
a Python program that takes a list of numbers and prints only the even
numbers, using a for loop and conditional statements.
ANSWER:
How It Works:
1. Iteration:
2. Conditional Check:
ANSWER:
Explanation:
1. User Input:
2. Boolean Expression:
Zia brings ""Pride and Prejudice"". Add this book to the book_list.
ANSWER:
Explanation of the Code:
1. Ordered:
2. Mutable:
3. Indexed:
5. Heterogeneous:
Create a List:
Accessing List;
ANSWER:
I)
1. append()
Adds an element to the end of the list.
2.insert()
3. remove()
4. pop()
6. count()
7. clear()
Removes all elements from the list, leaving it empty.
8. index()
9. reverse()
10. copy()
Returns a shallow copy of the list (a new list with the same
elements).
12. join() (Not a list method, but a string method commonly used with
lists)
ii)
You can also use the list() constructor to create an empty list.
ii)How can you concatenate two tuples in Python and Discuss the
immutability property of tuples in Python.(5) "
ANSWER:
I)
Examaple:
1. No Modification of Elements:
ANSWER:
Explanation:
1. Input Handling:
2. Discount Calculation:
3. Error Handling:
12. i. Formulate with an example program to find all the values in the list
that are greater than the specified number
ii. Write a program to find out the square root of two numbers"
ANSWER:
i)
Explanation:
• The result list contains all the values that meet this
condition, which is returned.
ii)
Explanation:
ANSWER:
Explanation:
ANSWER:
ANSWER:
a)
Example:
How It Works:
3. The loop runs until all items in the sequence have been
processed.
b)
Explanation:
1. Input Handling:
2. Special Cases:
ANSWER:
a)
Syntax:
Example: