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)

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

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))
>>> """is the same as"""
>>> def multiply (a, b):
...    return a * b
>>> print(multiply(6, 2))

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))

Other Tutorials

View All

Beginners guide to Regular Expressions

javascript - php - python

Regular Expressions (RegEx) are an extremely powerful tool that is common across most popular languages in use today. It can be used to find, replace and match text using a specialised instruction set.

Using variables in CSS

css - javascript

When styling your web app, you've probably found yourself copying hex colour codes, margin parameters or other styles to several elements to achieve a consistent design across your app.

Five practical .htaccess tricks

apache - php

Apache's '.htaccess' has some very powerful tools that you can use with minimal effort to enhance the user experience and even performance, of your website.