ECE
24CS101 Problem Solving and Python Programming
CIA – II (2 Marks):
1. Differentiate between break, continue, and pass statements.
Break terminates the loop immediately.
for i in range(5):
if ( i == 2):
break
print(i)
Output:
0
1
Continue skips the current iteration (loop) process and moves to the next.
for i in range(5):
if i == 2:
continue
print(i)
Output:
0
1
3
4
Pass does nothing; it is used as a placeholder.
for i in range(5):
if i == 2:
pass
print(i)
Output:
0
1
2
3
4
2. Difference between if, if-else, and if-elif-else statements.
The if statement checks a single condition and executes its block if the condition is
true.
Age =20
if A >= 18:
print(Age, “ is Eligible to vote”)
Output:
20 is Eligible to vote
The if-else statement provides two paths: one for true and another for false
conditions.
Age = 16
if A >= 18:
print(Age, “ is Eligible to vote”)
else:
print(Age, “ is not Eligible to vote”)
Output:
16 is not Eligible to vote
The if-elif-else statement is used when multiple conditions need to be tested
one after another. It executes the first block whose condition is true, otherwise
the else part runs if all fail.
Marks = 75
if Marks >= 90:
print(Marks, “ is O GRADE”)
elif Marks >= 80:
print(Marks, “ is A GRADE”)
elif Marks >= 70:
print(Marks, “ is B GRADE”)
elif Marks >= 60:
print(Marks, “ is C GRADE”)
elif Marks >=50:
print(Marks, “ is D GRADE”)
else:
print(Marks, “ is FAIL”)
Output:
75 is B GRADE
3. Difference between list and tuple.
List Tuple
List is mutable (can be changed). Tuple is immutable (cannot be changed).
Defined using square brackets: [ ] Defined using parentheses: ( )
Slower compared to tuples. Faster than lists.
Allows operations like append(), Does not support such modification
remove(), pop() methods.
Example: a = [1, 2, 3] Example: b = (1, 2, 3)
4. Syntax for creating a dictionary.
dict_name = {key1: value1, key2: value2, ...}
5. What is list slicing? Give an example.
List slicing is used to extract a portion of a list.
Subset of lists can be taken using the slice operator with two indexs in square
brackets, separated by a colon (:)
Example : A[2:5]
2 is the starting index 3rd Values, (Starting index is zero)
5 is the ending index - 1 (4th index) 5th Values.
A = [10, 20, 30, 40, 50]
print(A[2:5]) # Output [30, 40, 50]
print(A[ :4]) # Output [10, 20, 30, 40]
print(A[2: ]) # Output [30, 40, 50]
print(A[1:3]) # Output [20, 30]
6. Mention any four list methods.
A = [10, 20, 30, 40, 50]
append() is used to add the element at last
[Link](60)
print(a) # Output [10, 20, 30, 40, 50, 60]
remove() is used to delete the given value in list
[Link](40) # Output [10, 20, 30, 50, 60]
pop() is used to delete the first element
[Link]() # Output [20, 30, 50, 60]
[Link](-1) # Output [20, 30, 50]
max() is used to find the maximum in the list
max(A) # Output 50
7. Define a package in Python.
A package is a collection of related modules together within a single tree like
hierarchy.
A directory must contain a file named – [Link] file in order for python to
consider it as a package
8. Syntax for exception handling in Python.
try:
# Code that might raise an exception
except ExceptionType as e:
# Code to handle the exception
else:
# Code to continue if no exception occur
finally:
# Code execute if exception or not exception has occurred
9. What is an exception?
An exception is an error that occurs during the execution of a program and
interrupts normal program flow.
Examples include errors like dividing by zero, accessing invalid indexes, or
using wrong data types.
Exceptions can be handled using try and except blocks to prevent program
crashes.
Exception handling improves program stability and reliability.
10. What is a module? Give an example.
A module is a Python file containing code such as variables, functions, or classes.
Example: math, random, os module
1. What is a fruitful function? Give an example.
A fruitful function is a function that returns a value back to the part of the
program that called it.
These functions always use the return statement to send a result.
Fruitful functions are useful when the output is required for further
calculations or processing.
Example:
def square(x):
return x * x
2. What is the difference between if, if-else, and if-elif-else statements?
The if statement checks a single condition and executes its block if the condition is
true.
Age =20
if A >= 18:
print(Age, “ is Eligible to vote”)
Output:
20 is Eligible to vote
The if-else statement provides two paths: one for true and another for false
conditions.
Age = 16
if A >= 18:
print(Age, “ is Eligible to vote”)
else:
print(Age, “ is not Eligible to vote”)
Output:
16 is not Eligible to vote
The if-elif-else statement is used when multiple conditions need to be tested
one after another. It executes the first block whose condition is true, otherwise
the else part runs if all fail.
Marks = 75
if Marks >= 90:
print(Marks, “ is O GRADE”)
elif Marks >= 80:
print(Marks, “ is A GRADE”)
elif Marks >= 70:
print(Marks, “ is B GRADE”)
elif Marks >= 60:
print(Marks, “ is C GRADE”)
elif Marks >=50:
print(Marks, “ is D GRADE”)
3. Define list mutability
else: and aliasing.
print(Marks, “ is FAIL”)
Output:
75 is B GRADE
List mutability means that the elements of a list can be changed, added, or
removed after the list is created. This makes lists flexible for various
operations.
Aliasing occurs when two or more variables refer to the same list object in
memory. Changing the list using one variable will also affect the list seen
through the other variable.
4. What is meant by key-value pair in a dictionary?
A dictionary stores data in the form of key-value pairs, where each key is
unique.
The key acts as an index, and the value is the data associated with that key.
Key-value pairs allow fast access to data by referencing the key.
Example: {"age": 20} — here "age" is the key and 20 is the value.
5. List any two dictionary methods.
keys(): returns all keys in the dictionary.
values(): returns all values in the dictionary.
items(): returns key-value pairs as tuples.
update(): updates the dictionary with new key-value pairs.
6. Mention any four list methods.
append(): adds an element at the end of the list.
insert(): adds an element at a specified index.
remove(): removes the first matching element.
pop(): removes and returns an element from a given index.
sort(): arranges the list items in ascending or descending order.
7. Define a package in Python.
A package is a directory that contains multiple related Python modules.
It must contain an [Link] file that helps Python identify it as a package.
Packages help organize large programs into smaller, manageable components.
Example: a folder named maths containing files [Link], [Link], etc.
8. Define a text file.
A **text file** in Python is a file that stores data in plain text format (i.e.,
human-readable text) rather than binary data.
It normally has extension like .txt, .py, .doc, .xls, .pdf
Each line in a text file is stored as a sequence of characters separated by
newline characters.
It can be opened using modes like `'r'` (read), `'w'` (write), or `'a'` (append).
Example:
with open('[Link]', 'w') as file:
[Link]("Hello, World!")
9. What is an exception?
An exception is an error that occurs during the execution of a program and
interrupts normal program flow.
Examples include errors like dividing by zero, accessing invalid indexes, or
using wrong data types.
Exceptions can be handled using try and except blocks to prevent program
crashes.
Exception handling improves program stability and reliability.
10. Mention any two built-in exceptions in Python.
ZeroDivisionError: raised when a number is divided by zero.
TypeError: raised when an operation is performed on incompatible data types.
NameError: raised when a variable is not defined.
ValueError: raised when the data type is correct but the value is invalid.