Explanation:
First pair: (1, 3)
Python Coding December 13, 2025 Python Quiz No comments
The for x in a loop iterates over the original elements of the list, not the updated ones.
Changing a[0] does not affect the sequence of values that x will take.
Initial values:
a = [1, 2, 3, 4]total = 0
Modifying a list inside a for loop does not change the iteration values.
The loop uses an internal index to fetch the next element.
Python Coding December 11, 2025 Python Quiz No comments
You start with a list of three numbers.
len(lst) = 3, so range(3) generates:
i = 0i = 1i = 2
You will loop three times, using each index of the list.
This means:
lst[i] = lst[i] + iNow update values step-by-step:
lst[0] = lst[0] + 0lst[0] = 10 + 0 = 10
List becomes:
[10, 20, 30]lst[1] = lst[1] + 1lst[1] = 20 + 1 = 21
List becomes:
lst[2] = lst[2] + 2lst[2] = 30 + 2 = 32
List becomes:
[10, 21, 32]This program adds each element’s index to its value.
| Index (i) | Original Value | Added | New Value |
|---|---|---|---|
| 0 | 10 | +0 | 10 |
| 1 | 20 | +1 | 21 |
| 2 | 30 | +2 | 32 |
Python Coding December 10, 2025 Python Quiz No comments
๐ The array does NOT change.
i is just a temporary variable
It does NOT modify the original array a
So this line:
i = i * 2✔️ Only changes i
❌ Does NOT change a
Iteration steps:
| Loop Step | i Value | i * 2 | a (unchanged) |
|---|---|---|---|
| 1 | 1 | 2 | [1 2 3] |
| 2 | 2 | 4 | [1 2 3] |
| 3 | 3 | 6 | [1 2 3] |
You never assign the new values back into a.
for i in range(len(a)):a[i] = a[i] * 2print(a)
✅ Output:
[2 4 6]a = a * 2print(a)
✅ Output:
[2 4 6]❗ Looping directly over a NumPy array does NOT change the original array unless you assign back using the index.
Python Coding December 09, 2025 Python Quiz No comments
def f(x):return x + 1
This function:
Takes one number x
Returns x + 1
✅ Example:
f(1) → 2
f(5) → 6
This applies f() to each element:
| Original | After f(x) |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
Important:
map() does NOT execute immediately
It creates a lazy iterator
So at this point:
m → (2, 3, 4) # not yet computedNow we apply f() again on the result of the first map:
| First map | Second map |
|---|---|
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
So the final values become:
(3, 4, 5)This executes the lazy iterator and prints:
[3, 4, 5][3, 4, 5]
Python Coding December 08, 2025 Python Quiz No comments
clcoding = [1, 2, 3, 4]total = 0
✅ x comes from the original iteration sequence
✅ But you are modifying the same list during iteration
✅ Only index 0 keeps changing
✅ The loop still reads values 1, 2, 3, 4 safely
Changing list values during iteration is allowed
But changing list size can cause unexpected behavior
Python Coding December 08, 2025 Python Quiz No comments
List length = 3
This line does two things at once using a tuple:
| Part | What it Does |
|---|---|
| clcoding.append(0) | Adds 0 to the list |
| len(clcoding) | Gets updated length |
| [1] | Returns second value only |
✅ So each time f(x) runs → list grows by 1 → new length is returned
map() does NOT run immediately.
It runs only when next(m) is called.
List before: [1, 2, 3]
append(0) → [1, 2, 3, 0]
len() → 4
✅ Prints: 4
List before: [1, 2, 3, 0]
append(0) → [1, 2, 3, 0, 0]
len() → 5
✅ Prints: 5
List before: [1, 2, 3, 0, 0]
append(0) → [1, 2, 3, 0, 0, 0]
len() → 6
✅ Prints: 6
456
✅ map() is lazy
✅ Mutable list modified during iteration
✅ Tuple execution trick inside lambda
✅ Side-effects inside functional calls
Python Coding December 05, 2025 Python Quiz No comments
✅ This creates a filter object (iterator) that will return only even numbers
✅ Important: filter() does NOT run immediately — it waits until you convert it to a list or loop over it.
Now the list becomes:
[1, 2, 3, 4, 6]Now the filter actually runs, using the updated list.
✅ Even numbers from [1, 2, 3, 4, 6] are:
[2, 4, 6]
✅ filter() is lazy — it evaluates only when needed,
✅ So it always uses the latest version of the list.
Python Coding December 04, 2025 Python Quiz No comments
reduce() combines all elements of a list into a single value using a function.
Example logic:
reduce(lambda x, y: x + y, [1, 2, 3])# works like this:# 1 + 2 = 3# 3 + 3 = 6
Since the list has only one element, there is:
❌ No second value to add
✅ So reduce() simply returns the same single value
So:
reduce(lambda x, y: x + y, [5]) → 5This runs for:
i = 0
i = 1
i = 2
๐ Total = 3 times
Each time, this runs:
print(reduce(lambda x, y: x + y, a))And each time it prints:
5555
reduce() with a single-element list always returns that element
The loop just repeats the same result
Python Coding December 03, 2025 Python Quiz No comments
A dictionary with:
Key "x" → Value 5
Key "y" → Value 15
s will store the final total.
.values() returns only the values:
5, 15This means:
If v > 10 → add v
Else → add 2
| Iteration | v | Condition v > 10 | Added to s | New s |
|---|---|---|---|---|
| 1st | 5 | ❌ False | +2 | 2 |
| 2nd | 15 | ✅ True | +15 | 17 |
✅ Output:
17
✅ Dictionary
✅ Loop
✅ .values()
✅ Ternary if-else
✅ Accumulator variable
Python Coding December 02, 2025 Python Quiz No comments
Even though we write:
i = i + 5๐ This DOES NOT modify the NumPy array.
| Step | Explanation |
|---|---|
| for i in x: | i receives a copy of each value, not the original element |
| i = i + 5 | Only changes the local variable i |
| x | The original NumPy array stays unchanged |
So this loop works like:
i = 10 → i = 15 (x unchanged)i = 20 → i = 25 (x unchanged)i = 30 → i = 35 (x unchanged)
But x never updates, so output is still:
[10 20 30]for idx in range(len(x)):x[idx] = x[idx] + 5print(x)
✅ Output:
[15 25 35]x = x + 5print(x)
✅ Output:
[15 25 35]
Loop variable in NumPy gives a copy, not a reference.
To change NumPy arrays, use indexing or vectorized operations.
Python Coding December 01, 2025 Python Quiz No comments
i = 1arr.remove(1)
List becomes:
[2, 3]Now Python moves to the next index (index 1).
Now the list is:
[2, 3]Index 1 has:
i = 3So:
arr.remove(3)List becomes:
[2]There is no next element to continue.
You are modifying the list while looping over it
When an element is removed:
The list shifts to the left
The loop skips elements
This causes unexpected results
for i in arr[:]:arr.remove(i)
❌ Never modify a list while iterating over it directly.
Python Coding November 30, 2025 Python Quiz No comments
i = 0x = x + i = 0 + 0 = 0
i = 1x = x + i = 0 + 1 = 1
i = 2x = x + i = 1 + 2 = 3
i remains stored as the last value → 2
✅ In Python, loop variables (i) stay alive after the loop ends, unlike some other languages.
Python Coding November 30, 2025 Python Quiz No comments
This does NOT create a list immediately. It creates a lazy iterator:
res → (2, 3, 4)…but nothing is calculated yet.
for i in res:pass
This loop runs through all values: 2, 3, 4
But since we used pass, nothing is printed
Important: After this loop, the iterator is now EXHAUSTED
res is already empty
So converting it to a list gives:
A map() object can be used ONLY ONCE.
Once it is looped through, it becomes empty forever.
res = list(map(lambda x: x + 1, nums))for i in res:passprint(res) # [2, 3, 4]Python for Civil Engineering: Concepts, Computation & Real-world Applications
Python Coding November 28, 2025 Python Quiz No comments
a = [1, 2, 3]b = [10, 20, 30]
a contains: 1, 2, 3
b contains: 10, 20, 30
This pairs elements from both lists:
(1, 10)(2, 20)(3, 30)
i gets values from list a
j gets values from list b
| Iteration | i | j |
|---|---|---|
| 1st | 1 | 10 |
| 2nd | 2 | 20 |
| 3rd | 3 | 30 |
This changes only the loop variable i,
✅ It does NOT change the original list a
Example:
1 → becomes 101
2 → becomes 102
3 → becomes 103
But this updated i is never printed or used further.
Only j is printed
j comes from list b
So it prints:
102030
102030
✅ Changing i does not modify list a
✅ zip() pairs values but does not link memory
✅ Only j is printed, so i + 100 has no visible effect
Python Coding November 27, 2025 Python Quiz No comments
This list will store functions (lambdas).
The values of i will be:
0 → 1 → 2⚠️ Important:
You are NOT storing the VALUE of i,
You are storing a function that will return i later.
So after the loop:
funcs[0] → returns i
funcs[1] → returns i
funcs[2] → returns i
All three lambdas refer to the SAME variable i.
After the loop finishes:
i = 2This executes:
f() → returns i → which is 2So all functions return:
[2, 2, 2]Python lambdas:
Do NOT remember the value
They remember the variable reference
Value is looked up only when the function is called
This behavior is called Late Binding.
funcs = []for i in range(3):funcs.append(lambda i=i: i)print([f() for f in funcs])
Why this works:
i=i captures the value immediately
Each lambda stores its own copy
Python Coding November 27, 2025 Python Quiz No comments
It contains both falsy and truthy values.
lambda x: x means:
๐ Return the value itself.
filter() keeps only values that are truthy.
In Python, these are falsy values:
0, None, False, ""
So 0 is removed, and all non-zero numbers remain.
Since filter() returns an iterator, we convert it to a list to display it.
๐ This line:
filter(lambda x: x, nums)means:
“Keep only the values that are True in a boolean sense.”
Free Books Python Programming for Beginnershttps://t.co/uzyTwE2B9O
— Python Coding (@clcoding) September 11, 2023
Top 10 Python Data Science book
— Python Coding (@clcoding) July 9, 2023
๐งต:
Top 4 free Mathematics course for Data Science ! pic.twitter.com/s5qYPLm2lY
— Python Coding (@clcoding) April 26, 2024
Web Development using Python
— Python Coding (@clcoding) December 2, 2023
๐งต: