Introduction Python
Introduction Python
Statistics of a data:
To find mean, standard deviation and frequency deviation of an actual
data set from any physics experiment
Graph plotting:
Plotting of graphs of functions such as exponential and trigonometric
functions.
Data interpolation:
Lagrange’s Interpolation formula
Differential equation
Euler’s method Numerical approach to solve
Runge-Kutta method (fourth order) order differential equation
Predictor corrector method
Solution of second order solution family of differential equation.
Plotting of third order solution family of differential equation
Low languages can convert to machine code without a compiler or interpreter. second-
generation programming languages use a simpler processor called an assembler. For
example: assembly and machine code
On the other hand high-level language such as and other high-level languages you might
have heard of are C, C++, Perl, and Java, have to be processed before they can run.
Programs written in a high-level language take less time to write, they are shorter
and easier to read, and they are more likely to be correct. Second, high-level
languages are portable, meaning that they can run on different kinds of computers
with few or no modifications. Low-level programs can run on only one kind of
computer and have to be rewritten to run on another. Due to these advantages,
almost all programs are written in high-level languages. Low-level languages are
used only for a few specialized applications.
6/24/2021 Department of Physics, PU: SP Gupta 4
Two kinds of programs process high-level languages into low-level languages:
interpreters and compilers. An interpreter reads a high-level program and executes it,
meaning that it does what the program says. It processes the program a little at a time,
alternately reading lines and performing computations.
A compiler reads the program and translates it completely before the program starts
running. In this case, the high-level program is called the source code, and the translated
program is called the object code or the executable. Once a program is compiled, you can
execute it repeatedly without further translation
Debugging :
Programming is a complex process, and because it is done by human beings, it often
leads to errors. For whimsical reasons, programming errors are called bugs and the
process of tracking them down and correcting them is called debugging
History of Python:
Created in 1989 by Guido van Rossum and created as a scripting language for
administrative tasks Python is based on All Basic Code (ABC) and Modula-3. Named
after comic troupe Monty Python
Released publicly in 1991, and has growing community of Python developers and
evolved into well-supported programming language
SciPy
Next up is SciPy, one of the libraries we have been talking so much about. It has a
number of user-friendly and efficient numerical routines. These include routines for
optimization and numerical integration.
SymPy
It is an open-source library for symbolic math. With very simple and comprehensible
code that is easily extensible, SymPy is a full-fledged Computer Algebra System (CAS).
It is written in Python, and hence does not need external libraries.
Open a cmd window and use the next set of commands one by one to
install NumPy, SciPy, Matplotlib, pandas, SymPy
In command-line mode, we type Python programs and the interpreter prints the result:
Alternatively, we can write a program in a file and use the interpreter to execute the
contents of the file. Such a file is called a script. The name of script file must has
extension .py such as test.py and so
Help about a particular module such as math: Type the following code in command line
mode >>>help()
help> math
Importing function from math module: Type the following code in command line mode
from math import sin, pi
print(sin(pi/2))
Logical operator
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
print ('-----\t'*5)
\n new line print('-----\t\t\t'*5)
\t tab print('-----\n'*5)
\’ for printing ’ print('----\''*5)
\” for printing ” print('----\"'*5)
print('----%'*5)
print('--- # % & " '*5)
# print hello in ten times # print hello in ten times in one line
for i in range(10): for i in range(10):
print('Hello') print('Hello’,end=‘’)
print('A')
print('B')
for i in range(5):
print('C')
print('D')
print('E')
print(‘loop is also over')
The value we put in the range function determines how many times we will loop.
The way range works is it produces a list of numbers from zero to the value minus
one. For instance, range(5) produces five values: 0, 1, 2, 3, and 4.
6/24/2021 Department of Physics, PU: SP Gupta 19
range Statement Values generated
range(10) 0,1,2,3,4,5,6,7,8,9
range(1,10) 1,2,3,4,5,6,7,8,9
range(3,7) 3,4,5,6
range(2,15,3) 2,5,8,11,14
range(9,2,-1) 9,8,7,6,5,4,3
# try this
for i in range(1,21):
print(‘*’*i)
if statement
if statement: when we only want to do something provided something else is true
Conditional operators
The comparison operators are ==, >, <, >=, <=, and !=. That last one is for
not equals. Here are a few examples:
Expression Description
if x>5: if x is greater than 5
if x>=5: if x is greater than or equal to 5
if x==5: if x is 5
if x!=5: if x is not 5
There are three additional operators used to construct
more complicated conditions: and, or, and not
var = 1
while var == 1: # This constructs an infinite loop
num=eval(input('Enter a number :'))
print('You entered:', num)
print('Good bye!')
Question 2: Take a number in input and check whether number is odd or even.