0% found this document useful (0 votes)
6 views4 pages

Error Questions and Solution PDF

The document outlines common Python errors and their solutions, providing explanations for each error type. It covers issues such as incorrect assignment, missing colons, out-of-range list access, and type mismatches. Additionally, it explains the use of *args and **kwargs in function definitions for handling variable arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Error Questions and Solution PDF

The document outlines common Python errors and their solutions, providing explanations for each error type. It covers issues such as incorrect assignment, missing colons, out-of-range list access, and type mismatches. Additionally, it explains the use of *args and **kwargs in function definitions for handling variable arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Error Finding Questions

1. Question:
x = 10
if x = 5:
print("Equal")
Solution:
x = 10
if x == 5:
print("Equal")
Explanation:
The error here is the use of a single = sign, which is an assignment operator. It should be a double == to check
equality.

2. Question:
for i in range(5)
print(i)
Solution:
for i in range(5):
print(i)
Explanation:
The for loop is missing a colon (:) at the end of the statement. Python uses colons to define the start of a block.

5. Question:
x = [1, 2, 3]
x[5] = 10
Solution:
x = [1, 2, 3]
x.append(10)
Explanation:
Index 5 is out of range for the list. To add an element to the list, use append() instead of directly assigning to an out-
of-range index.

6. Question:
def greet(name):
return "Hello, " + name
print(greet("John"))
Solution:
def greet(name):
return "Hello, " + name
print(greet("John"))
Explanation:
This code is correct. It concatenates the string "Hello, " with the variable name and prints it. There is no error here,
but ensure the proper string concatenation using +.

7. Question:
x = "Hello"
y = "World"
print(x + y)
Solution:
x = "Hello"
y = "World"
print(x + " " + y)
Explanation:
The code correctly concatenates the strings x and y, but it doesn't add a space between them. To do so, add " "
between the two strings.
8. Question:
def func():
return 5 + "hello"
print(func())
Solution:
def func():
return 5 + int("hello")
print(func())
Explanation: You can't add a number to a string directly. If you want to add, convert the string "hello" into a
number, or handle the string appropriately.

9. Question:
x = [1, 2, 3]
del x[3]
Solution:
x = [1, 2, 3]
del x[2]
Explanation: Index 3 is out of range for the list x. The correct index for deletion is 2 because Python indexing starts
at 0.

10. Question:
a = {1, 2, 3}
a[0] = 5
Solution:
a = {1, 2, 3}
a.add(5)
Explanation: Sets do not support indexing, so attempting to assign to an index will result in an error. Use the .add()
method to add an element to the set.

11. Question:
x = "5"
y=5
print(x + y)
Solution:
x = "5"
y=5
print(int(x) + y)
Explanation: You can't add a string to an integer directly. Convert the string x to an integer using int().

12. Question:
x = "Hello"
x[0] = "J"
Solution:
x = "Hello"
x = "J" + x[1:]
Explanation: Strings in Python are immutable, meaning their characters cannot be modified. Instead, create a new
string by concatenating the desired character with the rest of the string.

13. Question:
def add_numbers(x, y):
return x + y
print(add_numbers(5, "5"))
Solution: def add_numbers(x, y):
return x + int(y)
print(add_numbers(5, "5")) Explanation:
(You cannot add an integer and a string directly. Convert "5" to an integer before adding).
14. Question:
x = {1, 2, 3}
y = {2, 3, 4}
print(x & y)
Solution:
x = {1, 2, 3}
y = {2, 3, 4}
print(x.intersection(y))
Explanation: & works for sets, but you can also use the .intersection() method for clarity and explicit set operations.

15. Question:
x = [1, 2, 3]
print(x[1:5])
Solution:
x = [1, 2, 3]
print(x[1:3])
Explanation: The slice x[1:5] goes beyond the length of the list. Python handles this by returning up to the last
index, so x[1:3] is correct for the given list.

16. Question:
a = [10, 20, 30]
print(a[1])
Solution:
a = [10, 20, 30]
print(a[0])
Explanation: This code is correct; however, if you were attempting to refer to another index or make another
logical error, check if the index is valid.

17. Question:
import math
print(math.sqrt(-1))
Solution:
import cmath
print(cmath.sqrt(-1))
Explanation: The square root of a negative number cannot be calculated using math.sqrt(). Instead, use the cmath
library for complex numbers.

19. Question:
x = [1, 2, 3]
y=x
y[0] = 10
print(x)
Solution:
x = [1, 2, 3]
y = x.copy()
y[0] = 10
print(x)
Explanation:
When you assign y = x, both x and y refer to the same object. Use .copy() to create a new list.

20. Question:
x = "Python"
y = "Python"
print(x is y)
Solution:
x = "Python"
y = "Python"
print(x == y)
Explanation:
The is operator checks object identity, while == checks value equality. For immutable objects like strings, is may
return True due to interning, but it’s better to use == for equality.

def example(*args, **kwargs):


print(args[1])
print(kwargs["name"])
example(1, 2, 3, name="John")

Explanation:
1. *args:
o *args is a way in Python to pass a variable number of positional arguments to a function.
o It collects all the extra positional arguments passed to the function into a tuple. In the case of the
example(1, 2, 3, name="John") call, args will be a tuple containing the values (1, 2, 3).
2. **kwargs:
o **kwargs is used to pass a variable number of keyword arguments to the function.
o It collects these arguments into a dictionary where the keys are the argument names and the values
are the corresponding values passed in the function call.
o In this case, kwargs will be a dictionary containing {"name": "John"} because you passed
name="John" as a keyword argument.
Function Breakdown:
• When the function example(1, 2, 3, name="John") is called:
o args will be (1, 2, 3) (a tuple).
o kwargs will be {"name": "John"} (a dictionary).
Inside the Function:
1. print(args[1]):
o args[1] accesses the second item in the tuple args.
o Since args = (1, 2, 3), args[1] will be 2.
o This will print 2.
2. print(kwargs["name"]):
o kwargs["name"] looks up the value associated with the key "name" in the kwargs dictionary.
o Since kwargs = {"name": "John"}, kwargs["name"] will be "John".
o This will print John.
Output: 2 John
Key Points:
• *args allows you to pass any number of positional arguments, and they will be available as a tuple inside the
function.
• **kwargs allows you to pass any number of keyword arguments (arguments with names), and they will be
available as a dictionary inside the function.

You might also like