Skip to main content

sys.argv or ... how to get an input straight from the console

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.

Comments

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Let's draw a circle with PIL in Python

Let's continue making our coding around PIL. Let's start with some basic drawing: a circle from PIL import Image , ImageDraw img = Image . new ( "RGB" ,( 60 , 60 ), 'white' ) dr = ImageDraw . Draw ( img ) dr . ellipse (( 0 , 0 , 60 , 60 ), 'yellow' ) img . show () this is the image produced *If you use jupyter notebook, just write img at the end to see the output.