- Courses
- đ»Programming Languages
- đPython
- Foundations
- Running Your First Script
On this page
Running Your First Script
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:
- Reads the file from top to bottom
- Parses the code to ensure it follows Pythonâs syntax rules
- Executes each statement in sequence
- 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!
Previous
Foundations
Next
Variables and Data Types
- What a Python Script Is
- Creating Your First Script
- Running the Script from the Terminal
- How Python Executes a Script
- Why Running Scripts Matters
