6.15: Python Examples
- Page ID
- 10685
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
Arrays
# This program demonstrates array processing, including:
# display, total, max, min, parallel arrays, sort
# multidimensional arrays, and dynamic arrays.
def display_array(array):
for index in range(len(array)):
print('array[' + str(index) + '] = ' +
str(array[index]))
def sum(array):
total = 0
for index in range(len(array)):
total += array[index]
return total
def max(array):
maximum = array[0]
for index in range(1, len(array)):
if maximum < array[index]:
maximum = array[index]
return maximum
def min(array):
minimum = array[0]
for index in range(1, len(array)):
if minimum > array[index]:
minimum = array[index]
return minimum
def display_parallel(names, ages):
for index in range(len(names)):
print(names[index] + ' is ' +
str(ages[index]) + ' years old')
def display_multidimensional():
game = [
['X', 'O', 'X'],
['O', 'O', 'O'],
['X', 'O', 'X'] ]
for row in range (0, 3):
for column in range(0, 3):
print(game[row][column], end='')
if column < 2:
print(' | ', end='')
print()
def dynamic_array():
import random
array = []
for index in range(5):
array.append(random.randint(0, 100))
display_array(array)
def main():
names = ['Lisa', 'Michael', 'Ashley', 'Jacob', 'Emily']
ages = [49, 48, 26, 19, 16]
display_array(names)
display_array(ages)
total = sum(ages)
maximum = max(ages)
minimum = min(ages)
print('total: ' + str(total))
print('maximum: ' + str(maximum))
print('minimum: ' + str(minimum))
display_parallel(names, ages)
ages.sort()
display_array(ages)
display_multidimensional()
dynamic_array()
main()
Output
array[0] = Lisa array[1] = Michael array[2] = Ashley array[3] = Jacob array[4] = Emily array[0] = 49 array[1] = 48 array[2] = 26 array[3] = 19 array[4] = 16 total: 158 nmaximum: 49 minimum: 16 Lisa is 49 years old Michael is 48 years old Ashley is 26 years old Jacob is 19 years old Emily is 16 years old array[0] = 16 array[1] = 19 array[2] = 26 array[3] = 48 array[4] = 49 X | O | X O | O | O X | O | X array[0] = 71 array[1] = 39 array[2] = 80 array[3] = 35 array[4] = 40

