E-Book For Python223
E-Book For Python223
TO PYTHON
1
PYTHON (SUBLIME)
What is Sublime? 2
Comment 4
How to make any line a comment? 4
Concept of Append 9
What is Append? 9
Concept of POW() 11
Loops 12
How to define a Loop? 12
How does loop work? 12
Modules: 14
Math module 14
Plotting Module 17
Description about some commands under plotting 19
Numpy Module 19
Scipy Module 23
How to import inbuilt functions 24
Concept of a function: 25
Definition a function: 25
Concept of returning a value: 28
What is Sublime?
Sublime is basically a text editor where we can write and run programs and
extract the outputs.
If the extension .py is not added to the name of the code, then python will throw
an error while running that program.
First Check:
If the compiled code is not giving the desired output, then the initial check will
be to check the name by which the program is saved. We can check the name
of the program as shown below,
4
Here we can see that 1st program is saved as hi.py while the 2nd program is
saved as hello. Since the extension is absent in the second program’s name, it
will not print out any output while running the program.
Also, it is recommended that you save the code before writing it. Sublime has
an in-built feature that will give different color to different inbuilt commands
when we use them. If we don’t save the code beforehand or if we save it
incorrectly, sublime will not color the inbuilt commands when we write the
codes. The difference between coding in a pre-saved and incorrectly saved
document is shown below
Comment
Comment is a line in the code which is never executed.
As “0“is the MINIMUM INDEX, the program will print the first value from the
velocity array when we print as Print(v[0]).
In the cited example, the first value in the velocity array is 5 and so to get the
first value we need to use the index 0.
To print the second value from an array, use the index 1 and so on.
● If we use index -1, then the last digit of an array will be printed:
7
● If we use index as -2 then the second last digit of an array will be printed:
If we use a Range Operator [i.e. colon operator (:)], all values of that array
will be printed out like shown below:
8
If we want only first 4 numbers from an array, then we can use the command
shown below:
From this, we can infer that if we need an output of the first four elements, we
generate the command
Print(v[0:4])
This will cause the program to print from the 1st element to the 4th element as
we know the 1st element is indexed as 0.
Concept of Append
To understand the concept of append, please have a look at the image posted
below,
What is Append?
Append is a command that will continuously append the value “tmp_x” and
“tmp_y” in empty arrays of “x” and “y” which we created previously.
To demonstrate what append does, we will launch python and we will be
creating an empty array first as shown below. On executing it, we will see there
will be nothing in the array.
10
When the “append” command is used, the output changes as shown below,
Since “a” is an array (empty array), append will continue storing values in it till it
ends.
11
Concept of POW()
If there is a need to give power to certain values or expressions, we make use
of an inbuilt command called POW() as shown below,
Loops
How to define a Loop?
Here range(0, num_values) means the FOR LOOP will run for num_values
times and it will start with the value of i = 0
● Make sure to insert a colon at the end of the statement where we have
defined the loop.
● Make sure there are proper given indentations (i.e. tab, spacing). Else,
python will throw an “indentation error” like the one illustrated below:
When two different kinds of indents are used in a program (like spacing instead
of Tab) or if indents are not used, Python throws an indentation error.
Eg : Nested For loop
14
Modules:
Python contains plenty of modules. To perform certain tasks, we need to import
a particular module suitable for that task.
● Math module
We need to import MATH library when our code contains any trigonometric
functions like sine, cosine or if we use mathematical constants like π.
15
To avoid such issues, we need to import MATH library in which all trigonometric
functions and mathematical constants are defined. When we execute the
program after importing the MATH module, we get the correct output as shown
below:
16
And also note how math module has been used before sine and cosine
function. Whenever required, they need to be used in a very similar manner.
For e.g.:
17
● Plotting Module
We need to import plotting module if we want our code to plot a graph. The
steps involved in importing the plotting module are explained in the below
images:
18
E.g.:
NOTE:
To get all plots visible, the following command must be used:
plt.show()
If we don’t use this command, the plots will not be visible even though the code
ran successfully.
This is how the output of a code with plotting module looks like:
19
plt.figure() --> this will keep on opening a new figure window for every loop.
() - Blank parenthesis mean “empty”. Here it is attached with plt.figure, which
means there is an empty figure window.
plt.savefig(„filename‟) -> this will continuously save the particular plot in that
figure window.
● Numpy Module
To give a power to each element of an array or to perform any mathematical
operations (like addition or subtraction) either on every element of an array or
between the arrays, we need to import the numpy library as shown below:
In the below example, “x” and “y” are the arrays and when we attempted to add
these arrays, it delivered uncanny results as highlighted below:
20
Once the numpy module was imported, the code generated desired results as it
added every element of the array “x” with the respective element of the array “y”
Likewise, to use linspace command in sublime, we need to import numpy
module as shown below,
22
● Scipy Module
Before importing this module, we have to first install Scipy Module.
To install scipy module, we need to go to the command prompt (CMD) and
make use of PIP command and then run the command as shown below,
And after typing out this command, hit enter and it will install scipy module. The
following message will be displayed on the screen:
Under scipy module, there are many inbuilt functions such as,
● ODEINT
● CURVE_FIT
And if we try to use these inbuilt functions without importing the scipy module,
they will not work and will end up with an error.
ODEINT :
24
CURVE_FIT:
25
Concept of a function:
A Function is a template. It is a set of code that we can use repeatedly.
Suppose there is some set of code which is needed to be used repetitively in
the main program then instead of writing over and over causing the the main
program to get lengthy, we can create a function for that part of code and then
call the function by its name and give the required inputs.
So function can make the main program short and easy.
Definition a function:
NOTE:
26
1. Never forget the colon (:) at the end after defining a function.
2. Also, take care of Indentations here as it signifies that this particular piece of
code is under a function.
To execute the set of operations and commands under a function, we need to
call a function as shown below,
In this scenario, the code ran successfully but it is not printing the value of “f"
even after writing the command print(f). This is because we have written all the
commands under a function and so we need to call a function by it’s
27
appropriate name. Then we must pass in the input arguments and then run the
code to get the output as shown below,
NOTE:
1. While calling a function make sure you pass in the same number of inputs
that were defined previously in the function.
2. The order by which the inputs are passed while calling a function should be
maintained to get accurate results.
28
Here we have defined a function and we are appending an empty array “A” with
the value of “f” before calling a function. It ran through successfully as shown
above.
29
We need to tell the Function explicitly to send the value for the particular
variable once the calculation is done. So that we can get to see all the stored
values while printing the values in the command window.
(in this case, we must tell the function to send the value for “A”, once the
calculation is done)
To avoid such cases, we need to make some modifications to the codes. There
are two ways to modify a code.
This command will save the file name starting with zero and will have 5 digits.
Since we are increasing the value of “ct” by 1 in every loop, each frame will get
a unique name and will be in sequence like shown below,
Now that all the frames are arranged sequentially, they will be stitched
sequentially to generate a smooth animation.
The command to stitch all frames (i.e. .png files) is as follows:
33
This command will stitch all the frames sequentially even if they are not
arranged sequentially.