Normally, there's an ad running in this spot, but you're using an ad blocker. To help support our blog, which provides free tutorials for everybody, we just ask that you whitelist or follow us on facebook, twitter, or subscribe using the form to the leftabove. Thank you!

    Python Software Development Using the Python REPL

    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.

    Python Fundamentals

    We'll going to skip the history lesson, and jump right into some useful Python commands. First up, let's run some math.

    Python Operators

    >>> 1+2
    >>> 2*3
    >>> 4/2
    >>> 1+2*4/2
    >>> (1+2)*4/2

    With the mathematical operators +, -, * and /, 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
    >>> b - a

    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 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 a and b's ints with strings (letters). This is possible because Python is both a dynamically typed and strongly typed language, allowing you to reassign variables with different types. But unlike a weakly typed language like JavaScript, where adding a string to a number "x" + 2 converts the output to a string, Python throws an error.


    if condition:

    The most basic Python conditional is the if else.

    Note: Indenting in Python.

    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

    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 c to 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 < <
    Greater than > >
    Less than or equal <=
    Greater than or equal >=
    Equals = ==
    Not equals !=


    >>> def add(a, b):
    ...     return a + b
    >>> add(1, 2)

    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 a and b when it performs the addition operation.

    This operation is performed from right to left.

    1. First, a + b is performed.
    2. The result is then passed to the left, to the return statement.
    3. return then passes the result along to the left and out of the function to the function caller.
    4. In the REPL, standard output is automatically printed to the screen, without having to explicitly call the print function, so the result gets passed of to the REPL to get displayed.

    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: add(1, 2).

    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.

    Did you like this tutorial? Help us pay for server costs by following us on Facebook, Twitter, and subscribing below, where you'll get post notifications, training webinar invites, and free bundles.