Python tutorial

December 28th, 2021 - Vincent Luciani
The tables below will help you to rehearse Python's most useful functionalities. Use the quick links below to jump directly to the type of information you are interested in.
Choose a chapter below:


Adding logs (adding threadname in case of multithreading)import logging

FORMAT="%(asctime)s %(message)s %(levelname)s %(funcName)s:%(lineno)d"
logging.basicConfig(filename='myfile.log', level=logging.DEBUG, format=FORMAT)

inside each module:
logger.debug("my message")

Get information from a usermyresult=input("message to the user")
print information to the consoleprint("hello")
comment on several linestriple double quote at the beginning and at the end
convert string to integerint(mystring)
convert integer to stringstr(myinteger)
get a variable's typetype(myvariable)
check if a variable is an intisinstance(myvariable,int)
get a random number between x and yimport random
myvar=random.randrange(x, y+1)
Assign a value to a parameter, with a default result if the first assignment is nullmyvariable=firstresult or secondresult
Assign a value to a parameter inside an if statement, and use the parameter further onif (myvariable :=myvalue ) and len(myvariable):
Best practices to define python programs called from the outsidedef main():
put the program here .....

if __name__=="__main__":

Handling Strings

get the nth character in a stringmystring[n-1]
get the length of a stringlen(mystring)
condition: is string within a stringif stringtosearch in mystring:
get string from position x to ymystring[x-1:y]
get string from the start to position ymystring[:y]
get string from position y to the endmystring[x-1:]
get string from position x from the end to position y from the endmystring[-x:-y]
convert to uppercasemystring.upper()
take off spaces before and after in stringmystring.strip()
replace x by y in a stringnewstring=mystring.replace(x,y)
split a string using a separatorresutlingarray=mystring.split(separator)
build a string using a template, with the order of parameter being used to choose the parametermytemplate="hello{1}how are you{0}"
build a string using a template, with the name of parameter being used to choose the parametermytemplate="hello{param1}how are you{param2}"
convert string to lowercasemystring.lower()
string template: take nth variable as double of m digits after zero{}
new line in string
carriage return\r
concatenate string in loop ( best option when the string is growing bigger and bigger at each loop)Use an array to accumulate pieces of string in the loop using append
at the end, transform to string: in this example pieces of string will be separated by ",", you can also use ""


Array's lengthlen(myarray)
Initialize a listmylist=[item1,item2,{"param1":"value1"}]
Types of arraysList is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered and unindexed. No duplicate members.
Dictionary is a collection which is unordered and changeable. No duplicate members.
Property of listsOrdered, changeable, allows duplicates
Same as list but unchangeableTuple
Similar to an objectDictionary
Unordered and unindexed. No duplicate memberSet
Check if value in a listif value in mylist:
Insert element in position x in a listmylist.insert(x-1)
Add an item at the end of the listmylist.append(newvalue)
Add a list at the end of another list or tupplemainlist.extend(otherlist)
Take off a value in a listmylist.remove(myvalue)
Remove nth value in a listmylist.pop(n-1)
Remove the last item in a listmylist.pop()
Delete the entire listdel mylist
Take off the entire content from a listmylist.clear()
Loop through a listfor x in mylist:
Loop through a list with the possibility to use the index numberfor i in range(len(mylist)):
Create a list of number from 0 to nmylist=range(n)
Create a list of number from x to y, with an increment of kmylist=range(x,y,k)
Short hand of loop on list with operation: print all elements in a list[print(x) for x in mylist]

it is called also a list comprehension
Short hand of loop on list with operation: put all elements of list1 containing string str1 in list2list2=[ x for x in list1 if str1 in x]
Short hand of loop on list with operation: put all elements of list1 in list2 but transform to upper caselist2=[ upper(x) for x in list1 if str1 in x]
Short hand of loop on list with operation: put all elements of list1 in list2 only if not equal to str1, else hardcode and put str2list2=[ x if x!=str1 else str2 for x in list1]
Short hand of loop on elements of a dictionary with operation: multiply the value of fields by 2transformed_object={k:v*2 for k,v in my_object.items()}

it is called also a dictionary comprehension
Sort a list ascendingmylist.sort()
Sort a list descendingmylist.sort(reverse=true)
Custom sort functiondef myfunc(x):
return y


the bigger is the return value ( y ) the higher it is in the hierarchy
Case insensitive sortmylist.sort(key=str.lower)
Reverse the order of a listmylist.reverse()
Make a copy of the list ( give two examples )secondlist=firstlist.copy()

you can also do:

Put a list at the end of another listresultinglist=firstlist.extend(secondlist)
Put an element at the end of a listresultinglist=firstlist.append(secondlist)
Concatenate two tuplesresultingtuple=tuple1 + tuple2
Add an item to a setmyset.add(newvalue)
Add a set to another setfirstset.update(secondset)
Taking off a value from a set, not raising any error if the value is not theremyset.discard(myvalue)
Put two sets into one setnewset=set1.union(set2)
Leave in a set only elements that are also in another setmyset.intersection_update(otherset)
Create a set having elements that are both in set1 and set2newset=set1.intersection(set2)
Leave in a set only elements that are not in another setmyset.symmetric_difference_update(otherset)
Create a set having elements that not common between set1 and set2newset=set1.symmetric_difference(set2)
Access the value of an element of a dictionarymyvalue=mydictionnary["mykey"]

you can also use ( safer in if statements) :

Access the value of an element of a dictionary, giving a default value if the element does not existmyvalue=mydictionnary.get("mykey","mydefaultvalue")
Get the list of keys in a dictionnaryfor x in mydictionnary.keys():
Get the list of values in a dictionnarymydictionnary.values()
Convert list of keys in a dictionnary to be able to take element number nresults=list(mydictionnary.keys())
Get the list of values pairs in a dictionnarymylist=mydictionnary.items()
Faster way of initializing a dictionnarymydictionnary={param1:"value1","param2":{"param21":"value21"}}

param1 is a parameter containing a string. Parameters can be put in the place of keys
Check if a key is in a dictionnaryif mykey in mydictionnary:
Remove item from a dictionnary using a keymydictionnary.pop(mykey)
Add elements from one dictionary to anothermydictionnary.update(additionaldictionnary)
Empty a dictionnarymydictionnary.clear()
Loop through all the keys in the dictionnaryfor x in mydictionnary:
initialize a listmylist=["val1","val2"]
initialize a tuplemytuple=("val1","val2")
initialize a dictionarymydictionnary={"var1":"value1","var2":"value2"}
initialize a setmyset={"val1","val2"}


Else if in pythonelif
Go to the next iteration in a while loopcontinue
Stop a loopbreak
If else if elseif condition:
elif condition2:
Execute code at the end of a loopelse:
Shorthand for if elseprint("hello") if condition else print("something else")
Condition: field is nullmyfield==None

Careful in some cases the value is 'null' (must check when debugging)
condition : is alphanumericisalnum()
condition : only letterisalpha()
condition : only numbersisnumeric()
condition: is a listisinstance(mylist, list):


Function with a variabledef myfunc(x):
return y
Function with number of variables unknown, use the nth argumentdef myfunc(*x):
Call function by giving the variable names and valuesdef myfunc(var1,var2):

Call function with number of variable unknow by giving the variable names and values, in the function use value of var1def myfunc(**myvars):

Call function with number of variable unknow without giving the variable names and values, in the function use value of the first variabledef myfunc(*args):

Define function with default value to param1def myfunc(var1=defaultvalue):
Create a lambda that multiplies x by y and use it as argument to printmylambda=lambda x,y: x* y
Function returning a parameterized lambda ( multiplying input by the parameter)def mylambdacreator(param1):
return lambda x : x * param1

Create a lambda in such a way that you can cope with the situation where you don't know if it will be called with one argument or twolambda *args: args[0] if len(args)<=1 else args[0]+args[1]
What can local function do that non local functions cannot doThey can reference parameters outside of the local function but within the function (main function) including this local function. This applies also to the main function's input parameters
This is a closure
Example of use of local function to create a factory of functionsdef augment(myfactor):
def multiply(inputnumber,myfactor):
return inputnumber*myfactor
return multiply

myresult=newfunction(4) -> gives 8
non-local scopeparameter defined in function (and not returned) stays in the scope of the function and is out of scope outside of the function.
parameter nonlocal makes so that it is still in scope outside of the function. At the same time it is not global, so if this function has a parent function, the parameter will out of scope after the parent function is called
global scopeIf you declare : global myvariable, and you assign a value to it, the scope will be outside of any called function's scope

If you have func1 setting myvariable=1 , calling func11, and inside func11 you define global myvariable, and set myvariable=2 , then:
inside func1 value will be 1, outside func1 ( global scope ), value will be 2
example of use of local function and non local scopeTimer function that creates a timer, with local function to calculate time elapsed since last call ( last call is a non local variable inialized to None in the timer function) declaring last call as nonlocal
Call to timer function (creation of the timer) will initialize the last call time to None, but at each call of elapsed(), the last call setup during last call of elapsed will be kept in memory at the timer() function level
Principle of decoratorsModify or enhance functions without changing their definition
Takes a function as argument and return another function that modifies the original function
Simple example of decoratorsdef convert_to_string(f):
def wrapper_function(*args,**kwargs):
return str(result_before_modification)
return wrapper_function

def multiply_by_two(*args,**kwargs):
return 2*kwargs.get("input")

How a class must be structured to be able to be used as a decoratorclass MyClass():
make use of self.f (function to modify)
return self.f(args*,kwargs**)

can be used for example to increment a counter each time the modified function is called:

You can add also use wrappers to login information, or to validate arguments passed to the function
Use of more than one decorator@decorator2
def mymodifiedfunction():

careful, the decorator applied first is the one nearer the function name (decorator1 in this example). Then decorator2 is applied to the function returned by decorator1
Have the decorator inherite meta data from the modified functionAdd @functools.wrap(f) before the definition of the function that the decorator will return

def convert_to_string(f):
def wrapper_function(*args,**kwargs):
return str(result_before_modification)
return wrapper_function
Decorator and parametersdef convert(type):
def convertor(f):
def wrapper_function(*args,**kwargs):
if type=="string":
return str(result_before_modification)
elif type=="integer":
return int(result_before_modification)
return wrapper_function
return convertor

def multiply_by_two(*args,**kwargs):
return 2*kwargs.get("input")
Decorating a function belonging to a class, and selfdef convert_to_string(f):
def wrapper_function(self,*args,**kwargs):
return str(result_before_modification)
return wrapper_function


Class with constructor and functionclass MyClass:
def __init__(self,param1,param2):
def myfunction(self,param3):

Instantiate a classmyobject=MyClass(param1,param2)
Delete an objectdel myobject
Create a child class, with constructor using parent class and adding a variableclass MyChildClass(MyParentClass):
def __init__(self, oldParam,newParam):
Create an interfacefrom abc import ABC, abstractmethod
class MyInterface(ABC):
def __init__(self,myparams):

def myfunctions(self, myparams):
Implement an interfaceclass MyClass(MyInterface):


Access an element of a moduleIn

In the calling script:
import mymodule
List elements belonging to a modulemylist=dir(mymodule)
Import only a given element from a modulefrom mymodule import myelement
import from folderfrom foldername import filenamewithoutextension

You may need to put an empty file in the folder with the following name:

Date and Time

Get the current date and timeimport datetime
Create a date object representing March 8th, 2005import datetime
Format a date object to show YYYY-MM-DD hour:minutes:second:microsecondx.strftime("%Y-%m-%d %H:%M:%S:%f")
Format a date object to show Month in English, day of the week in English x.strftime("%B %A")


Round upceil
Round downfloor


Convert json to a dictionnaryimport json
Convert dictionnary to jsonmyjson=json.dump(mydictionnary)
Convert dictionnary to json, giving an indent of 4 charactersmyjson=json.dump(mydictionnary,indent=4)
Convert dictionnary to json, sorting keysmyjson=json.dump(mydictionnary,sort_keys=true)

Regular Expressions

Condition using a regular expressionimport re
Get the first element when using a searchimport re,mytext)
if myresult:
Pattern: my string starts with x and end with ymypattern="^x.*y$"
Get a list containing all the matches of a patternmylist=re.findall(mypattern,mytext)
Split a string in a list using a regular expressionmylist=re.split(mypattern,mytext)
Split a string in a list using a regular expression but split only a the first occurrencemylist=re.split(mypattern,mytext,1)
Perform a replacement using a regular expression. Replace first n occurencesmyresultingstring=re.sub(mypattern,replacingstring,mytext,n)
Regular expression with groups : find all matching pattern and for each match, get first group and second group mytuples=re.findall(r'mypattern', mystring)
for matchingtuple in mytuples:
print matchingtuple [0]
print matchingtuple [1]
Regular expression: if the pattern contains parenthesis ( forming groups ), find the different, mystring)
Regular expression : in if statement, condition that match is foundif, mystring):
Regulary expression : any character any number of times, but greedy.*?
Match letter digit or underscore\w
Beginning or end of word\b
Match a whitespace character\s
Match a number\d
Match only at start of string\A
Match only at end of string\Z
Repetition: zero or one?
Repetition: one or more+
Repetition: zero or more*
Repetition: from n to m times{n,m}
Repetition: n times{n}
Repetition: more than n times{,n}
Match any character.
Match word1 or word2[word1|word2]
Match any character except a,b,c[^abc]
List of characters to escape. ^ $ * + ?{[ ] \ | ( )


Get version of pippip --version
Install a modulepip install mymodule
Install a module, using version greater than npip install 'mymodule>n'
Install a module, using version npip install mymodule==n
Install the last version of a modulepip install -U mymodule
Install the last version of pip itselfpip install -U pip
purpose of setup.pyfile called when installing the module with pip
inside you have for example install_requires=[...
that gives the list of modules required
Find a package
Remove a packagepip uninstall mymodule
List the installed packagespip list

Virtual Environments

Create a virtual environmentpip3 install virtualenv
create a directory for virtual environments
go to directory and type:
virtualenv -p python3 virtualenvname
"C:\Users\sesa201795\AppData\Local\Programs\Python\Python38\python.exe" -m venv virtualenvname

inside Scripts, :
you will see your env name in parenthesis on the left ( before the prompt)
you will be in a virtual env. From there you have your own python exe and pip exe.
from there install packages with pip
you can launch your script from there (first go to the directory where you script is) , it will use packages you installed from this virtual env.

keep virtual environment folder separate from separate project

Go out of the virtual environmentType "deactivate" and press enter
Create a requirements file with a description of all packages installedpip freeze > requirements.txt (inside the main directory, from the virtual env directory you created, and after activating the virtual env with activate)
Install all packages based on the requirements filein another environment:
pip install -r requirements.txt
Run your program in visual studio code using your virtual environmentWhen running your program with code editor, choose to run with your virtual environment by selecting the python.exe located in a virtual environment folder
In visual studio code: Ctrl+Shift+P=> type python:s
Run your program in pycharm using your virtual environmentFile, project structure, modules tab on the left, select the current module you work on, then on the right select the tab Dependencies. In module sdk combo box choose add sdk, python sdk, existing environment ( if you have already prepared the environment), then click on button with 3 dots and select the python.exe file in the venv/scripts folder

Error Handling

Try catchtry:
except Exception as e:
raise Exception( "My error: %s" % str(e) ) from None
you can also log the information instead of raising the exception in case you need to still go on with the process.
Try catch, and execute commands whether there is an error or not
(for example to close a connection or a file)
Raise an exceptionraise Exception(mymessage)

File Handling

Open a text file for writing from scratch and write a linemyfile=open("myfile.txt","w")
Open a text file for readingmyfile=open("myfile.txt","r")
Open a text file for writing at the end of the filemyfile=open("myfile.txt","a")
Read the full text of a
Loop through the file, line by linefor myline in myfile:
Close a fileclose(myfile)
Check if a file or folder existimport os
if os.path.exists(myfile):
Remove a fileimport os
Remove a folderimport os
Extract data from pdfimport pdfplumber

def read_pdf_file(path):
with as pdf:

for page in pdf.pages:
"," ")

full_document_extract=" ".join(full_extract)

return full_document_extract


Get answer from soap request headers={'content-type': 'application/xml'}

body=""" xmlns:doc="http://mydocumentserviceaddress/">

... as per specification




Conditional breakpointAdd a breakpoint, then right click on the break point and in "condition" add a condition ( piece of code as you would write on the code itself, for example:
start > max
Format codeCtrl+Alt+L
Go to the places where the function was calledCtrl+B
Go to the place where the function was defined ( coded )Ctrl+alt+B
Show the list of functions on a page with links to go directly to them ( file structure )Ctrl + F12
Go directly to a class knowing its nameCtrl + N and start entering the class name
Refactor / rename shortcutShift + F6
Select an area of code ( for example everything under an if statement or a while loop)Ctrl + w ( do it several times if there are parent areas)
Change python version used on the python consoleFile, settings, Build execution ..., python console: python interpreter
Change python version used when checking the syntax in the projectFile, settings, Project:....., Python interpreter
Search for elements in the codectrl+alt+f -> can click on small button on right to turn on regex + use button to pin the window.
While the program is stopped at a breakpoint, use complex execution to get a valueOn the debug tab at the bottom, on the console tab within ( where print() message are shown), enter the logic where the >>> is as if you were typing a python program after entering the command python.
In case of if statement, press enter twice at the end of the statement
Show the git information and functions on the left hand side ( commit tab )File, settings, version control, commit: use non-modal commit interface
Setup to be able to display documentation of functions and classes when hoveringFile, settings, tools, python integrated tools, docstring, docstring format: Google, check "Analyze Python code in docstrings" and "render .... "
Document a function or classdef function_documented():
what is the function doings

myarg (type): my explanation

myarg1 (type): my explanation
myarg2 (type): my explanation



For bullet points: leave a blank line before the first bullet point, and indent with 4 spaces
Install a pluginFile, settings, Plugin, search for plugin, eventually click on search on the marketplace
Useful pluginsKite AI code autocomplete: autocomplete

To use mypy:
mypy tab
right click on file or folder: inspect code
See branches graphicallyalt+9
What to do if you forgot to pull changes from master before created your branche, and then you worked on this branch. As a result you missed a lot of changes on masterOn the bottom left of Pycharm, you have your branch selected.
Click on it, select master, then update ( to update master).
Then reclick on Master, merge into current.

If you get a list of files with conflicts, click on button Merge
you get a screen with 3 column:
left: your code, center: result of merge, right: master
everything from left or right should be either rejected or accepted. If conflict is too complicated, change result column manually, and then reject on left and right.
At the end click on apply

Other tactic:

checkout master: git fetch origin ( or git pull from master )
checkout yournewbranch:
click on your branch on top down of pycharm. Then remote branch, origin/master -> rebase onto selected (select master)
Make a scratch file for proof of conceptctrl+alt+shift+insert, then choose the language
move file from project to scratchIf scratch directory is not there, create any scratch file to have this directory automatically created. Then drag and drop your file to this newly created Scratches folder under "Scratches and console"
Select entire columnsCtrl+insert or, if you do not have an insert key:
Press Ctrl+Shift+A (Find Action feature) and type column. The first result is Column Selection Mode, click on On to turn it on.

Select text with keyboard (by holding Shift)

Once done, using Find Action Feature, turn it off.
Shortcut to see difference between expected result and real result in unit testclick on the window showing the results, and ctrl+D

Unit Test

basic unit testimport unittest

class ThisIsMyTest(unittest.TestCase):
def test_this_thing(self):
Unit test with nose2 and a parameterFile must be called test_.... and be in folder test

from import params

def test_is_ok(value):
assert value.startswith('a word')

Then on root of project, on console, run nose2
Unit test coverage with branch testingpip install coverage

From the root of the project ( to check coverage of tests in folder test):
coverage run --source=./sourcecodefolder --branch -m unittest discover -s test/ && coverage report

To get the missed coverage:
coverage run --source=./sourcecodefolder --branch -m unittest discover -s test/ && coverage html
Then go to
htmlcov folder, open index.html in browser, then click on the file having coverage not 100%
Run unit test in PycharmEdit configuration, then add, choose python unit test, choose pytest in bin
select the folder where your tests are

go to File| Settings | Tools | Python Integrated Tools , choose pytest as default test runner. If you get the warning “No pytest runner found in selected interpreter”, click the Fix button.
Best practices unit testName of test function says exactly why is tested
Per test function, only one group of{preparation, running, asserting)
Simple stub in python, without using any frameworkIn the function using the function to stub:
class MyClass:
def __init__(self,stubbingobject=false):
myresult=stubbingobject or objecttostubb()

In the test using the function:

Dummy versus stub versus fakeDummy usually a null value parameter
Stub gives a hard coded answer, and is always passing the test
Fake is more ellaborate, although not production ready, and can fail the test if the input is not correct
Record a print to be able to compare it to an expected printimport sys

from io import StringIO


print("hello world")

captured_value=captured_output .getvalue()
print ('Captured:', captured_value )


Get a configuration object from a yaml fileimport yaml

def get_config_from_yaml(path):
with open(path, "r") as ymlfile:
cfg=yaml.load(ymlfile, Loader=yaml.FullLoader)
return cfg

cfg will be like a dictionnary. Lists will be like lists within the dictionnary
Configuration with one parameter, one parent and child parameters, and one listmyparameter: myvalue
mychild: mychildvalue
- myvalue1
- myvalue2

Parallel versus asynchronous

CPU intensive tasksString operation
search algorithms
graphics processing
number crunching algorithms
Tasks involving waitingDatabase reads, writes
web service calls
copying, downloading, uploading data
When parallel programming bestParallel programming best for CPU intensive tasks
When asynchronous bestTasks involving waiting
Package to run tasks in parallelMultiprocessing package
Package for asynchronous
What is a threadsmallest sequence of instruction that can be executed by the operating system
Run two tasks in parallelimport threading

class MyStuffToDo(threading,Thread):
def __init__(self,myparam):

def run(self):



Create thread inside a loop threads=[]
for myvar in mylist:
# carefull to put the , after the last parameter

for t in threads:
Create thread that never stopsthreading.Thread(daemon=true)
Create a thread with an undefinite list of argumentsthreading.Thread(kwargs={param1,param2,....})
Launch asynchronous function from main program (synchronous)import asyncio

Declare asynchronous functionasync def myfunction(myparameters):
Launch asynchronous function from main program (synchronous)myresult=await myfunction(myparameters)
Define a semaphore to limit the use of a ressource to onesemaphore=threading.Semaphore()
Define a semaphore to limit the use of a ressource to nIn the main program:
Reserve a shared ressource when working with threadsAt the beginning of the action for which you want to prevent more than n run at the same time:
Unreserve a shared ressource when working with threadsAt the end of the action

This website is non commercial and does not register any of your personal data (no cookie, no statistics). The site and its content are delivered on an "as-is" and "as-available basis".