20 Python Concepts I Wish I Knew Way Earlier

<p>There are lots of concepts we need to grasp in Python. And everyone learns them differently, in different sequences. Here are some things I wish I learnt much earlier when I was still a Python beginner.</p> <h1>1) Tuple Unpacking + Tuple Unpacking With *</h1> <pre> person = [&#39;bob&#39;, 30, &#39;male&#39;] name, age, gender = person # name=&#39;bob, age=30, gender=&#39;male&#39;</pre> <p>^ we can use tuple unpacking to assign multiple variables at one go.</p> <pre> fruits = [&#39;apple&#39;, &#39;orange&#39;, &#39;pear&#39;, &#39;pineapple&#39;, &#39;durian&#39;, &#39;banana&#39;] first, second, *others = fruits # first=&#39;apple&#39;, second=&#39;orange&#39; # others = [&#39;pear&#39;, &#39;pineapple&#39;, &#39;durian&#39;, &#39;banana&#39;]</pre> <p>^ we can add&nbsp;<code>*</code>&nbsp;in front of variables to unpack&nbsp;<em>everything else</em>&nbsp;into that variable.</p> <h1>2) List Comprehension + Dict/Set Comprehension</h1> <pre> lis = [expression for i in iterable if condition]</pre> <pre> l1 = [i for i in range(1,4)] # [1,2,3] l2 = [i*2 for i in range(1,4)] # [2,4,6] l3 = [i**2 for i in range(1,4)] # [1,4,9] l4 = [i for i in range(1,4) if i%2==1] # [1,3]</pre> <p>^ with list comprehension, we can create a custom list in one line of code.</p> <pre> set1 = {i for i in range(1,4)} # {1,2,3} d1 = {i:i**2 for i in range(1,4)} # {1:1, 2:4, 3:9}</pre> <p>^ set comprehension and dictionary comprehension can be used to create sets and dictionaries in the same way we create lists using list comprehensions.</p> <h1>3) Ternary operator</h1> <pre> score = 57 if score &gt; 90: grade = &#39;A*&#39; elif score &gt; 50: grade = &#39;pass&#39; else: grade = &#39;fail&#39; # grade = &#39;pass&#39;</pre> <p>^ a normal if-elif-else block</p> <pre> score = 57 grade = &#39;A*&#39; if score&gt;90 else &#39;pass&#39; if score&gt;50 else &#39;fail&#39; # grade = &#39;pass&#39;</pre> <p>^ we can condense the if-elif-else block into ONE line using the ternary operator.</p> <h1>4) Magic Methods In Python Classes</h1> <pre> class Dog(): def __init__(self, name, age)&hellip;</pre> <p><a href="https://levelup.gitconnected.com/20-python-concepts-i-wish-i-knew-way-earlier-573cd189c183">Click Here</a></p>