# this is the "my_script.py" file located on the Desktop...
# this is a comment
print("HELLO WORLD!")
= 2 + 2
x print(x)
HELLO WORLD!
4
In this exercise, we’ll practice using the local development environment to create, edit, and execute a simple Python program.
First we will explore the Python shell, which will be able to execute single expressions of Python code. We might use the Python interpreter in practice to quickly test out short snippets of code.
Open an interactive Python interpreter:
python -i
You will be able to execute Python expressions, such as the following:
= 2 + 2
x
print(x)
Press enter after each expression.
When done, type exit()
and press enter to quit the interpreter, and return to the command line.
Some limitations of the Python interpreter are that it is harder to execute multi-line expressions, and after we quit the interpreter the code we wrote is lost.
In order to write longer Python programs, and save some code for later, we will use a Python script. A Python script is a text file containing Python code, where the file extension is “.py”.
Exercise:
python ~/Desktop/my_script.py
) to see its output.Example Python code:
# this is the "my_script.py" file located on the Desktop...
# this is a comment
print("HELLO WORLD!")
= 2 + 2
x print(x)
HELLO WORLD!
4
Once you see the printed messages in your command-line application, you have succeeded. Edit the file (by changing the message or the numbers), save it again, and run it again.