Handy Python tricks for beginners

09 Dec, 2018

When first starting out in a language like Python, things tend to move a bit slow until you learn how to use the built-in functions and operators which exist to speed up the development process. So as to try and mitigate that problem, this tutorial will go through a few of the most useful tips and tricks that beginners in Python should know.

Side Note: These techniques have been tested in Python3 so if you're having problems getting some of them to work, I would recommend checking your Python version.

Importing modules
Python allows you to import modules which are files that contain definitions and statements that you can use in your program.


>>> import numpy as np
>>> import pylab
		

String repetition without loops
You can multiply a string by a number "n" and then the result will be that same string repeated n times.


>>> n = 5
>>> print("Hello"*n)
HelloHelloHelloHelloHello
		

Swapping the value of two variables in one operation
Rather than using an extra temporary variable to perform the swap, this is a compact alternative that can speed up your code and also improve its readability.


>>> x = 3
>>> y = 8
>>> """swapping x and y"""
>>> x, y = y, x
		

Copying a list
Using Python's list.copy() method you can easily create a duplicate list. Unfortunately, this method is only available in Python3.


>>> some_list = [2, 4, 6, 8]
>>> some_list_copy = some_list.copy()
>>> some_list_copy[0] = 100
>>> print(some_list)
[2, 4, 6, 8]
>>> print(some_list_copy)
[100, 4, 6, 8]
		

Exponents notation
Calculating n to the power of k can be done using Python's exponent notation "**".


>>> 2**5
32
		

Reversing a list
Python provides a simple function to reverse the order of a list using reversed().


>>> some_list = [1, 2, 3, 4, 5]
>>> reversed = list(reversed(some_list))
>>> print(reversed)
[5, 4, 3, 2, 1]
		

The lambda keyword.
You can declare functions in a compact one line format using the python lambda keyword.


>>> multiply  = lambda a, b: a * b
>>> print(multiply(6, 2))
12
>>> """is the same as"""
>>> def multiply (a, b):
...    return a * b
>>> print(multiply(6, 2))
12
		

Joining a list of elements into a string
The .join() string method in Python provides a simple way to join a list of elements in a list into a single string.


>>> sentence = ['wow', 'thats', 'handy']
>>> print('-'.join(sentence))
wow-thats-handy
		

Other Tutorials

View All

How to use jQuery's ajax to load dynamic content

javascript - php


Providing dynamically loaded content instead of loading a whole new page can go a long way in terms of improving the experience your users have on your website.

How to center things with CSS

html - css


Being able to center text, divs and images is one of the most important things that you can utilize to create clean and modern websites.

Detecting system dark mode in CSS

css


The "dark mode" feature has been spreading to apps and websites far and wide recently. It brings a new and discrete look to your applications as well as offering some extra control to the user...