Home > Python > Introduction to Python

Ch. 1.4 Conditionals (if, elif, else)

Chapters Index
Program 1

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else)

Python 3.11 Ready

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

Conditionals (if, elif, else): Logical Branching & Piecewise Physical Laws

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

1. Core Principles & Theoretical Framework

Piecewise Physics: Models potential energy wells, elastic wall collisions, and threshold phase transitions.
Relational Checks: Evaluates state conditions using relational ($<, >, ==, \neq$) and boolean operators (and, or, not).
Boundary Handling: Detects spatial domain boundaries to reflect velocities or apply absorbing conditions.

2. Mathematical Formulation & Algorithmic Pipeline

Piecewise 1D finite potential well formulation:

$$ V(x) = \begin{cases} 0, & |x| < a \ V_0, & |x| \ge a \end{cases} \quad \implies \quad \texttt{V = 0.0 if abs(x) < a else V0} $$

3. Numerical Mechanics & Implementation Logic

  • Threshold Branching: Route computation to appropriate analytical or numerical regime based on physical parameters.
  • Elastic Reflection: Invert velocity ($v \leftarrow -v$) when particle coordinate crosses boundary $x \le 0$ or $x \ge L$.
  • Short-Circuit Evaluation: Structure compound conditions so computationally lightweight checks run before expensive functions.

4. Relevant Computational Practice Exercises

Exercise 1
Compute the electric potential $V(r)$ of a uniformly charged sphere of radius $R$ inside ($r < R$) and outside ($r \ge R$).
Exercise 2
Classify roots of a quadratic equation $ax^2 + bx + c = 0$ into real distinct, degenerate, or complex conjugate roots.
Exercise 3
Simulate an elastic bouncing ball under gravity, reversing velocity with a restitution coefficient upon impact with $y = 0$.
Standard Literature & References:
  • Mark Newman, "Computational Physics", University of Michigan (2013).