Download presentation
Presentation is loading. Please wait.
1
Python Programming for Physicists
1 Python Programming for Physicists
2
2 Main Topics Introduction. Algorithms. Python programming.
Python libraries. Graphs and data analysis. 3D simulation. The methods to solve important problems. MikroC programming. Proteus
3
3 Introduction The Python programming language is an excellent choice for learning, teaching, or doing computational physics. It is a well-designed, modern programming language that is simultaneously easy to learn and very powerful. It includes a range of features tailored for scientific computing, including features for handling vectors, inverting and diagonalizing matrices, performing Fourier transforms, making graphs, and creating 3D graphics.
4
Importance of Computational Physics
Run
5
3 Algorithm An algorithm is an effective method that can be expressed within a finite amount of space and time and in a well-defined formal language for calculating a function. Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing "output“ and terminating at a final ending state.
6
Inputs ( Height – angle – mass)
3 Algorithm Inputs ( Height – angle – mass) Is it ok No Stop * yes Go on Hit No θ yes To the next step
7
Algorithm Finding Roots Searching Method 𝑦= 𝑥−2 2 −2 Is it ok yes
𝑦= 𝑥−2 2 −2 Is it ok Inputs (a-b) yes Calculation No Error No Stop yes Print
8
Algorithm Integration
9
INSTALLING PYTHON There are currently two different versions of Python in circulation, version 2 and version 3. There are a few small differences between versions 2 and 3 that affect some of the programs. We need these libraries Numpy/Numpy Mkl Matplotlib Visual (Vpython) Scipy
10
Python Window Python shell Python files
11
Print The simplest Code in Python and it shows that if the python working or not Print(‘Hello’) To run the program F5 in python files window ( Run – Run Modulus) Or by press enter in shell window
12
Variables and Data Variables of different types store different kinds of quantities. The main types we will use for our physics calculations are the following: Integer: Integer variables can take integer values and integer values only, such as 1, 0, or − Both positive and negative values are allowed, but not fractional values like 1.5. Float: A floating-point variable, or “float” for short, can take real, or floating-point, values such as , −6.63 × 10−34, or 1.0. Complex: A complex variable can take a complex value, such as 1+2j or −3.5 − 0.4j. Notice that in Python the unit imaginary number is called j, not i. String: it’s a variable hold texts and labels.
13
Store Variables and Data
To store value for a variable is by using the simple code x = 5 / var = 1+2j / first_var = 0.01. It must contain letters and if you want numbers (it is not necessary). It must begin with letter. Just underscore is allowed.
14
Output and input output Input print(x,z)
print("The value of x is",x,"and the value of y is",y) print(x,z,sep="...") Input X=input(“Enter the value of X : “) y=raw_input(“Enter the value of X : “)
15
ARITHMETIC x+y addition ,x-y subtraction x*y multiplication ,x/y
x**y raising x to the power of y x//y the integer part of x divided by y, meaning x is divided by y and the result is rounded down to the nearest integer. x%y modulo, which means the remainder after x is divided by y. x|y OR of two integers. x&y AND of two integers.
16
Operators and functions
The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. Operators Action Abs(x) Take absolute value Float(x) Convert to float Int(x) Convert to integer Complex(x) Convert to complex Str(x) Convert to string Add(x,y) Add x to y Max(x,y,z,…) The maximum value Min(x,y,z,…) The minimum vlaue
17
What about this statement:
The mean that the new value of x is the old value plus 1 or anything that we want such as: x=2*x+2 x=x**2+6 The new operation come from this think X+=1 X-=5 X*=2.5 x/=2 X=1 X=X+1
18
Functions, libraries and modules
There are many operations one might want to perform in a program that are more complicated than simple arithmetic, such as multiplying matrices, calculating a logarithm, or making a graph. Python comes with facilities for doing each of these and many other common tasks easily and quickly. These facilities are divided into packages—collections of related useful things—and each package has a name by which you can refer to it.
19
How to use libraries - Math
To use any library we need to import it. (From math import *) this is to import everything in this library. (From math import sin,cos,log,log10). Math library is already installed in python if we need other libraries we have to install it. Import math as m
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.