The Python Interpreter and Your First Program
The Interactive Interpreter
Python can be run interactively. Open your terminal and type python (or python3). You will see the >>> prompt. This is great for testing small snippets.
python
2 + 2 4 'Hello' + ' World' 'Hello World' exit()
Writing and Running a Script
Create a file named hello.py and put the following single line inside it:
python
hello.py
print('Hello, Python World!')
Running the Script
- Navigate to the directory where you saved
hello.pyusing your terminal. - Execute the script using the Python interpreter:
bash python hello.py
Output:
Hello, Python World!
Comments
Comments are lines that the interpreter ignores. They are used to explain the code. Use the # symbol for single-line comments.
python
This is a comment explaining the next line of code.
print('This line runs.') # This is an inline comment.