Sei sulla pagina 1di 18

Command line prompt

 There are a variety of ways to tell Python to execute the code

 IDEs ( Spyder, JupiterLab etc.)

 Icon clicks( using .py in ipython files)

 Command line

 Command line: cmd Command Prompt (for windows), shell or


terminal window( for UNIX, Linux and Mac OS X )

 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

 We can execute a python file (.py) in command prompt by using the


term python followed by filename.py.

 a python file Myfirstcode.py has written to print Hello, world then


through the command line the script can be executed as:

> python Myfirstcode.py


Hello, world

NOTE:Check the Current directory of command prompt should be same as the


directory path of the script
Command line Arguments

 So far we have been taken input in python using raw_input() or


input().

 Another method can be used as command line arguments to give


input to the scripts.

 The command line arguments is the text you enter after the word
python when you run a scripts.

 The Command Line Arguments must be given before the execution


of the script that is we can pass the input with the filename

 on the other hand input() is used to get the input while the python
script is running.
A simple example

 Suppose we have the following scripts(cmdadd.py) written to add


two input numbers:

# cmdadd.py
import sys
arg1 = int(sys.argv[1])
arg2 = int(sys.argv[2])
print(arg1 + arg2)

 The above script can be execute in command line as:

>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 cmdarg.py 5 ic152


['cmdarg.py', '5', 'ic152']
5
ic152
Command line Arguments

 The number of arguments in the command line can be known by


len(sys.argv)

 argv[0] is the name of the program


argv[1] is the first command-line argument
argv[2] is the next argument
...
...

 The arguments are always saved as strings even if we type an


integer in the argument list. We need to use int() function to
convert the string to integer.
Advantages
 Suppose we have a script fact.py to find the factorial of the input
integer as:
#fact.py
import sys
from math import factorial
print(factorial(int(sys.argv[1])))

>python fact.py 5
120
>python fact.py 4
24
>python fact.py 6
720
Advantages

 We can use the command line arguments to write the programs


which we are going to use frequently.

 For example, Suppose we need to find factorial many times.

 We can get the output by simply writing the command for


getting the factorial of a number

 Similarly we can pass input data such as file names.

 We can write shell scripts that process large number of inputs.


Example
 Suppose we have a script quad.py to find the Root of the quadratic
equation ax2 + bx + c from the input a, b, c:

#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

I A flowchart is a diagram that depicts the “flow” of a


program.
I Graphically depicts the logical steps to carry out a task and
shows how the steps relate to each other.
I Flowchart is the pictorial representation of algorithms.
I One can see a flow chart as a blueprint of a design you have
made for solving a problem.
A simple example
To find the sum of two numbers:

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.

Potrebbero piacerti anche