0% found this document useful (0 votes)
4 views2 pages

Complex Python Syntax Questions

The document contains a series of Python code snippets along with their outputs and explanations. It covers topics such as default mutable arguments, list references, for loop behavior with else, list comprehensions, and the behavior of try-finally blocks. Each question is followed by the corresponding output and a brief explanation of the underlying concept.

Uploaded by

mohitnilvarn2004
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)
4 views2 pages

Complex Python Syntax Questions

The document contains a series of Python code snippets along with their outputs and explanations. It covers topics such as default mutable arguments, list references, for loop behavior with else, list comprehensions, and the behavior of try-finally blocks. Each question is followed by the corresponding output and a brief explanation of the underlying concept.

Uploaded by

mohitnilvarn2004
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/ 2

Q1. What is the output of the following code?

```python
def func(x=[]):
x.append(1)
return x

print(func())
print(func())
```

Output:
[1]
[1, 1]
Explanation: Default mutable arguments retain changes between calls.

Q2. What is the output of the following code?


```python
a = [1, 2, 3]
b=a
a.append(4)
print(b)
```

Output:
[1, 2, 3, 4]
Explanation: `a` and `b` refer to the same list object.

Q3. What is the output of the following code?


```python
for i in range(3):
print(i)
else:
print("Done")
```

Output:
0
1
2
Done
Explanation: The `else` block in a for loop runs when the loop doesn't exit via `break`.

Q4. What is the output of the following code?


```python
x = [i*i for i in range(5) if i%2 == 0]
print(x)
```

Output:
[0, 4, 16]
Explanation: List comprehension with condition filters even numbers.
Q5. What is the output of the following code?
```python
def f():
try:
return 1
finally:
return 2

print(f())
```

Output:
2
Explanation: The `finally` block overrides the return in `try`.

You might also like