30 Python Concepts I Wish I Knew Way Earlier

Everyone’s Python journey is different, and we all learn different concepts at different speeds, in different sequences. But some things ay off a lot more when you learn them early. Here are 30 Python concepts I wish I learn much earlier in my own Python journey.

1) f-strings (formatted strings)

I was taught to convert non-strings into strings before using the + operator to concatenate it with a string. But this got annoying very quickly.

# ANNOYING
name = 'tom'
age = 5

s = 'my name is ' + name + ' and my age is ' + str(age)

# using f-strings
name = 'tom'
age = 5

s = f'my name is {name} and my age is {age}'

We need to spend less effort caring about spaces and data types when we use f-strings. Additionally, f-strings enable use to do some cool things:

name = 'tom'
age = 5
height = 1.44

s = f'{name=} {age=} {height=}'
# name='tom' age=5 height=1.44

pi = 3.14159265
s = f'pi is {pi:.2f}'
# pi is 3.14

And many many other cool functionalities.

2) Tuple Unpacking (with & without *)

person = ['tom', 5, 'male']

name, age, gender = person
# name='tom', age=5, gender='male'

We can use tuple unpacking to assign multiple variables in one line.

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

first, second, *others = letters
# first='A', second='B'
# letters = ['C', 'D', 'E', 'F', 'G']

We can add a * character in front of variables to signify that it should contain everything else that hasn’t been unpacked.

Click Here :-