About the author
In some cases, we need to pass parameters while calling a python code. It's like passing data while calling file. That data can be in any format, e.g IP address, Name, Integer or anything. e.g
Here you are using ping tool and passing IP as parameters. In same way we can pass parameters to python program also, which can be usefull in multiple ways. e.g
python3.5 example.py arg1 arg2
Understand this with below program
On line 1 we have what’s called an “import.” This is how you add features to your script from the Python feature set. Rather than give you all the features at once, Python asks you to say what you plan to use. This keeps your programs small, but it also acts as documentation for other programmers who read your code later.
The argv is the “argument variable,” a very standard name in programming that you will find used in many other languages. This variable holds the arguments you pass to your Python script when you run it. In the exercise you will get to play with this more and see what happens.
Line 3 “unpacks” argv so that, rather than holding all the arguments, it gets assigned to four variables you can work with: script, first, second, and third. This may look strange, but “unpack” is probably the best word to describe what it does. It just says, “Take whatever is in argv, unpack it, and assign it to all these variables on the left in order.”
After that, we just print them out like normal.
Now lets take another example where we will pass parameter and ask question both at same time. Run this code and see the result.
Write a code to take integer arguements and print after doing some mathematics on them.
Try passing more parameter then defined and see error.
About the author
Please login to comment