# Handy Python tricks for beginners

## Importing modulesPython 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 loopsYou 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 operationRather 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 listUsing 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 = 100
>>> print(some_list)
[2, 4, 6, 8]
>>> print(some_list_copy)
[100, 4, 6, 8]
``````

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

``````
>>> 2**5
32
``````

## Reversing a listPython 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 stringThe .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
``````