print("Hello World!")
Hello World!
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.
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.py
1 and run it from the terminal by typing python my_python_file.py
2.
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.
2- Running a Python Script from the Terminal
.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 |
:::
print()
function.Hello World
:print("Hello World!")
Hello World!