Introduction to Python

What is Python?

Python is a prerequisite for programming experiments in oTree. However, it is much bigger than that. It is one of the most popular general-purpose programming languages in the world. Arguably, its success partially lies in its simplicity. And that is good news for us!

Another good news is that, Python has a fantastic ecosystem of libraries. This means that it is not necessary to reinvent the wheel each time. For instance if you’d like to use a specific algorithm, (let’s say you have an experiment where you match people and places according to their prefences, using deferred acceptance algorithm, you can find several packages for that.

Just to whet your appetite, here are some things you can do with Python: - Data cleaning, data analysis, statistical modelling - Machine Learning applications - Agent-Based Modelling - Web Scraping - Building web and desktop applications

So if you are learning Python for the first time, I’d say this is a very good investment. I hope you’ll enjoy it. In this book, however, we will focus on a small subset of Python features that are necessary for us to build experiments and that will be sufficent. So if you like to learn further and expand your Python superpowers, there are many great sources online to learn Python, depending on the path you’d like to follow.

How to interact with Python?

Option 1: Python Interpreter

Python, in its core, is a command-line interpreter. This means that you can write Python code in a text editor and run it from the terminal. For instance, you can write a Python script in a text editor, save it as my_python_file.py1 and run it from the terminal by typing python my_python_file.py2.

1 Python scripts take the extension .py. You are free to choose the name before the extension for regular Python files. (In oTree we will have a certain structure and naming convention for our Python files though. But don’t worry about that for now.)

2 If you have Python installed on your computer and you are an impatient person in general, you can go ahead and try this. Create a file called my_python_file.py with a single line: print("Hello World!"). Then run it from the terminal by typing python my_python_file.py. You should see Hello World! printed in the terminal.

Fig 14.2

Option 3: Development Environment

2- Running a Python Script from the Terminal

  • Python scripts have the extension .py

3- Using a development environment

  • Some of them are PyCharm, VSCode, RStudio and so on.

  • Usually Shift + Enter or Ctrl + Enter to send the command to shell

4- Jupyter Notebook

  • Browser environment for writing and running interactive Python code.

  • You can combine text and code cells to create a notebook.

Keyboard Shortcut Description
Shift + Enter Send the cell to the kernel for execution
Ctrl + Enter Run the cell and advance to the next cell
Enter Edit the cell
Esc Stop Editing the cell
H Help
M Cell to Markdown (text)
Y Cell to code

:::

Hello World!

  • To print things in the console, one should use print() function.
  • Let’s go ahead and print Hello World:
print("Hello World!")
Hello World!