Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Command line
IDEs and Icon click have been used so far directly . So this module
is meant to describe the uses of command line to execute the
Python file and the command line argument Properties.
Command line prompt
The command line arguments is the text you enter after the word
python when you run a scripts.
on the other hand input() is used to get the input while the python
script is running.
A simple example
# cmdadd.py
import sys
arg1 = int(sys.argv[1])
arg2 = int(sys.argv[2])
print(arg1 + arg2)
>python cmdadd.py 5 9
14
Command line Arguments
The command line arguments in python can be processed by using
‘sys’ module
All the Command line arguments in Python is saved as a list
sys.argv
for a .py file name cmdarg.py
# cmdarg.py
import sys
argumentList = sys.argv
print(argumentList)
print(sys.argv[0])
print(sys.argv[1])
>python fact.py 5
120
>python fact.py 4
24
>python fact.py 6
720
Advantages
#quad.py
import sys
import cmath
a = int(sys.argv[1])
b = int(sys.argv[2])
c = int(sys.argv[3])
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
>python quad.py 1 6 8
The solutions are (-4+0j) and (-2+0j)
Flow Chart
Require: A, B
1: C = A + B ;
2: C is the sum.
Common Symbols
Example
To find sum of a list of numbers
Require: List
1: Sum = 0, k = 0;
2: while (k < length(List )) do {
3: Sum = Sum + List [k ];
4: k = k + 1;
5: }
6: Sum is the sum of the list.
Example
To find Maximum of a list of numbers
Require: List
1: k = 0, Max = List [0];
2: while (k < length(List )) do {
3: if (Max < List [k ]) then {
4: Max = List [k ];
5: }
6: k = k + 1;
7: }
8: Max is the maximum element in the
list.
Example
To find Factorial of a list of numbers
Require: N
1: Product = 1, k = 1;
2: while (k ≤ N ) do {
3: Product = Product ∗ k ;
4: k = k + 1;
5: }
6: Praduct is the Factorial of
N.
Example
To find roots of a function f (x) using Bisection Method
Require: a1 , b1 , f (a1 ) ∗ f
(b1 ) < 0
log((b1 − a1 )/)
1: k = 1, MaxIter = ceil 1 + ;
log 2
2: while (k ≤ MaxIter ) do {
ak + bk
3: ck = ;
2
4: if (f (ck ) = 0) then {
5: break;
6: }
7: if (f (ak ) ∗ f (ck ) < 0) then {
8: ak +1 = ak , bk +1 = ck ;
9: f (ak +1 ) = f (ak ), f (bk +1 ) = f (ck );
10: else
11: ak +1 = ck , bk +1 = bk ;
12: f (ak +1 ) = f (ck ), f (bk +1 ) = f (bk );
13: }
14: k = k + 1;
15: }
ak + bk
16: ck = is the root.
2
Example
To find roots of a function f (x) using Bisection Method
Practice Exercise
Write the Pseudo code and Flow chart for the following problems.
I Converting Celsius to Farehnite.
I Finding the HCF and LCM of two numbers.
I Finding the roots of a Quadratic Equation.
I Finding the Lagrange interpolating polynomial for given data
points.
I Finding the roots of a function using Newton-Raphson
Method.
I Finding the roots of a function using Regula Falsi method.
I For K-means Clustering.
I For KNN classifier.