The Basics

Throughout the class, we’re going to do a lot of hands-on, dive-in, figure-it-out-along-the-way work. There’s a good chance you’ll have a lot of moments when you don’t know exactly what’s going on, and that’s okay! I want to keep your hands on they keyboard, and your mind working hard. If you get stuck, don’t worry, I’m here to help.

REPL

As we jump right in, we’re going to start with typing code at the Python REPL.

REPL stands for Read-Execute-Print Loop, but don’t worry about remembering that. What you should remember is that the Python REPL (sometimes called the Python shell or Python command prompt) is a place where we can do quick and dirty Python-y stuff. It’s great for testing things out, and getting familiar with how Python works.

To start the REPL, open up a command/terminal window. In your command/terminal window, type:

$ python3.6

This will start the REPL. You can tell when you are in the REPL because the prompt will change to be >>>.

Note

If you’re on Windows and python3.6 doesn’t work, try py3.6.

We can type code in the Python REPL and once we hit Enter Python will execute our code and print out the result.

Arithmetic

Let’s look at some basic math:

>>> 2 + 2
4

Python has numbers and arithmetic, just like every other programming language.

You can add integers. You can add floating point numbers.

>>> 2 + 2
4
>>> 1.4 + 2.25
3.65

You can subtract, multiply, and divide numbers.

>>> 4 - 2
2
>>> 2 * 3
6
>>> 4 / 2
2.0
>>> 0.5 / 2
0.25

Note that division between two integers in Python 3 returns a float.

>>> 3 / 2
1.5

If you want your integers to round-down you can use a double slash:

>>> 3 // 2
1

We can use double asterisks to raise a number to a power. For example, 2 to the 10th power is 1024:

>>> 2 ** 10
1024

Your Turn: REPL 🏁

REPL Exercises

Let’s play around in the REPL. Open a command/terminal window and type python3.6.

Addition and Subtraction

  1. Add a few numbers, subtract a few numbers, and just get the feel of the REPL.
  2. What happens if you throw spaces in between the numbers? What happens if you don’t?
  3. What about parentheses?

Multiplication, Division and Powers

  1. Play around with multiplication and division. Try big numbers, small numbers, and floats. Is anything surprising?
  2. What do you expect to happen if you raise a large number to the power of a large number? What did happen?
  3. Can you figure out what % does?

Bonus: Text

  1. What if we just want to print text in the REPL? Can you figure out how to do that?