You write your Python code in a text file with a name like
hello.py. How does that code Run? There is program installed on your computer named "python3" or "python", and its job is looking at and running your Python code. This type of program is called an "interpreter".
A nice benefit of using an interpreter is that you can start interactive version of the interpreter and type Python code right into it to see what it does. What happens if you use
+ in Python code like this:
'hello' + 3?
You could look up the answer to this, but a very quick way to find out is to type code into the interpreter and see what it does.
There are 3 ways to get the interpreter:
1. On parlante.org, use the hack mode interpreter page (there's a button at bottom of each problem page). This is kind of a nick science-project experiment, but it has the virtue that it works in the browser and doesn't depend on having Python installed on your computer.
2. Open a command-line terminal. Mac: run the "Terminal" app in the Utilities folder. Windows: type "powershell" in the lower left, this opens the Windows command line terminal. In the terminal type the command "python3" ("python" on Windows). This runs the interpreter program directly. Type ctrl-d to exit when done (on Windows ctrl-z).
3. If you have PyCharm installed, at the lower-left of any window, click the Python Console tab. Or use the Tools > Python Console menu item.
The interpreter prints a ">>>" prompt and waits for you to type some Python code. It reads what you type, evaluates it, and prints the result - the so called read-eval-print loop (here what the user types is shown in bold)
>>> 1 + 2 * 3 7 >>> 'hello' + 'there' 'hellothere' >>> max(1, 5, -2) 5 >>> 'hello' + 2 TypeError: can only concatenate str (not "int") to str
This is an excellent way to experiment with Python features to see how they work. If you have some code you are thinking about, it can be quick and informative to fire up the interpreter, and type the code in to see what it does.
It's possible to write multi-line indented code in the interpreter. If a line is not finished, such as ending with ":", hitting return does not run the code right away. You can write further indented lines. When you enter a blank line, the interpreter runs the whole thing. (For throw-away code like this, I am willing to indent just 2 spaces).
>>> for i in range(10): ... mult = i * 100 ... print(i, mult) ... 0 0 1 100 2 200 3 300 4 400 5 500 6 600 7 700 8 800 9 900
What are all the functions that the
str type has? You could look it up, but it's also possible in the interpreter to pull the list of function names directly out of Python this (
str is the name of the built-int string type):
>>> 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']
The above is not the most pretty form of the data, as we're just dumping the names of Python's internal structure. Names that begin with 2 underbars like
__add__ are special internal functions that code does not normally need to know about. You can see regular functions like
lower() and 'find()` in the list.
With the special help function, you can pull up the PyDoc for each function (the triple-quote comments for a function). To refer to a function within the
str type use the form
str.find. Note that the function name is not followed by parenthesis - this is the unusual case of referring to a function but not calling it.
>>> help(str.find) find(...) S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.