In this Python programming tutorial, we're going to focus on programming Python exclusively in the Interpreter or REPL (read-eval-print-loop). It's a handy tool for quickly executing Python calculations. Not only are you able to write new code on the fly, but you can also import libraries into your session for real time debugging.
If you're unable to install Python, you also have the option to use the browser-embedded python REPL below. Go ahead and type right inside that code window. To make it more convenient, I recommend that you have two windows of this page running side-by-side. This allows you to run the code in one window, while scrolling through the article in another.
We'll going to skip the history lesson, and jump right into some useful Python commands. First up, let's run some math.
>>> 1+2 3 >>> 2*3 6 >>> 4/2 2 >>> 1+2*4/2 5 >>> (1+2)*4/2 6
With the mathematical operators
/, you can perform basic math. Type out these operations like you would on a calculator. PEMDAS rules apply, so use parenthesis when you want to force a specific order of operations.
>>> a = 1 >>> b = 2 >>> a + b 3 >>> b - a 1
Python allows us to store values into variables, in order to reuse them over multiple operations. Variable names must start with a letter or underscore, and should use all lowercase letters with words separated with underscores. The full Style Guide for Python Code is available at python.org.
Python is Strongly Typed
>>> a = "Jane" >>> b = "Doe" >>> "Hello %s %s." % (a, b) 'Hello Jane Doe.' >>> c = 2 >>> a + " " + b 'Jane Doe' >>> a + " " + b + " " + c Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects >>>
Notice that you can override old variables. Furthermore, the new variables can be of a different type. Notice how we've replaced
"x" + 2 converts the output to a string, Python throws an error.
if condition: indentedStatementBlock
The most basic Python conditional is the
Indenting matters in Pythons. According to PEP 8, spacing is preferred over tabs, but whichever you utilize, be consistent. Don't mix spaces with tabs. Use 4 spaces.
When writing out conditionals in the REPL, you hit enter to move to the next line, indent manually, and then hit enter twice when you want to close the conditional. The ellipses are there to indicate that you are within a block of some sort.
>>> c = 2 >>> if c > 1: ... print True ... else: ... print False True
Here, we're asking: if c is greater than 1, print
True to the screen. Else, print
False. We've previously declared
c to be
2. Feel free to reassign
0 and see what happens when you rerun the conditional. Also try to experiment with the following conditional expressions:
|Expression||Math Symbol||Python Equivalent|
|Less than or equal||≤||<=|
|Greater than or equal||≥||>=|
>>> def add(a, b): ... return a + b ... >>> add(1, 2) 3
Here's a function that accepts two numbers and returns the sum.
In Python, we "define" functions with
def. Again, spacing is important.
def is followed by the name of the function and parameters inside the parenthesis.
a stores the first integer, and
b stores the second. The function will then reference
b when it performs the addition operation.
This operation is performed from right to left.
a + bis performed.
- The result is then passed to the left, to the
returnthen passes the result along to the left and out of the function to the function caller.
- In the REPL, standard output is automatically printed to the screen, without having to explicitly call the
After we've defined our function, we invoke it by simply calling it by name and giving it values to store in the paramenters like so:
A function can contain as many parameters as it needs and consists of as many lines for it to do its job. Generally, you want to keep functions concise, focused on one job, and readable. If a great deal of steps need to be performed, it's best to break up each step across multiple smaller functions to make debugging easier.
That's all for this tutorial. In the next chapter of this series, we'll be writing code in
.py files using a code editor. While the Python REPL is great for small calculations, large functions and programs are too unwieldy in this environment.
- Fluent Python: Clear, Concise, and Effective Programming
- Python Cookbook: Recipes for Mastering Python 3
- Introducing Python: Modern Computing in Simple Packages
- Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)
- Head First Python: A Brain-Friendly Guide