On this page

Running Your First Script

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

What a Python Script Is​

A Python script is a plain text file containing Python instructions. The file uses the .py extension, and Python executes the instructions from top to bottom. Scripts allow you to save code, run it repeatedly, and build programs that grow in complexity.

A script differs from the interactive Python prompt. The prompt executes one line at a time, while a script executes a complete sequence of instructions as a program.

Creating Your First Script​

Create a new file in your text editor and save it as:


# hello.py

print("Hello, world!")

This file contains a single instruction. When executed, Python reads the file, interprets the instruction, and displays the text.

Running the Script from the Terminal​

Open your terminal or command prompt. Navigate to the directory where the script is saved. Then run:


python hello.py

If the file is written correctly, the terminal will display:


Hello, world!

This confirms that Python read the file and executed the instruction.

How Python Executes a Script​

When you run a script, Python performs the following steps:

  1. Reads the file from top to bottom
  2. Parses the code to ensure it follows Python’s syntax rules
  3. Executes each statement in sequence
  4. Stops when it reaches the end of the file or encounters an error

Python does not infer missing instructions. It executes exactly what is written, in the order it is written.

Why Running Scripts Matters​

Running scripts is the foundation of Python development. Every program you write—whether a small utility or a larger application—begins as a script. Learning how to create, save, and execute scripts ensures that you can test your code, organize your work, and build increasingly complex programs throughout the course.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Foundations

Next

Variables and Data Types