Glossary

Select one of the keywords on the left…

Programming in PythonIntroduction

Reading time: ~20 min

This chapter is an introduction to programming in Python, which is a general-purpose language with a very large user base in the software engineering world. With the emergence of a powerful stack of scientific computing packages since the early 2000s, it has emerged as the most popular language for data science.

Although programming is a powerful tool, learning to program is also about honing your problem solving skills and thinking in an organized way about structure and computation. You are likely to find that computer science ideas support your ability to reason about complex systems, even in situations where you won't be programming anything. This is a useful frame of mind to bring to the learning process.

This course contains many exercises. Doing them in earnest is essential for knowledge and skill retention. You should solve each exercise prior to clicking the "Continue" button to see an example solution.

Installation

There are several ways to access Python:

Inline. This course will let you execute Python code blocks in the webpage (thanks to Juniper and Binder). So if you don't want to install anything yet, you don't have to. (However, the first cell you run will be slow with this method, like up to 30 seconds, since your environment has to be launched behind the scenes on Binder's servers. If it's taking too long, reload the page.)

Binder. You can also run Python code in a notebook on the Binder website. To launch with a set of packages tailored to this course, click here. Then, select New (top right corner and Python 3). It is highly recommended that you keep a tab with a Binder notebook open while working through this course, because it can serve as a space for scratch work, and it provides more features than the blocks which appear in-page.

Anaconda. Python is bundled with its system of scientific computing packages and for managing Python environments in a distribution called Anaconda. This is the recommended way to install Python on your own computer. Download and launch the installer to set it up on your computer.

CoCalc. If you want a complete environment without having to install anything locally, CoCalc is a batteries-included, community-oriented platform for open-source mathematical and scientific computing. You can use it for free with limited functionality, and it's $14 per month to support the project and get paid account features.

Usage

Once you have Python installed, there are several ways to interact with it.

REPL. Launch a read-eval-print loop from the command line. Any code you enter will be executed immediately, and any values returned by your code will be displayed. To start a session, open your operating system's Terminal and run python or ipython (the latter being more colorful and having more features). You can do this in Binder by selecting New > Terminal.

Script. Save a file called example.py and run python example.py from the command line (in the same directory as the file) to execute all the code in the script. You can do this in Binder by selecting New > Text File and then changing the name of the text file to something that ends in .py.

Jupyter. Like a REPL, but allows inserting text and math expressions, grouping code into blocks, etc. This is the interface provided by default in Binder, and you can launch a notebook locally by running jupyter notebook from the command line (assuming you have Anaconda installed).

Integrated development environment (IDE). Essential for extensive software development projects, an IDE provides an editor for writing code, conveniences to help you code more efficiently, and a debugger to help you fix your mistakes. There are many IDEs for Python, including Visual Studio Code, Atom, and PyCharm.

Exercise. Sort the following Python interaction modes in the order in which they appear in this video.

REPL
Script
Jupyter