Running Python Scripts
Running Python Scripts
A Python script is a file containing Python code. Learning how to run scripts correctly is a core skill for every developer.
What Is a Script?
A script is usually a .py file containing instructions for Python to execute.
Example file:
main.py
Your First Script
Create a file named main.py and add:
print("Hello from a Python script!")
Run from Terminal
Navigate to the folder containing the file, then run:
pythonmain.py
Or:
python3main.py
How Execution Works
Python reads the file from top to bottom.
Example:
print("Start")
print("Middle")
print("End")
Output:
Start
Middle
End
Running from an Editor
Most editors provide a Run button or integrated terminal.
Saving Before Running
Always save your file before running updated code.
Common Errors
File Not Found
You are in the wrong folder.
Syntax Error
There is invalid Python code.
Nothing Changed
You forgot to save the file.
Code Along
Create this script:
print("Welcome")
print("Today I am learning scripts")
print("This is useful")
Run it successfully.
Mini Challenge
Create a script that prints:
Morning
Afternoon
Evening
Night
Real World Use Case
Automation tools, data jobs, backend tasks, and utilities often begin as Python scripts executed from the terminal or server.
Quiz
- What extension does a Python script use?
- What command runs
app.py? - In what order does Python execute lines?
- Why should you save before running?
Assignment
Create about_me.py that prints your name, goal, and favorite hobby.
Summary
You learned what Python scripts are, how to run them, how execution flows, and how to solve common script-running problems.