User Tools

Site Tools


competitive_programming:python_interpreter

This is an old revision of the document!


Python3 Interactive Interpreter

The Python3 interactive interpreter (typically invoked with python3 on the command line) allows for the entry of arbitrary Python3 commands, making it a great calculator and a great place to test simple ideas.

Interactive Interpreter: Loops

>>> from math import *
>>> for i in range(10):
...  print(int(log(2**i, 10))+1, end=' ') # single space before the print() to simulate indentation
...                                       # additional lines in the loop here or <return> to end
1 1 1 1 2 2 2 3 3 3 >>>

The above fragment computes powers of two, and then uses the decimal logarithm to compute the number of digits in each power.

This can be extended to basic nested structures.

>>> for i in range(3):
...  for j in range(3):             # preceded by a single space
...   print(i, j, sep=',', end=" ") # preceded by two spaces
...                                 # single <return> keystroke
0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 >>>

If you are looking to do anything more complicated, it is likely that you should create a temporary '.py' file.

Interactive Interpreter: Importing Files

The import statement can be used to import arbitrary Python3 files, which can be useful for quick unit tests (and doesn't require further file manipulation to drive tests).

competitive_programming/python_interpreter.1534259855.txt.gz · Last modified: 2018/08/14 10:17 by jguerin