ASSERT STATEMENT IN PYTHON
ASSERT STATEMENT
The assert statement is used for error checking. Suppose, we want to compute the percentage of marks obtained in a subject. It may happen that value of variables maxMarks and marks entered by the user may not be in proper range, for example, marks may be negative or greater than the maxMarks. Hence, we need to make sure that inputs provided by the user are in the correct range. For this purpose, we make use of an assert statement that has the following form:
COMMAND LINE ARGUMENTS
Let us now revisit the program to find area of the rectangle. So far, we have executed the Python programs in Python IDLE. However, we may choose to run the Python script from the command line interface. For example, the script area may be run from the command prompt by executing the following step:
1 open the directory(say, F: \PythonCode\Ch02)containing the file area.py
2 open command prompt window using an option in context memu (opend using shift+right click)
3 execute the command: python area.py
F: \PythonCode\Ch02>python area.py
Enter the Following values for rectangle:
Lenght : integer value: 20
Breadth : integer valus: 10
Area of rectangle is 200
End of program
So far, we have been taking inputs from the user in an interactive manner. However, we may also take the inputs as commands line arguments while executing a script from the command prompt as follows:
F:\PythonCode\Ch02>python area1.py 20 10
Area of rectangle is 200
End of program
In above command, 20 and 10 serve as inputs for the script area1. Whenever, we execute a script from command line, it takes name of the script as the first argument followed by other input argument (if any) in string form and stores them in the list (discussed later) sys.argv. We access the arguments stored in a argv using indexes argv[0], argv[1], argv[2] , etc. Examine the script areal. Since, the number of arguments including the script name should be three, we ensure this using condition len(sys.argv) == 3. We also assume that in the command
python areal.py 20 10
command line arguments length and breadth that follow the script name (areal.py) are stored in argv[1] and argv[2] respectivly.
_____________________
this section may be skipped on first reading without loss of continuity.
0 Response to "ASSERT STATEMENT IN PYTHON"
Post a Comment