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

Local Storage vs Cookies (The storage showdown)

javascript


Local site storage can open up a ton of possibilities in web design and breath new life into an otherwise static web page. Whether you want to do something simple like "show a welcome message only once", or something more complex.

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.

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