Passing Command-Line Arguments in Python

Parameters are variables declared in the script and the arguments are the data you pass into these parameters. The whole purpose of passing arguments to a script is to change\tweak the behavior, output or functionality of script from outside the script without making any changes to the actual program. Both Python and PowerShell provide couple of ways pass, access and parse arguments in the script, following are some such methods.

‘sys.argv’ in Python

Python has a ‘sys’ module or the system module that has a `argv` attribute, which provides the list of command-line arguments passed to the Python program. It’s basically an array holding the command line arguments of the program.
Index of this array starts at zero ( 0 ), not at one ( 1 ) and by default the first argument of the `argv` attribute i.e, ‘sys.argv[0]’ is always the name of the program where it was invoked.
So for example if we call a script file named: ‘’ with arguments ’13’,  ’45’ and ’57’ then,
  • sys.argv[0] is ‘’
  • sys.argv[1] is 13
  • sys.argv[2] is 23
  • sys.argv[3] is 57

$args Automatic Variable in PowerShell

‘$args’ is an Automatic variable in PowerShell that contains an array of the undeclared parameters passed to a script, to know more check the help documentation in PowerShell using:  Get-Help about_Automatic_Variables


This is just a teaser content from my book in form of a blog post!

If you want to know more about what are the similarities between Python’s Argument Parser and PowerShell’s param() statement and other more advanced techniques to pass command line arguments?

Then read my book (below) which is still in progress, on lean publishing format.

Buy early, pay less, free updates!

My new book :  PowerShell Scripting Guide to Python

This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.

“Use what you know to learn what you don’t. ” also known as Associative learning.

Book follows a comparative method to jump start readers journey in Python, but who is the target audience? and who should read this book –

  • Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
  • Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.

Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloudto the future – Data ScienceArtificial Intelligence (AI) and Machine Learning Python is a must know.

But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell

NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!

While the book is in progress, please review it and send any feedback or error corrections at [email protected]

Optical Character Recognition

Subscribe to our mailing list

* indicates required