r/PythonLearning • • Apr 04 '26

Discussion 10 Python Tricks I Wish I Knew as a Beginner

I've been learning Python for about a year now and I keep stumbling on little features that make me go "wait, that's been there the whole time??" Figured I'd share the ones that actually changed how I write code day to day.

  1. Swap variables without a temp

a, b = b, a

I was writing temp variables like a caveman for months before I found this.

2) Underscores in large numbers

budget = 1_000_000

Python just ignores the underscores. It's purely for readability and honestly I use this constantly now.

3) enumerate() takes a start argument

for i, item in enumerate(["a", "b", "c"], start=1):

print(i, item) # 1 a, 2 b, 3 c

No more writing i + 1 every time you need 1-based indexing.

4) The walrus operator := (3.8+)

if (n := len(my_list)) > 10:

print(f"List is too long ({n} elements)")

Assign and check in one line. Took me a while to warm up to this one but now I love it.

5) collections.Counter is stupidly useful

from collections import Counter

Counter("mississippi")

# Counter({'s': 4, 'i': 4, 'p': 2, 'm': 1})

I used to write manual counting loops like an idiot. This does it in one line.

6) Star unpacking

first, *middle, last = [1, 2, 3, 4, 5]

# first=1, middle=[2, 3, 4], last=5

Really handy when you only care about the first or last element.

7) zip() for parallel iteration

names = ["Alice", "Bob"]

scores = [95, 87]

for name, score in zip(names, scores):

print(f"{name}: {score}")

Beats the hell out of for i in range(len(...)).

8) Dict merge with | (3.9+)

defaults = {"theme": "light", "lang": "en"}

user_prefs = {"theme": "dark"}

settings = defaults | user_prefs

# {'theme': 'dark', 'lang': 'en'}

Right side wins on conflicts. So much cleaner than {**a, **b}.

9) any() and all()

nums = [2, 4, 6, 8]

all(n % 2 == 0 for n in nums) # True

Replaced a bunch of ugly for-loop-with-a-flag patterns in my code.

10) f-string = for debugging (3.8+)

x = 42

print(f"{x = }") # prints: x = 42

This one's small but it saves so much time when you're printing variables to figure out what's going wrong.

Which of these was new to you? I'm curious what else I might be missing, drop your favorite trick in the comments.

332 Upvotes

21 comments sorted by

2

u/the114dragon Apr 04 '26

I knew very few of these, and might have to start using some (enumerate I'm looking at you) in scripts that I already have

2

u/No-Sentence4401 Apr 04 '26

Me too! And I've been coding in Python for almost half a decade!

1

u/the114dragon Apr 04 '26

I don't even need to know anything complicated for exams, but I like to practice with more complicated stuff, especially like this. It feels like much better practice.

11

u/Overall_Anywhere_651 Apr 04 '26

This formatting is killing me. It has taken years off my life just by looking at it.

3

u/No-Sentence4401 Apr 04 '26

Haha sorry! I really took the time to format it properly πŸ™πŸ»

4

u/Lokrea Apr 04 '26 edited Apr 04 '26

Thanks for the tips! But yes, the formatting ... Please show code as code, maybe with Markdown?

Example of inline code.

a code block:

code

Use ` for inline or ``` for a code block.

Lists can be made, adding a - (dash) or 1. before each item (Note: 2 spaces after "First" and "Second" to force line breaks):

  1. First
    The code

  2. Second

    code block
  3. Etc.

The markup, but ` switched to |, to allow showing it :)

1. First  
   The |code|
1. Second  
   |||
   code block
   |||
1. Etc.

4

u/Lokrea Apr 04 '26

Formatted with Markdown, source at the bottom.

  1. Swap variables without a temp

    a, b = b, a

    I was writing temp variables like a caveman for months before I found this.

  2. Underscores in large numbers

    budget = 1_000_000

    Python just ignores the underscores. It's purely for readability and honestly I use this constantly now.

  3. enumerate() takes a start argument

    for i, item in enumerate(["a", "b", "c"], start=1):
    print(i, item) # 1 a, 2 b, 3 c

    No more writing i + 1 every time you need 1-based indexing.

  4. The walrus operator := (3.8+)

    if (n := len(my_list)) > 10:
    print(f"List is too long ({n} elements)")

    Assign and check in one line. Took me a while to warm up to this one but now I love it.

  5. collections.Counter is stupidly useful

    from collections import Counter
    Counter("mississippi")
    # Counter({'s': 4, 'i': 4, 'p': 2, 'm': 1})

    I used to write manual counting loops like an idiot. This does it in one line.

  6. Star unpacking

    first, *middle, last = [1, 2, 3, 4, 5]
    # first=1, middle=[2, 3, 4], last=5

    Really handy when you only care about the first or last element.

  7. zip() for parallel iteration

    names = ["Alice", "Bob"]
    scores = [95, 87]
    for name, score in zip(names, scores):
    print(f"{name}: {score}")

    Beats the hell out of for i in range(len(...)).

  8. Dict merge with | (3.9+)

    defaults = {"theme": "light", "lang": "en"}
    user_prefs = {"theme": "dark"}
    settings = defaults | user_prefs
    # {'theme': 'dark', 'lang': 'en'}

    Right side wins on conflicts. So much cleaner than {**a, **b}.

  9. any() and all()

    nums = [2, 4, 6, 8]
    all(n % 2 == 0 for n in nums) # True

    Replaced a bunch of ugly for-loop-with-a-flag patterns in my code.

  10. f-string = for debugging (3.8+)

    x = 42
    print(f"{x = }") # prints: x = 42

    This one's small but it saves so much time when you're printing variables to figure out what's going wrong.

SOURCE (switch | to `)

1. Swap variables without a temp  
   |||
   a, b = b, a
   |||
   I was writing temp variables like a caveman for months before I found this.

1. Underscores in large numbers  
   |||
   budget = 1_000_000
   |||
   Python just ignores the underscores. It's purely for readability and honestly I use this constantly now.

1. |enumerate()| takes a start argument  
   |||
   for i, item in enumerate(["a", "b", "c"], start=1):
   print(i, item) # 1 a, 2 b, 3 c
   |||
   No more writing i + 1 every time you need 1-based indexing.

1. The walrus operator |:=| (3.8+)  
   |||
   if (n := len(my_list)) > 10:
   print(f"List is too long ({n} elements)")
   |||
   Assign and check in one line. Took me a while to warm up to this one but now I love it.

1. |collections.Counter| is stupidly useful  
   |||
   from collections import Counter
   Counter("mississippi")
   # Counter({'s': 4, 'i': 4, 'p': 2, 'm': 1})
   |||
   I used to write manual counting loops like an idiot. This does it in one line.

1. Star unpacking  
   |||
   first, *middle, last = [1, 2, 3, 4, 5]
   # first=1, middle=[2, 3, 4], last=5
   |||
   Really handy when you only care about the first or last element.

1. |zip()| for parallel iteration  
   |||
   names = ["Alice", "Bob"]
   scores = [95, 87]
   for name, score in zip(names, scores):
   print(f"{name}: {score}")
   |||
   Beats the hell out of for i in range(len(...)).

1. Dict merge with | (3.9+)  
   |||
   defaults = {"theme": "light", "lang": "en"}
   user_prefs = {"theme": "dark"}
   settings = defaults | user_prefs
   # {'theme': 'dark', 'lang': 'en'}
   |||
   Right side wins on conflicts. So much cleaner than {**a, **b}.

1. |any()| and |all()|  
   |||
   nums = [2, 4, 6, 8]
   all(n % 2 == 0 for n in nums) # True
   |||
   Replaced a bunch of ugly for-loop-with-a-flag patterns in my code.

1. |f|-string = for debugging (3.8+)  
   |||
   x = 42
   print(f"{x = }") # prints: x = 42
   |||
   This one's small but it saves so much time when you're printing variables to figure out what's going wrong.

2

u/No-Sentence4401 Apr 04 '26

Thank you πŸ™πŸ» I will be mindful next time

3

u/FreeLogicGate Apr 04 '26

You know you can edit your post... Lokrea literally did the edit for you, just take the source version, search/replace for the ||| with backtics and update your original post, and everyone wins.

8

u/Due_Awareness9049 Apr 04 '26

Generators and comprehensions

2

u/Downtown_Finance_661 Apr 04 '26

I use comprehensions a lot but have never used generators. Have no idea where i could use them. Please give simple examples how you use generators.

4

u/ALonelyKobold Apr 04 '26

I have 1000 API calls I need to make, set up a generator to give me the next call rather than holding them all in memory

3

u/FreeLogicGate Apr 04 '26

Here's an example with/without a generator you can compare. Goal is to compute sum of all squares in a range. First with a list comprehension (as well as a memory use diagnostic).

from pympler.asizeof import asizeof

numRange = 2_000_000
print(f"{(asizeof([num for num in range(numRange)])/1024**2):.2f} MBytes")

sum_of_squares = sum([num ** 2 for num in range(numRange)])
print(f"Sum of squares: {sum_of_squares:,}")

This uses a generator expression rather than filling the list (note the small but important syntax difference -- no [...] with the generator syntax). Python doesn't create a 77mb python list with the generator expression. If you run these you'll see the results are the same.

numRange = 2_000_000
sum_of_squares = sum(num ** 2 for num in range(numRange))
print(f"Sum of squares: {sum_of_squares:,}")

2

u/Downtown_Finance_661 Apr 04 '26

Wtf. Why no one teach me this before?

Tbh generator syntax looks strange at first glance: we pass some 'unbounded' multiword expression to sum() function and expect the function to manage it.

5

u/plydauk Apr 04 '26

A couple of my own:Β 

Using collections.defaultdict instead of checking whether a certain key exists in a dict or using {}.setdefault(...) and {}.get(...). For example:

from collections import default dict

my_dict = defaultdict(int)
my_dict['apple'] += 1
print(my_dict)

Similarly, use something like if 'apple' in set(['banana', 'orange', ...]) instead of checking against a list of values. set uses hashing to achieve search in constant time, as opposed to linear time of a list.

3

u/Worth-Orange-1586 Apr 04 '26

Match statements

1

u/Jackpotrazur Apr 04 '26

Interesting

1

u/somebody_throw_a_pie Apr 04 '26

no idea how i never learned 2

1

u/Euphoric_Run_3875 Apr 13 '26

Here is also a python tool i wish i know earlier β€œsentro” absolutely amazing https://pypi.org/project/sentro/

1

u/priya-08 Apr 20 '26

This is really helpful, thank you! I wish I had seen this when I first started learning Python πŸ˜„