Learn Python – Strings, Numbers, and the REPL

Sun Aug 28, 2016 - 1100 Words

Today we’re going to start our first series on a programming language, and we’ll be digging into Python. Python is a wonderful language to learn and to start with because it is used in so many different fields, from web programming, to data-science, all the way to scripting animations. We’re going to start from the beginning, assuming no prior knowledge of programming and see where this journey takes us. In today’s episode specifically we’re going to become familiar with the a few basic types in python and how we can run our code as we’re developing it.

Goal for this Tutorial:

  • Become comfortable in the REPL for trying out code in Python.
  • Create our first python script and learn to run it from the command line.
  • Investigate a few of the basic types in Python (strings, comments, and numbers).

For this tutorial we’re going to use the official Python image from Docker Hub so that everyone is at the same starting point. If you’ve never used Docker please watch my series on using Docker for Development. To get the image that we’ll be using you can run:

$ docker pull python:3.5

After you’ve downloaded the image start a container running bash so that we can run commands as though we were running python on our machine:

$ docker run --rm -it python:3.5 bash

Getting Started with Python

The first thing we’re going to do is run the python command from the command line:

$ python

You should see something like this:

Python 3.5.2 (default, Aug  9 2016, 20:58:38)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

This puts us at a prompt known as a REPL, which stands for Read, Evaluate, Print, Loop. We can type lines of Python code here and they will be run write away. REPLs are a great place to play with a language and learn what works and what doesn’t without worrying about setting up a script and running the script.

The first thing every programmer does in any programming language is write his/her “Hello World” program. This program prints the words “Hello World!” to the screen and depending on the language can take quite a few lines of code, but that’s not the case in Python. Let’s write our Hello World now:

>>> print("Hello World!")

You’ll notices that right underneath the line with three greater than symbols it now says “Hello World!” because we printed it out. Breaking this down we needed to use the built-in function print to get our words the print to the screen and we passed in the String “Hello World!”. A string is anything within either single or double quotes in Python (and more programming languages). We’ll be using these a lot, and a good way to think of strings is that they are used if we want to represent words, phrases, or numbers with specific formatting, for instance currency “$1.00”.

Introducing Numbers and Math

Beyond printing our strings, one of the things that we’ll use a lot in Python are numbers. It is pretty common to want to do Math and numbers for the most part work as expected. There are 2 main types of numbers that we’ll work with in Python: Integers and Floats.

Examples of Integers would be the numbers 1 or -12. Examples of Floats would be the numbers 2.5 or -0.5. Floats are the type that we use to represent decimal numbers.

Let’s do some math using these new number types:

>>> 1 + 1 # Addition
2
>>> 10 - 4 # Subtraction
6
>>> 5 / 3 # Division, notice that it returns a float instead of an int
1.6666666666666667
>>> 5 // 3 # Floor division, always returns an int
1
>>> 3 * 9 # Multiplication
27
>>> 8 % 3 # Modulo division, returns an int representing the remainder
2

Creating a python script

We now know how to interact with the Python REPL and work with strings and number types. The last thing we’re going to look at is how to put our code in a script so that we can work with it over type and run more than one line at a time.

For this we’re going to need to stop our container so that we can share through our local directory as a volume to allow us to work with our python script in our text editor. Exiting the REPL requires either hitting Ctrl+d or using the function exit()

$ docker run --rm -it -v $PWD:/usr/src/ -w /usr/src python bash

The -w flag is being used here so that as soon as we’re in the container we’re already in the proper directory to see our source code.

Now let’s open up a new file in our text editor called planets.py.

hello.py

1 + 1
5 / 4

print("There are %s planets" % 9)

Save the file and then from within your running container run

$ python planets.py
There are 9 planets

Our first 2 lines did the math behind the seen and they returned 1 and 1.2 respectively, but we didn’t tell python to print them so we never saw them. on our 4th line we joined our strings and numbers together by using what’s called a format string. The %s is replaced with what follows the % after the string. We could put multiple values in the string also, but we would need to group the values that we want to put in the string together using parenthesis and commas.

Let’s adjust this since I really don’t know what Pluto is at the moment.

planets.py

print("There are %s planets, plus %s" % (8, "pluto"))

Recap

Today we learned how to use the Python REPL, wrote our first Python script, and learned about strings and numbers. This is a good first step in our Coder Journey. In the next episode we’ll look at how we can store information in variables, group pieces of data together in lists, and package up our code to run it again and again using functions.