4 Python tricks you should know as a beginner

D. Adefioye
2 min readMar 3, 2021

Introduction

One of the many advantages python as programming language has over other language is its friendly syntax for beginners . It simplicity can be seen in its various one-liners and very easy functionalities. There are various aspect to this, but we would just explore 4 of these.

1. List Unpacking

Many time, we are faced with the challenge of copying items within existing list to a new list.

The Asterisks(‘*’) enables unpacking without fear of tampering with previous list(shallow copy). See below example

2. List Comprehension

Python provides a very flamboyant way making simple for loops with one liners using List comprehension. Even though list comprehensions comes with its short-comings for more complicated loops, at least for simple loops it practically does an amazing job.

Additionally, you can include conditions to list comprehension as well. Let’s assume you want to create a list for odd numbers . Python one-liner comes to the rescue.

3. Variable swapping

Variable swapping in python comes very handy and easy to use. You are able to swap values of variables around without using any temporary variable to store these values. Lets look at the below example

Now let’s see the sample of the implementation in a bubble sorting function

4. Multiple assignment

Earlier, we had seen how the * was used to unpack items in a list. A more interesting part is that it can also be used for to hold multiple assignment in a variable.

In conclusion, python will continue to remain a beloved language for beginners not just only because of its strive in artificial intelligence, good community support, but in its simplicity and readability.

--

--