Code Snippets

Loop and Print

To print numbers with a leading zero use this:

for i in range(15):
    print('Num: {0:02}'.format(i))

Get Python Version

As usual, there are a couple of ways to do this:

import sys
print("Python Version: %s.%s.%s" % sys.version_info[:3])
print("Python Version: %s.%s.%s %s %s" % (sys.version_info.major, sys.version_info.minor, sys.version_info.micro, sys.version_info.releaselevel, sys.version_info.serial))
print(sys.version)

import platform
print(platform.python_version())
Whilst the output does differ with these it is better to run them and see, however sys.version displays a lot of detail including build information and so will vary by platform. It is my opinion that sys.version_info is the best option, giving precise control.

The source code for this is available at python/python-info.py · master · Geoff Lawrence / Geoff Does Stuff · GitLab

Current Interpreter

The recommended way to establish the current interpreter, its location and possibly to reinvoke it is to use sys.executable.

Handy Code

type(i)- will display what type i is
type(i) is int - returns true is i is an int and false otherwise

Data Structures

If you need a reminder on how to use a Python data structure then 5. Data Structures — Python 3.7.1 documentation is a good starting point. This page covers, lists, tuples, sequences, dictionaries and how to work with them.