Python, from Zero, for AILesson 29 of 89

Module 3 · Functions, modules, and code you can read again in March

Giving your script a proper command line

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 9 of 9 in this chapter

This chapter’s goalSplit a working script into functions and modules with explicit arguments, return values and docstrings, explain from Python’s scoping rules why a variable changed inside a function did or did not change outside it, and run the result from a terminal with named options.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:44Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

input() is the wrong tool for a script

A program that asks questions with input() cannot be scheduled, cannot be run by another program, and cannot be put in a pipeline. The moment a script is useful, somebody wants to run it on a timer or on fifty files, and interactive prompts stop that dead.

The values a run needs should arrive on the command line.

The crude version

Python
import sys

path = sys.argv[1]
print(f"processing {path}")
Bash
python3 clean.py data/sales.csv

sys.argv is a list of strings. Position 0 is the script name, so real arguments start at 1. Everything is a string, including numbers.

This works and it stops working the moment you have two options, or an optional one, or want --help. Then you are writing an argument parser by hand, badly.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Giving your script a proper command line’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/command-line-arguments (Accessed: 29 November 2026).