Home > Python > Introduction to Python

Ch. 1.5 For Loop

Chapters Index
Program 1

For Loop

Python 3.11 Ready

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

For Loop

Python 3.11 Ready

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

For Loop

Python 3.11 Ready

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

For Loop

Python 3.11 Ready

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

Mathematical Problem Formulation

Understanding Python's range() Function

range(6): Generates a sequence of integers from 0 to 5 (inclusive of 0 but exclusive of 6).

Iteration with for Loop:

for number in range(6): Iterates over each integer in the sequence.

Print the Current Integer:

print(number): Prints the current integer on each iteration.

Program 5

For Loop

Python 3.11 Ready

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

Mathematical Problem Formulation

range(1, 10, 2): Generates a sequence of integers starting from 1 up to (but not including) 10, with a step of 2. This means it will include every second number starting from 1 to 9.

for number in range(1, 10, 2): Iterates over each integer in the sequence produced by range(1, 10, 2).

Program 6

For Loop

Python 3.11 Ready

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

For Loop

Python 3.11 Ready

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

For Loop

Python 3.11 Ready

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

For Loop

Python 3.11 Ready

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

Mathematical Problem Formulation

Outer Loop (for i in range(1, 4)):

The outer loop goes through numbers \(1\), \(2\), and \(3\). range(1, 4) means "start at \(1\) and go up to (but not including) \(4\)." So, it will produce the numbers \(1\), \(2\), and \(3\).

Inner Loop (for j in range(10, 13)):

For each value of \(i\) from the outer loop, this inner loop goes through numbers \(10\), \(11\), and \(12\). range(10, 13) means "start at \(10\) and go up to (but not including) \(13\)." So, it produces the numbers \(10\), \(11\), and \(12\).

Output of Nested Loops

First Group: When i = 1:

It prints 1 with 10, 11, and 12.

Second Group: When i = 2:

It prints 2 with 10, 11, and 12.

Third Group: When i = 3:

It prints 3 with 10, 11, and 12.

Program 10

For Loop

Python 3.11 Ready

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

For Loop

Python 3.11 Ready

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

For Loop: Deterministic Iteration & Finite Series Summations

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

1. Core Principles & Theoretical Framework

Deterministic Loops: Iterates over a known number of discrete time steps, spatial grid nodes, or particle indices.
Series Evaluation: Computes finite expansions such as multipole potentials, Fourier series, and Taylor polynomials.
Time Stepping: Advances dynamical systems sequentially: $t_n = t_0 + n \Delta t$.

2. Mathematical Formulation & Algorithmic Pipeline

Total electric potential from a discrete array of $N$ point charges:

$$ \Phi(\mathbf{r}) = \sum_{i=1}^N \frac{q_i}{4\pi\varepsilon_0 |\mathbf{r} - \mathbf{r}_i|} $$

3. Numerical Mechanics & Implementation Logic

  • Accumulator Pattern: Initialize total sum (e.g. total_potential = 0.0) before the loop.
  • Range Iterator: Use range(start, stop, step) to index through discrete integer steps without memory overhead.
  • State Storage: Record time-series coordinates in accumulator lists at each step for post-processing.

4. Relevant Computational Practice Exercises

Exercise 1
Compute the total gravitational field of $N = 50$ equal masses placed uniformly along the circumference of a circle.
Exercise 2
Evaluate the Taylor series expansion for $\cos(x)$ up to $N = 10$ terms and compare accuracy against <code>math.cos(x)</code>.
Exercise 3
Integrate a uniform velocity trajectory $x(t)$ over 200 discrete time steps of $\Delta t = 0.01\text{ s}$.
Standard Literature & References:
  • Mark Newman, "Computational Physics", University of Michigan (2013).