Before writing Python programs, you must have Python installed on your system. Once installed, you can write and run Python code using two common modes:
.py file and run it later as a full program.python --version..py extension, for example main.py.In interactive mode, you type Python code directly into the Python shell and see the result immediately. This is great for experimenting with small pieces of code.
In script mode, you write your code inside a .py file. This allows you to build larger programs that can be reused, modified, and shared.
python --version
Python 3.12.0
>> print("Hello from Python")
Hello from Python
# main.py
print("This is a Python script")
python main.py
When you run the version command in your terminal:
python --version
you will see output similar to:
Python 3.12.0
In interactive mode, after typing:
print("Hello from Python")
the shell immediately shows:
Hello from Python
In script mode, running python main.py executes all the code inside main.py and prints:
This is a Python script
python3 instead of python on Linux/Mac if the system already uses Python 2.x.python.print("Python is fun!")hello.py with your own greeting message and run it.