Home > Python > Introduction to Python

Ch. 1.3 Mathematical Operations

Chapters Index
Program 1

Mathematical Operations

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 2

Mathematical Operations

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 3

Mathematical Operations

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 4

Mathematical Operations

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 5

Mathematical Operations

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 6

Mathematical Operations

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Theoretical Principles & Computational Physics Guide

Mathematical Operations: Arithmetic Expressions & Operator Precedence

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Algebraic Expressions: Translates algebraic force laws, kinetic energies, and kinematic equations directly into executable code.
Operator Precedence: Follows standard algebraic hierarchy: Exponentiation (**) > Multiplication/Division (*, /, //, %) > Addition/Subtraction.
Modular Arithmetic: Modulo operator % implements periodic boundary conditions in spatial lattices and particle simulations.

2. Mathematical Formulation & Algorithmic Pipeline

Newtonian gravitational and electrostatic inverse-square forces:

$$ F = G \frac{m_1 m_2}{r^2} \quad \implies \quad \texttt{F = G * m1 * m2 / r**2} $$

3. Numerical Mechanics & Implementation Logic

  • Explicit Parentheses: Always parenthesize fractional denominators (e.g. a / (b * c) rather than a / b * c).
  • True vs Floor Division: Use / for exact physical quantities and // for discrete indices and lattice sites.
  • Exponentiation: Prefer x**2 over math.pow(x, 2) in core loops for speed and exact integer types.

4. Relevant Computational Practice Exercises

Exercise 1
Calculate the gravitational attraction between Earth and the Moon using astronomical parameters in SI units.
Exercise 2
Evaluate the relativistic Lorentz factor $\gamma = 1 / \sqrt{1 - v^2/c^2}$ for speeds $v = 0.5c, 0.9c, 0.99c$.
Exercise 3
Simulate 1D periodic boundary wrapping for a particle moving on a ring of circumference $L = 10.0\text{ m}$.
Standard Literature & References:
  • Hans Petter Langtangen, "A Primer on Scientific Programming with Python", Springer (2016).