How to Run Python Scripts

Running Python in the Command Line

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.

Running Python Interactively

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.

Running Python Scripts Directly (Unix-like OS)

If your script has a shebang line and executable permission, you can run it directly like:

chmod +x my_script.py
./my_script.py

3 Sample Python Scripts You Can Run

1. Hello World

print("Hello, World!")

2. Simple Addition

a = 5
b = 7
sum = a + b
print("Sum:", sum)

3. Check Even or Odd

num = int(input("Enter a number: "))
if num % 2 == 0:
    print(num, "is even")
else:
    print(num, "is odd")

Tips for Running Python Scripts