DEV Community

Cover image for 5 Python Tricks Every Python Developer should know

5 Python Tricks Every Python Developer should know

Blessing Agyei Kyem on January 03, 2023

Photo by Chris Ried on Unsplash As a Python developer or enthusiast, it's recommended you know some python tricks to help you facilitate your w...
Collapse
 
samadon1 profile image
Samuel Amankwah Donkor

Wow these are some cools tricks 🔥

Collapse
 
blessing988 profile image
Blessing Agyei Kyem

Thanks!

Collapse
 
ilteriskeskin profile image
Ali İlteriş Keskin

Nice tricks, thanks.

Collapse
 
blessing988 profile image
Blessing Agyei Kyem

Glad you love them!

Collapse
 
mohammadparsajavidi profile image
mohammadparsa-javidi

that was perfect 👌

Collapse
 
blessing988 profile image
Blessing Agyei Kyem

Thanks!

Collapse
 
behainguyen profile image
Be Hai Nguyen

Hi Blessing Agyei Kyem,

Great article, thank you.

Following your example:

ten_numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

*first_9, last_num = ten_numbers
Enter fullscreen mode Exit fullscreen mode

I tried this:

*first_2, *next_7, last_num = ten_numbers

print(first_2)
print(next_7)
print(last_num)
Enter fullscreen mode Exit fullscreen mode

It does not like it:

SyntaxError: multiple starred expressions in assignment
Enter fullscreen mode Exit fullscreen mode

While this is acceptable:

_, _, *three_to_nine, last_num = ten_numbers

print(three_to_nine)
print(last_num)
Enter fullscreen mode Exit fullscreen mode
[3, 4, 5, 6, 7, 8, 9]
10
Enter fullscreen mode Exit fullscreen mode

Take care.

Collapse
 
blessing988 profile image
Blessing Agyei Kyem

You can't use * twice in your code when unpacking the elements

Collapse
 
behainguyen profile image
Be Hai Nguyen

I used this before. But not with *. Thank you and take care.