-
Notifications
You must be signed in to change notification settings - Fork 0
Interpreter
Python is an interpreted language (unlike C and C++ which are compiled). Another term would be a scripting language. While this does bring longer execution times than other languages (being 10 to 100 times slower than C), it allows for a faster development and has a wide array of modules from machine learning to statistics and asynchronous code (such as chat bots).
The REPL (Read Evaluate Print Loop) runs on a command line and can be called through python
. As the name suggests, it loops while awaiting for an input, interpreting it and printing its result. Unlike other python code, a print() may not be necessary for the interactive interpreter to output to standard out (the terminal in our case).
Warning: As python is a language where tabs have meaning, extra spaces or tabs before the input will result in an error.
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
If you need multiple lines of code at once (for example an if statement) you simply enter a new line and continue writing (the prompt symbol will change from >>>
to ...
).
>>> if 1 == 1:
... print("True")
...
True
It will also ignore comments (preceded with #
).
>>> # This is a comment
... print("Hello world!") # Another comment
Hello world!
To exit the REPL type quit()
. Certain OS specific keybinds also work, in windows Ctrl+Z and in Unix Ctrl+D.
We highly encourage you to try the REPL, as it provides an easy and quick way to try new things without having to write and run the code from a file.
This funcion returns the attributes of an object.
For example, to know all the attributes of a string:
>>> dir("anyString") # or dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
This function can help you get all the attributes when your IDE doesn't show it or when you don't have access to the internet.
If you ever need documentation but you don't have access to it, calling it without arguments (help()
) will bring it up for you. This function's true capabilities lie within the REPL.
If you call help()
with an argument you can see the documentation of it specifically.
For example, if you call it with int
, str
or float
it will bring up the respective documentation of their data types. If you call it with a class, function or method it will give you its documentation, for example if you wanted to know what split
(that you found with dir) does:
>>> help(str.split)
Help on method_descriptor:
split(self, /, sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string.
sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
maxsplit
Maximum number of splits to do.
-1 (the default value) means no limit.
If help is called with a variable in the argument it will assume its datatype as the argument.
Note: In windows, sometimes too much information will show up, if you want to stop the help function in the REPL simply type Ctrl+C to stop it.
For those who dislike print debugging or whose IDE does not include a debugger, Python comes with a debugger in the module pdb.
To use it simply include include pdb; pdb.set_trace()
. In Python, semi-colons can be used to have two statements in the same line, although it is usually discouraged.
A table with useful pdb commands:
Command | Purpose |
---|---|
h, help | List the commands available |
n, next | Execute the next line |
c, cont, continue | Continue executioin until a breakpoint is hit |
w, where, bt | Print a stack trace showing where execution is |
u, up | Pop up a level in the stack |
d, down | Push down a level in the stack |
l, list | List source code around current line |