CodingNic

Python Foundations

Running Python Scripts

Python Foundations 15 min read

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:

text

main.py

Your First Script

Create a file named main.py and add:

python

print("Hello from a Python script!")

Run from Terminal

Navigate to the folder containing the file, then run:

bash

pythonmain.py

Or:

bash

python3main.py

How Execution Works

Python reads the file from top to bottom.

Example:

python

print("Start")

print("Middle")

print("End")

Output:

text

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:

python

print("Welcome")

print("Today I am learning scripts")

print("This is useful")

Run it successfully.

Mini Challenge

Create a script that prints:

text

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

  1. What extension does a Python script use?
  2. What command runs app.py?
  3. In what order does Python execute lines?
  4. 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.