Warning: Undefined array key 0 in /var/www/tgoop/function.php on line 65

Warning: Trying to access array offset on value of type null in /var/www/tgoop/function.php on line 65
974 - Telegram Web
Telegram Web
Wait, why did all functions return 2?

The lambdas don’t capture the value of i at each loop iteration, they capture the variable itself. By the time you call the functions, the loop has finished, and i is left at its final value, 2.

This is called late binding and can cause unexpected behavior when creating closures inside loops.

So as a quick tip, Bind the current value at definition time using a default argument(like shown in the second image) Then each lambda remembers its own i value independently.
2
👍1
#PyQuiz

What is the type of type in Python?
Anonymous Quiz
38%
type
23%
class
30%
object
10%
meta
1
👍2
🔥1
#PyQuiz

What's the output of bool('False')?
Anonymous Quiz
35%
True
42%
False
5%
None
18%
Error
Python Functions You definitely Need to Know
3
👍43
1
#PyQuiz

What's the result of len(set("hello"))?
Anonymous Quiz
40%
5
37%
4
2%
3
21%
Error
Difference Between Variable and Object in Python
3👍1
#PyQuiz

Which is faster: list() or [ ]?
Anonymous Quiz
30%
list()
45%
[ ]
7%
Same speed
18%
Depends on list size
Why did x change too?!

Because x and y are just two names pointing to the same list in memory. When you modify one, the change reflects in both.

Use .copy() or slicing [:] if you want a separate list:
y = x.copy()
That way, you’re not changing the original but just making your own version.
3👍2
#PyQuiz

Which of these is NOT a valid Python data type?
Anonymous Quiz
18%
set
15%
dict
57%
array
10%
tuple
Feels off?

It's because all your objects share one variable (without you realizing it)

At first glance, it seems like every object should start fresh, right? But in this case, count is a class variable, which means it’s shared by all instances of the class.

Every time you create a new Counter(), you’re actually incrementing the same shared variable not something unique to each object.

If your goal is to give each object its own value, define it like this instead

class Counter:
    def __init__(self):
        self.count = 1

Now, each instance has its own count, stored on the object itself . no sharing, no surprises.
2
#PyQuiz

What does range(5)[-1] return?
Anonymous Quiz
14%
5
48%
4
12%
-1
25%
Error
3
What is an F-String in Python?
4
Looks neat, right? But let’s slow down.

The *b syntax is called extended iterable unpacking. It grabs everything in the middle of the list, leaving the first item (a) and the last (c) outside the star. This pattern is super handy, but can also behave unexpectedly if you assume it’ll grab just one item or not consider the structure of the data.

The starred variable always gets a list, and it can be empty so plan accordingly when unpacking, especially in function arguments or loops.
For example: Consider the following code
x, *y = [42]
print(y)  # []


No error, but y is just an empty list! Unpacking doesn’t always fill every name the way you might guess.
1👍1
2025/10/17 16:43:50
Back to Top
HTML Embed Code: