PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

CLI arguments

← All stacks

Theory

94/268

CLI arguments

Command-line args come in sys.argv. argv[0] is the script name. argv[1:] are extra words after python file.py.

Example: python add.py 2 3 → argv is [add.py, 2, 3]. int() them before adding. argparse is the nicer library for real tools.

CLI arguments — output — script name then 5. argv[0] is the file; rest are extra words.

Exam tip

What is argv[0]? Then one tiny add.py idea.

Example

# CLI arguments
import sys
print(sys.argv[0])
args = ["2", "3"]  # like python add.py 2 3
print(int(args[0]) + int(args[1]))

CLI arguments — output: script name then 5. argv[0] is the file; rest are extra words.

Short notes

  • DefCommand-line args come in sys.argv.
  • RuleExample: python add.py 2 3 → argv is [add.py, 2, 3].
  • RememberCLI arguments — output: script name then 5.

Questions

1

Explain CLI arguments as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with CLI arguments?

Previous← ArraysNextMagic methods →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.