Home > Python > Iterable Data Type

Ch. 2.2 Tuple

Chapters Index
Program 1

Tuple

Python 3.11 Ready

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

Tuple

Python 3.11 Ready

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

Tuple

Python 3.11 Ready

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

Tuple

Python 3.11 Ready

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

Python Tuples: Immutable Physical Constants & Phase Space Pairs

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

1. Core Principles & Theoretical Framework

Immutability & Integrity: Guarantees that fundamental constants, coordinate pairs, and state vectors are protected from accidental mutation.
Packing & Unpacking: Enables clean multi-variable return statements from ODE functions: r, v = update(r, v, dt).
Memory Efficiency: Tuples have lower memory overhead and faster allocation than dynamic lists.

2. Mathematical Formulation & Algorithmic Pipeline

Phase space coordinate state tuple:

$$ \mathbf{\Gamma} = (x, y, z, p_x, p_y, p_z) \in \mathbb{R}^6 $$

3. Numerical Mechanics & Implementation Logic

  • Tuple Packing: Group multiple observables into single return statements: return (x_new, v_new, energy).
  • Unpacking Assignment: Assign returned variables cleanly: pos, vel, e = integrate_step(pos, vel).
  • Hashable Keys: Use tuples as dictionary keys for 2D/3D discrete grid lookups where lists are prohibited.

4. Relevant Computational Practice Exercises

Exercise 1
Write a 2D projectile function that returns a tuple of state variables $(x, y, v_x, v_y, E_{\text{tot}})$.
Exercise 2
Demonstrate tuple unpacking to swap two phase space variables without an intermediate temporary variable.
Exercise 3
Create an immutable registry of physical constants $(c, G, h, e, m_e)$ using named tuples.
Standard Literature & References:
  • Hans Petter Langtangen, "A Primer on Scientific Programming with Python", Springer (2016).