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

CSS Material design toggle switch

html - css - javascript


Because sometimes all you want is just a good looking switch without importing an entire library to do it.

Responsive web design guide

css - html


In 2017 it was recorded that around 63% of all U.S web traffic is made up of mobile devices. It is becoming increasingly vital that websites adopt a responsive design to accommodate this rapidly growing portion of the market.

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.

karnamalone@gmail.com