Python - UT-2 Querstion & Answers
Python - UT-2 Querstion & Answers
String comparison means checking if two strings are equal or determining their
lexicographic (dictionary) order. It's a common operation in programming.
Example in Python:
python
CopyEdit
str1 = "Apple"
str2 = "Banana"
str3 = "Apple"
# Equality check
print(str1 == str2) # False
print(str1 == str3) # True
# Lexicographic comparison
print(str1 < str2) # True (because 'Apple' comes before 'Banana')
print(str2 > str3) # True (because 'Banana' comes after 'Apple')
A dictionary in Python is a collection of key-value pairs. Each key is unique and maps to a
value.
4) How to open a new file in python?
In Python, you use the open() function to open (or create) a file.
5) List any four common exception errors and explain when they occur?
6) List any four methods that are used in python list?
1.append()
2. remove()
3. insert(index, value)
4. pop()
Removes and returns the item at the given index (or last item by default).
PART-B
1. Explain the basic list operation that can be performed in python with an example
for each?
1. Creating a List
my_list = [1, 2, 3, 4]
📌 2. Accessing Elements
print(my_list[0]) # Output: 1
📌 3. Updating Elements
my_list[1] = 10
# my_list becomes [1, 10, 3, 4]
📌 4. Adding Elements
my_list.append(5)
my_list.insert(2, 99)
📌 5. Removing Elements
my_list.remove(10)
my_list.pop(2)
📌 6. Slicing a List
Example:
📌 8. Checking Membership
Use in or not in
Example:
3 in my_list # True
99 not in my_list # Depends on content
2) Explain how a string can be created in python and explain the various slicing
operation in string?
✅ Examples:
str1 = 'Hello'
str2 = "World"
str3 = '''This is a multi-line string'''
📌 String Indexing
text = "Python"
print(text[0]) # Output: 'P'
print(text[-1]) # Output: 'n' (negative indexing)
📌 String Slicing
🔁 Slicing Format
string[start : end : step]
Example:
text = "Programming"
print(text[0:11:2]) # Output: 'Pormig'
3) Write a python programme to create atext file with the content ‘’my first python
programming using files’’ in the name file 1.txt and then copy the contents of the file to
file 2.txt
✅ Python Program:
# Step 1: Create and write to file1.txt
with open("file1.txt", "w") as file1:
file1.write("my first python programming using files")
Python provides several ways to handle exceptions using the try-except block. Here are the
common methods:
1. Basic try-except
try:
x = 10 / 0
except:
print("An error occurred")
Output:
An error occurred
try:
x = int("abc")
except ValueError:
print("ValueError: Cannot convert string to integer")
Output:
ValueError: Cannot convert string to integer
try:
x = 10 / 0
except ZeroDivisionError:
print("ZeroDivisionError occurred")
except ValueError:
print("ValueError occurred")
Output:
ZeroDivisionError occurred
4. else Block
try:
x = 10 / 2
except ZeroDivisionError:
print("Error: division by zero")
else:
print("Success! Result is", x)
Output:
Success! Result is 5.0
5. finally Block
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("This will always execute")
Output:
Summary Table