Hi,
let's say we want to make a program to find the square of a number. Let's say we don't want to get the input from the user with the input() function in the usual process, but we want to input the number that we want to process by the program when we call the program in the console in this way:
C:\examples > python square.py 3
The square of 3 is equal to 9
So, it's a nice way to do things in a quick and easy way.. nothing to run... just what you saw.
The code to do that is this:
---------------------- code ----------------
import sys # sys is the module that we need for this
square = int(sys.argv[1])**2
print("The square of {} is {}".format(sys.argv[1],square))
----------------------------------------------
Now... save the file as "square.py"
go check for the folder where you put the file (in my example in "examples") and when you see the icon of the folder press shift and the right button of the mouse: from the contextual menu that will pop up, choose the voice of the menu "open the command line window from here" and the cmd will appear locaded in the folder you got the file (you can use command cd to go into that dir antway). Now something like this will come on the cmd window:
C:\examples>
examples is the name of the folder where I saved my python file, but yours could be different.
Now write this
C:\examples>python square.py 2
output:
The square of 2 is 4
Well,, we did it... that is how you can input a value into you program when you call the program without need to write some code to ask the user to input the data (the number to be sqared).
Some questions:
1) Why sys.argv[1] and not sys.argv[0]?
Because sys.argv[0] contains the name of the file itself (i.e. square.py)
2) There could be more argv?
Yes, as many as you want. You can have access to them using sys.argv[1], sys.argv[2], sys.argv[3], etc.
3) How can I know how many argv are passed... to use this in the code?
Use len(sys.argv)
For example you could:
-------------- code ------------
for i in range(len(sys.argv)):
print(i)
---------------------------------
4) What is range()?
With range(10) for example you can iterate numbers from 0 to 9.
5) What is it print("The square of {} is {}".format(sys.argv[1],square))?
the .format allows you to enter values of variables into the curly brackets inside the strings, so that you can easily mix text and variables. It's really good to make a nice code when you have to show text with variable results.
let's say we want to make a program to find the square of a number. Let's say we don't want to get the input from the user with the input() function in the usual process, but we want to input the number that we want to process by the program when we call the program in the console in this way:
C:\examples > python square.py 3
The square of 3 is equal to 9
So, it's a nice way to do things in a quick and easy way.. nothing to run... just what you saw.
The code to do that is this:
---------------------- code ----------------
import sys # sys is the module that we need for this
square = int(sys.argv[1])**2
print("The square of {} is {}".format(sys.argv[1],square))
----------------------------------------------
Now... save the file as "square.py"
go check for the folder where you put the file (in my example in "examples") and when you see the icon of the folder press shift and the right button of the mouse: from the contextual menu that will pop up, choose the voice of the menu "open the command line window from here" and the cmd will appear locaded in the folder you got the file (you can use command cd to go into that dir antway). Now something like this will come on the cmd window:
C:\examples>
examples is the name of the folder where I saved my python file, but yours could be different.
Now write this
C:\examples>python square.py 2
output:
The square of 2 is 4
Well,, we did it... that is how you can input a value into you program when you call the program without need to write some code to ask the user to input the data (the number to be sqared).
Some questions:
1) Why sys.argv[1] and not sys.argv[0]?
Because sys.argv[0] contains the name of the file itself (i.e. square.py)
2) There could be more argv?
Yes, as many as you want. You can have access to them using sys.argv[1], sys.argv[2], sys.argv[3], etc.
3) How can I know how many argv are passed... to use this in the code?
Use len(sys.argv)
For example you could:
-------------- code ------------
for i in range(len(sys.argv)):
print(i)
---------------------------------
4) What is range()?
With range(10) for example you can iterate numbers from 0 to 9.
5) What is it print("The square of {} is {}".format(sys.argv[1],square))?
the .format allows you to enter values of variables into the curly brackets inside the strings, so that you can easily mix text and variables. It's really good to make a nice code when you have to show text with variable results.
Comments
Post a Comment