The sys module is one of the common and frequently used modules in Python. In this article, we will walk you through how to use the sys module. We will learn about what argv[0] and sys.argv[1] are and how they work. We will then go into how to parse Command Line options and arguments, the various ways to use argv and how to pass command line arguments in Python 3.x In simple terms,Command Line arguments are a way of managing the script or program externally by providing the script name and the input parameters from command line options while executing the script. Command line arguments are not specific just to Python. These can be found in other programming languages like C, C# , C++, PHP, Java, Perl, Ruby and Shell scripting. Understanding sys.argv with examples  sys.argv is a list in Python that contains all the command-line arguments passed to the script. It is essential in Python while working with Command Line arguments. Let us take a closer look with a few examples. With the len(sys.argv) function, you can count the number of arguments. import sys print ("Number of arguments:", len(sys.argv), "arguments") print ("Argument List:", str(sys.argv)) $python test.py arg1 arg2 arg3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']Module name to be used while using sys.argv To use sys.argv, you will first need to the sys module. What is argv[0]? Remember that sys.argv[0] is the name of the script. Here – Script name is sysargv.py import sys print ("This is the name of the script: ", sys.argv[0]) print ("Number of arguments: ", len(sys.argv)) print ("The arguments are: " , str(sys.argv))Output:This is the name of the script: sysargv.py Number of arguments: 1 The arguments are: ['sysargv.py']What is "sys. argv [1]"? How does it work? When a python script is executed with arguments, it is captured by Python and stored in a list called sys.argv. So, if the below script is executed: python sample.py Hello Python Then inside sample.py, arguments are stored as: sys.argv[0] == ‘sample.py’ sys.argv[1] == ‘Hello’ sys.argv[2] == ‘Python’Here,sys.argv[0] is always the filename/script executed and sys.argv[1] is the first command line argument passed to the script . Parsing Command Line options and arguments Python provides a module named as getopt which helps to parse command line options and arguments. Itprovides a function – getopt, whichis used for parsing the argument sequence:sys.argv. Below is the syntax: getopt.getopt(argv, shortopts, longopts=[]) argv: argument list to be passed.shortopts: String of short options as list . Options in the arguments should be followed by a colon (:).longopts: String of long options as list. Options in the arguments should be followed by an equal sign (=). import getopt import sys first ="" last ="" argv = sys.argv[1:] try: options, args = getopt.getopt(argv, "f:l:", ["first =", "last ="]) except: print("Error Message ") for name, value in options: if name in ['-f', '--first']: first = value elif name in ['-l', '--last']: last = value print(first + " " + last)Output:(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python getopt_ex.py -f Knowledge -l Hut Knowledge Hut (venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python getopt_ex.py --first Knowledge –last Hut Knowledge HutWhat are command line arguments? Why do we use them? Command line arguments are parameters passed to a program/script at runtime. They provide additional information to the program so that it can execute. It allows us to provide different inputs at the runtime without changing the code. Here is a script named as argparse_ex.py: import argparse parser = argparse.ArgumentParser() parser.add_argument("-n", "--name", required=True) args = parser.parse_args() print(f'Hi {args.name} , Welcome ')Here we need to import argparse package Then we need to instantiate the ArgumentParser object as parser. Then in the next line , we add the only argument, --name . We must specify either shorthand (-n) or longhand versions (--name) where either flag could be used in the command line as shown above . This is a required argument as mentioned by required=True Output: (venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py --name Nandan Hi Nandan , Welcome (venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py -n Nandan Hi Nandan , Welcome The example above must have the --name or –n option, or else it will fail.(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py --name usage: argparse_ex.py [-h] --name NAME argparse_ex.py: error: the following arguments are required: --namePassing command line arguments in Python 3.x argv represents an array having the command line arguments of thescript . Remember that here, counting starts fromzero [0], not one (1). To use it, we first need to import sys module (import sys). The first argument, sys.argv[0], is always the name of the script and sys.argv[1] is the first argument passed to the script. Here, we need to slice the list to access all the actual command line arguments. import sys if __name__ == '__main__': for idx, arg in enumerate(sys.argv): print("Argument #{} is {}".format(idx, arg)) print ("No. of arguments passed is ", len(sys.argv))Output:(venv) C:\Users\Nandank\PycharmProjects\DSA\venv\Scripts>python argv_count.py Knowledge Hut 21 Argument #0 is argv_count.py Argument #1 is Knowledge Argument #2 is Hut Argument #3 is 21 No. of arguments passed is 4Below script - password_gen.py is used to generate a secret password by taking password length as command line argument.import secrets , sys, os , string ''' This script generates a secret password using possible key combinations''' ''' Length of the password is passed as Command line argument as sys.argv[1]''' char = string.ascii_letters+string.punctuation+string.digits length_pwd = int(sys.argv[1]) result = "" for i in range(length_pwd): next= secrets.SystemRandom().randrange(len(char)) result = result + char[next] print("Secret Password ==" ,result,"\n")Output:(venv) C:\Users\Nandank\PycharmProjects\DSA\venv\Scripts>python password_gen.py 12 Secret Password == E!MV|,M][i*[Key takeaways Let us summarize what we've learnt so far. We have seen how to use the sys module in Python, we have looked at what areargv[0] and sys.argv[1] are and how they work, what Command Line arguments are and why we use them and how to parse Command Line options and arguments. We also dived into multiple ways to use argv and how to pass command line arguments in Python 3.xHope this mini tutorial has been helpful in explaining the usage of sys.argv and how it works in Python. Be sure to check out the rest of the tutorials on KnowledgeHut’s website and don't forget to practice with your code! Rated 4.0/5 based on 14 customer reviews # How to use sys.argv in Python 6K • by Nandan Kumar • 03rd Jul, 2020 • Last updated on 25th Aug, 2020 • 8 mins read The sys module is one of the common and frequently used modules in Python. In this article, we will walk you through how to use the sys module. We will learn about what argv[0] and sys.argv[1] are and how they work. We will then go into how to parse Command Line options and arguments, the various ways to use argv and how to pass command line arguments in Python 3.x In simple terms,Command Line arguments are a way of managing the script or program externally by providing the script name and the input parameters from command line options while executing the script. Command line arguments are not specific just to Python. These can be found in other programming languages like C, C# , C++, PHP, Java, Perl, Ruby and Shell scripting. ## Understanding sys.argv with examples sys.argv is a list in Python that contains all the command-line arguments passed to the script. It is essential in Python while working with Command Line arguments. Let us take a closer look with a few examples. With the len(sys.argv) function, you can count the number of arguments. import sys print ("Number of arguments:", len(sys.argv), "arguments") print ("Argument List:", str(sys.argv))$ python test.py arg1 arg2 arg3
Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

### Module name to be used while using sys.argv

To use sys.argv, you will first need to the sys module.

### What is argv[0]?

Remember that sys.argv[0] is the name of the script.

Here – Script name is sysargv.py

import sys
print ("This is the name of the script: ", sys.argv[0])
print ("Number of arguments: ", len(sys.argv))
print ("The arguments are: " , str(sys.argv))

Output:

This is the name of the script:  sysargv.py
Number of arguments:  1
The arguments are:  ['sysargv.py']

## What is "sys. argv [1]"? How does it work?

When a python script is executed with arguments, it is captured by Python and stored in a list called sys.argv.

So, if the below script is executed:

python sample.py Hello Python

Then inside sample.py, arguments are stored as:

• sys.argv[0] == ‘sample.py’
• sys.argv[1] == ‘Hello’
• sys.argv[2] == ‘Python’

Here,sys.argv[0] is always the filename/script executed and sys.argv[1] is the first command line argument passed to the script .

## Parsing Command Line options and arguments

Python provides a module named as getopt which helps to parse command line options and arguments. Itprovides a function – getopt, whichis used for parsing the argument sequence:sys.argv.

Below is the syntax:

getopt.getopt(argv, shortopts, longopts=[])
• argv: argument list to be passed.
• shortopts: String of short options as list . Options in the arguments should be followed by a colon (:)
• .longopts: String of long options as list. Options in the arguments should be followed by an equal sign (=).
import getopt
import sys

first =""
last =""
argv = sys.argv[1:]
try:
options, args = getopt.getopt(argv, "f:l:",
["first =",
"last ="])
except:
print("Error Message ")

for name, value in options:
if name in ['-f', '--first']:
first = value
elif name in ['-l', '--last']:
last = value

print(first + " " + last)

Output:

(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python getopt_ex.py -f Knowledge -l Hut
Knowledge Hut
(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python getopt_ex.py --first Knowledge –last Hut
Knowledge Hut

## What are command line arguments? Why do we use them?

Command line arguments are parameters passed to a program/script at runtime. They provide additional information to the program so that it can execute.

It allows us to provide different inputs at the runtime without changing the code.

Here is a script named as argparse_ex.py:

import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
print(f'Hi {args.name} , Welcome ')

Here we need to import argparse package

Then we need to instantiate the ArgumentParser object as parser. Then in the next line , we add the only argument, --name . We must specify either shorthand (-n) or longhand versions (--name)  where either flag could be used in the command line as shown above . This is a required argument as mentioned by required=True

Output:

(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py --name Nandan
Hi Nandan , Welcome
(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py -n Nandan
Hi Nandan , Welcome 

The example above must have the --name or –n option, or else it will fail.

(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py --name
usage: argparse_ex.py [-h] --name NAME
argparse_ex.py: error: the following arguments are required: --name

## Passing command line arguments in Python 3.x

• argv represents an array having the command line arguments of thescript .
• Remember that here, counting starts fromzero [0], not one (1).
• To use it, we first need to import sys module (import sys).
• The first argument, sys.argv[0], is always the name of the script and sys.argv[1] is the first argument passed to the script.

Here, we need to slice the list to access all the actual command line arguments.

import sys
if __name__ == '__main__':
for idx, arg in enumerate(sys.argv):
print("Argument #{} is {}".format(idx, arg))
print ("No. of arguments passed is ", len(sys.argv))

Output:

(venv) C:\Users\Nandank\PycharmProjects\DSA\venv\Scripts>python argv_count.py Knowledge Hut 21
Argument #0 is argv_count.py
Argument #1 is Knowledge
Argument #2 is Hut
Argument #3 is 21
No. of arguments passed is  4

Below script - password_gen.py is used to generate a secret password by taking password length as command line argument.

import secrets , sys, os , string
''' This script generates a secret password using possible key combinations'''
''' Length of the password is passed as Command line argument as sys.argv[1]'''
char = string.ascii_letters+string.punctuation+string.digits
length_pwd = int(sys.argv[1])

result = ""
for i in range(length_pwd):
next= secrets.SystemRandom().randrange(len(char))
result = result + char[next]
print("Secret Password ==" ,result,"\n")

Output:

(venv) C:\Users\Nandank\PycharmProjects\DSA\venv\Scripts>python password_gen.py 12
Secret Password == E!MV|,M][i*[

### Key takeaways

Let us summarize what we've learnt so far. We have seen how to use the sys module in Python, we have looked at what areargv[0] and sys.argv[1] are and how they work, what Command Line arguments are and why we use them and how to parse Command Line options and arguments. We also dived into multiple ways to use argv and how to pass command line arguments in Python 3.x

Hope this mini tutorial has been helpful in explaining the usage of sys.argv and how it works in Python. Be sure to check out the rest of the tutorials on KnowledgeHut’s website and don't forget to practice with your code!

### Nandan Kumar

Author

QA Automation Architect having 12+ Years of experience in UI , Backend and Database testing ( Selenium, Cucumber, Gherkins, Protractor, TestNG) having strong Python/Java Coding skills. Strong experience in Devops/QAops , MongoDB and Certified AWS Solution Architect.

Strong Problem solver having worked on multiple domains: Media and TV/Video Streaming, Investment Banking, E-Commerce/Payments and Cloud Security /Networking/SaaS/IaaS/PaaS.

## Join the Discussion

SPECIAL OFFER Upto 20% off on all courses
Enrol Now

## Scala Vs Python Vs R Vs Java - Which language is better for Spark & Why?

Rated 4.5/5 based on 1 customer reviews
8242
Scala Vs Python Vs R Vs Java - Which language is b...

One of the most important decisions for the Big da... Read More

## What is Context in React? How to use Context in React?

Rated 4.0/5 based on 1 customer reviews
7930
What is Context in React? How to use Context in Re...

What the hack is Context?Have you ever wondered ab... Read More

## Introduction to Yield in Python

When we write a function, which should perform some operation and provide some result back, we generally use the return statement for returning the result.The yield statement is only used when defining a generator function and is only used in the body of the generator function. Yield are used in Python generators. A python generator is defined like a normal function, but whenever it will needto generate a value, it does so with the yield keyword rather than return. If the body of a def function contains yield, the function will becomea generator function. A Python generator is a function return a generator iterator (just an object we can iterate over) by calling yield .Yield maybe called with a value, in which case that value is treated as the "generated" value. Yield is a keyword in Python that will used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. Return sends a value back to its caller, whereas yield can produce a sequence of values. We must use yield when we want to iterate over a sequence but don't want to store the entire sequence in memory so in that case you can use yield Python generators. The function will continue the execution immediately after the last yield run. It will allow its code to produce a series of values and sending them back like a list.Example:In the above example, yield is the keyword and 1, 2, 3 are the values that will store in the generator and to print the value inside the generator we are using for loop. We can use yield when we want to iterate over a sequence, but don't want to store the entire sequence in memory.Explaining the Iterables: To understand what yield does, you must understand what generators are. And before generators come iterables.Aniterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop. Ex- iterables include lists, tuples, and strings - any such sequence can be iterated over in a for-loop. In simpler words, whatever that will appear on the right-side of a for-loop: for x in iterable:  is aniterable.Iterablecan be anything that you can loop over with a for loop.An object can becallediterable,if we can get an iterator from iterable. Maximum built-in containers in Python like: list, tuple, string etc. areiterables.Important property(Method) of an iterable is that it has an __iter__() method or iter() method which allows any iterable to return an iterator object. Sequences Sequences are a very common type of iterable. Ex- for built-in sequence types are lists, strings, and tuples.Iterable will support element access using integer indices via the __getitem()__ special method (indexing) and define a __length()__ method that returns the length of the sequence. Also, we can use the slicing technique on them.Other IterablesMany things in Python are iterables, but not all of them are sequences. Dictionaries, file objects, sets, and generators all are iterables, but none of them is a sequence.We can loop over an iterable without using a for loop. Some of us may think that we can use a while loop and generate indices to get this output.It seems that this approach works very well for lists and other sequence objects as well. What about the non-sequence objects? They don’t support indexing, so this approach will not work for them, we will get an error.But how the Python’s for loop works on these iterables then? We can see that it works with sets.When you create a list, you can read its items one by oneWhat is an Iterator? Iterator is an object which can be looped through and maintains its state during the iteration which means it remembers where it is during an iteration. An iterator is an object that enables a programmer to traverse a container, particularly lists. Different types of iterators are often provided via a container's interface. It has a __next__() method that returns the next value from the iteration and updates the state to point at the next value. Iterator is simplyan object that can be iterated upon. An object it will return data, one element at a time.Python iterator object must implement two special methods, that is __iter__() and __next__() , collectively called the iterator protocol.Thepurpose of an iterator is allow to process with every element of a container while isolating the user from the internal structure of the container. The container provides the methods for creating iterators. Iterator is an object and it will represent a stream of data. You can create an iterator object by applying the iter() function to an iterable.Difference between iterable and iterator. Iterable is an object, which can iterate over. It will create an Iterator when passed to iter() method. Iterator is an object, which is used to iterate over an iterable object using __next__() method. For example, a list is iterable but a list is not an iterator.When we will apply the iter() function to an iterable we will get an iterator. If we will call the iter() function on an iterator it will give us itself back in python.You can use an iterator to manually loop over the iterable it came from. if repeated passing of iterator to the built-in function next() it will return successive items in the stream. Once, when you consumed an item from an iterator, it has gone. When data aren’t available it will give us a StopIteration and exception is raised.Iterators also have the __iter__() method and it will return self object.List is an iterable. When you will use a list comprehension, you create a list, and so an iterableThese iterables are handy because you can read them, but you store all the values in memory and it’s not always what you want when you have a lot of values. Iterator Protocol The iterator objects are required to support the following two methods, which together form the iterator protocol: iterator.__iter__() Return the iterator object itself. This is required to allow both containers and iterators to be used with the for statements. iterator.__next__() Return the next item from the container. If there are no items, then it will raise the StopIteration exception. We have seen some examples with iterators. Python has many built-in-classes that are iterators. Ex- an enumerate and reversed objects are iterators. Enumerate ExampleReversed ExampleThe Python’s zip, map and filer objects are also iterators. Explaining the Generators: A Generator is a function use to store the value by using yield keyword. A function which returns a generator iterator. This is a normal function only except that it will contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function in python.If function will contains the yield keyword then that function will become a generator and you can print the value by using for loop.You can convert a function into generator simply you need to use yield keyword in below example you can see that we can use multiple yield statement to return the dataIn above example, we defined new_generator() function in this we have assign a variable n and giving the value as 1 then print the statement and then yield the valueIf we execute generator function as a normal function then it will give us the object so for that we have to store the function in the variable x then we have use next method for executionYielding into Python List:We can use directly generator for making a list and we can store the value inside the mylist then we can print the mylist by using for loopPython Generators with a Loop:In the below example, we defined a function i.e. reverse_string and it is a generator because we are using yield keyword and we are iterating each word from the statementGenerator Expressions in Python: We use a generator function or generator expression when we want to create a custom iterator. They are simpler to use and need less code to achieve the output. Generator expressions are similar to the list comprehensions,Just like a list comprehension, the general expressions are concise. In most cases, they use to written in one line of code.An expression that returns an iterator. It looks like a normal expression followed by a for expression defining a loop variable, range, and an optional if expression ex,We can also add a conditional expression on the iterable. You can see in the above example, we did square root of the even number, we directly print the value without store the single value and we print the class i.e. generator  Explaining the Yield: Yield is a simple statement. Its primary job is to control the flow of a generator function similar tothat return statements. Yield that is used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. Any function that will present a yield keyword is termed as generator When you call a generator function or use a generator expression, you return a iterator called a generator. You can try to assign this generator to a variable in order to use it. When you will call to special methods on the generator, such as next(), the code within the function is executed till yield. When the yield statement is hit, the program suspends function execution and returns the yielded value to the caller. (Return stops function execution completely.) When a function is suspended, the state of that function is saved. This includes any variable bindings local to the generator in python, the instruction pointer, the internal stack, and any exception handling. This allows you to resume function execution whenever you will call one of the generator’s methods. Like this, all function evaluation picks back up right after yield. You can see this in action by using multiple yield statements:In the above example, we defined a function i.e. counter consist of while loop which has one condition (x < 5) and is yielding the value of x and then it will increment by 1. If we use yield keyword in the function then that function will become as Generator Yield is the special keyword in python, yield will know what it execute in the last time.In the above function, when we executed first time we are getting 0 as output but in the second execution it will increment by 1 and we got 1 as output and again it will iterate the loop till the condition will not satisfy. When we use yield keyword in the function it will store the state of the local variableExample:In the above example, you can see that with the help of yield keyword It is easy to find the word inside the string statement and it will count the number of times that word will occur because yield keyword will know the last state Benefits of yield: Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. We should use yield when we want to iterate over a sequence but don't want to store the entire sequence in memory yield is used in Python generators. A generator function or expression will defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. Conclusion: In this tutorial, you have learned about Iterable, Generator functions, Yield Iterable: An iterable is something you can loop over. Sequences are a very common type of iterable. So many things in Python are iterables, but not all of them are in sequences. An iterator is an object and it will represent a stream of data. It does the iterating over an iterable. You can use an iterator in python to get the next value or to loop over it. Once, your loop over an iterator, there are no more stream values. Additionally, in Python, the iterators are also iterables which act as their own iterators. However, the difference is that iterators don’t have some of the features that some iterables have. They don’t have length and can’t be indexed. Many built-in classes in Python are iterators. Generator: We have learnt, how to use and write generator functions and generator expressions A generator function is a function which returns an iterator. A generator expression is an expression that returns an iterator. Yield: The yield keyword in python is used to return a value from a function(just like return ) but this keyword also maintains the state of the local variables of the function and when the function is called again in python, the execution is started from the yield statement executed last time.We should use python yield when we want to iterate over a sequence but don't want to store the entire sequence in memory
Rated 4.0/5 based on 13 customer reviews
7928
Introduction to Yield in Python

When we write a function, which should perform som... Read More