30 Python Concepts I Wish I Knew Way Earlier
<p>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.</p>
<h1>1) f-strings (formatted strings)</h1>
<p>I was taught to convert non-strings into strings before using the <code>+</code> operator to concatenate it with a string. But this got annoying very quickly.</p>
<pre>
# 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}'</pre>
<p>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:</p>
<pre>
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
</pre>
<p>And many many other cool functionalities.</p>
<h1>2) Tuple Unpacking (with & without *)</h1>
<pre>
person = ['tom', 5, 'male']
name, age, gender = person
# name='tom', age=5, gender='male'</pre>
<p>We can use tuple unpacking to assign multiple variables in one line.</p>
<pre>
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
first, second, *others = letters
# first='A', second='B'
# letters = ['C', 'D', 'E', 'F', 'G']</pre>
<p>We can add a <code>*</code> character in front of variables to signify that it should contain everything else that hasn’t been unpacked.</p>
<p><a href="https://levelup.gitconnected.com/30-python-concepts-i-wish-i-knew-way-earlier-3add72af6433">Click Here :-</a></p>