TGViewer
Channel Public Channel
Python Learning

Python Learning

@python_bds

Python learning resources

Beginner to advanced Python guides, cheatsheets, books and projects.

For data science, backend and automation.
Join 👉 https://rebrand.ly/bigdatachannels

DMCA: @disclosure_bds
Contact: @mldatascientist
Subscribers
5.75K
Photos
582
Videos
2
Links
130

Showing posts older than #979 · Back to latest

Older Posts 20 shown
Post #978 938
  • ❤ 1
Post #976 976
  • ❤ 2
Post #973 965
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
Post #971 776
What is an F-String in Python?
  • ❤ 4
Post #970 808
  • ❤ 4
Post #969 797
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
Post #967 833
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
Post #965 879
Difference Between Variable and Object in Python
  • ❤ 3
  • 👍 1
Post #962 1.04K
  • 👍 4
  • ❤ 3
Post #961 1.06K
Python Functions You definitely Need to Know
  • ❤ 3
Post #959 1.1K
  • 🔥 1
Post #958 1.15K
  • 👍 2
Older posts →
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →