BASIC vs. Python: Mean and Standard Deviation by an Iterative Method
Calculators Used:
BASIC: Sharp EL-5500 III
Python: Casio fx-CG 100
Introduction
The following code calculates the sum, mean, and standard deviation of a data set. The method that will be used is from voidware.com, which suggests an iterative method, as follows (Voidware):
M_0 = 0, S_0 = 0, n = 0
Loop with data point X:
M_n+1 = M_n + (X – M_n) ÷ (n + 1)
S_n+1 = S_n + (X – M_n) × (X – M_n+1)
n = n + 1
When all the data as been tabulated:
S = √(S ÷ (n – 1))
M: mean
S: standard deviation
Source
“Standard Deviation” Voidware. http://www.voidware.com/sd.htm Website was last updated in 2018. Last Retrieved July 4, 2025.
BASIC: Sharp EL-5500 III
10 PRINT "ONE VAR STATS"
12 CLEAR
30 INPUT "DATA? "; X
32 E=M+(X-M)*(N+1)
34 S=S+(X-M)*(X-E)
36 N=N+1
38 M=E
40 U=U+X
42 INPUT "MORE? (1=YES) "; C
44 IF C=1 THEN 30
60 S=√(S/(N-1))
70 PRINT "# DATA= "; N
72 PRINT "SUM= "; U
74 PRINT "MEAN= "; M
76 PRINT "STD DEV= "; S
78 END
Notes:
CLEAR: clears all the variables A-Z and resets their values to zero
PRINT: pauses the screen while the message is displayed
Python: Casio fx-CG 100
(Can be we used with any calculator with Python)
# one var stat without the math module
# voidware.com/sd.htm
def onevar(data):
# set up
m,s,u,n=0,0,0,0
for i in data:
e=m+(i-m)/(n+1)
s+=(i-m)*(i-e)
n+=1
m=e
u+=i
s=(s/(n-1))**0.5
return [n,u,m,s]
Notes:
We can store multiple values to variables at once. Example:
var4, var3, var2, var1 = value1, value2, value3, value4
Storage Arithmetic:
s+=expression -> s=s+expression
s-=expression -> s=s-expression
s*=expression -> s=s*expression
s/=expression -> s=s/expression
Example
Data set:
n = 1: x = 105
n = 2: x = 107
n = 3: x = 86
n = 4: x = 96
n = 5: x = 113
Data Structure: [105, 107, 86, 96, 113]
Results:
# DATA: 5 (N)
SUM: 507 (U)
MEAN: 101.4 (M)
STD DEV: 10.54988151 (S)
Eddie
All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.
The author does not use AI engines and never will.