Running Python

Run Python code from a file or interactively in the REPL.

Syntaxpython hello.py

There are two main ways to run Python code.

1. Run a script file

Save your code in a file ending in .py, then run it from the terminal with python filename.py.

2. The interactive REPL

Type python with no filename to open the REPL (Read-Eval-Print Loop). You type one line, press Enter, and see the result instantly. It is perfect for quick experiments. Type exit() to leave.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • A developer runs a data processing script from the terminal each morning to generate reports.
  • A learner opens the REPL to quickly test how a string method behaves.
  • A CI pipeline executes a Python test file automatically on every code push.

More examples

Run a script file

Shows the standard way to execute a Python source file from the terminal.

Example · bash
# Save this as hello.py:
# name = 'Ada'
# print('Hello,', name)

python hello.py
# Output: Hello, Ada

Use the REPL interactively

Demonstrates typing expressions into the REPL and seeing results without print().

Example · python
# Inside the Python REPL (started with: python3)
>>> x = 7
>>> x * 6
42
>>> 'hello'.upper()
'HELLO'
>>> exit()

Run one-liner from terminal

Uses the -c flag to run a short Python expression directly in the shell.

Example · bash
python3 -c "print('Hello from the command line')"
# Output:
# Hello from the command line

Discussion

  • Be the first to comment on this lesson.