r/PythonLearning • u/No-Sentence4401 • 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.
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.
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:
codeUse ` 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):
First
ThecodeSecond
code blockEtc.
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.
Swap variables without a temp
a, b = b, aI was writing temp variables like a caveman for months before I found this.
Underscores in large numbers
budget = 1_000_000Python just ignores the underscores. It's purely for readability and honestly I use this constantly now.
enumerate()takes a start argumentfor i, item in enumerate(["a", "b", "c"], start=1): print(i, item) # 1 a, 2 b, 3 cNo more writing i + 1 every time you need 1-based indexing.
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.
collections.Counteris stupidly usefulfrom 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.
Star unpacking
first, *middle, last = [1, 2, 3, 4, 5] # first=1, middle=[2, 3, 4], last=5Really handy when you only care about the first or last element.
zip()for parallel iterationnames = ["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(...)).
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}.
any()andall()nums = [2, 4, 6, 8] all(n % 2 == 0 for n in nums) # TrueReplaced a bunch of ugly for-loop-with-a-flag patterns in my code.
f-string = for debugging (3.8+)x = 42 print(f"{x = }") # prints: x = 42This 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
1
1
1
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 π
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