To run a Python script from the command line or terminal, use the python
or python3
command followed by the script filename:
python my_script.py
# or, if python3 is your default:
python3 my_script.py
This executes the Python script and displays any output in the terminal.
You can start an interactive Python session by typing:
python
# or
python3
This opens the Python REPL (Read-Eval-Print Loop) where you can type and execute Python commands one by one.
If your script has a shebang line and executable permission, you can run it directly like:
chmod +x my_script.py
./my_script.py
print("Hello, World!")
a = 5
b = 7
sum = a + b
print("Sum:", sum)
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
python --version
to check).python
vs python3
) depending on your environment.python script.py
(shebang lines are not used).