![Page 1: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/1.jpg)
Input and typing trouble!
print 'Please input a number of meters'
meters = input() # get input from usercm = meters * 100 # convert to centimeters
print ('That is', cm, 'cm.’) # print out the result
What is python thinking ?!?
![Page 2: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/2.jpg)
Fix #1: use a type converter
print 'Please input a number of meters'
meters = int(input()) cm = meters * 100
print ('That is', cm, 'cm.’)
check out my newly installed int converter!
The type of variable (box) matters!name: meters
type: intname: cmtype: int
1 100
![Page 3: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/3.jpg)
Fix #1: use a type converter
print 'Please input a number of meters'
meters = int(input()) # get int input from usercm = meters * 100
print ('That is', cm, 'cm.’)
print 'Please input a number of meters'
meters = float(input()) # get float input from usercm = meters * 100
print ('That is', cm, 'cm.’)
str converts to string type
![Page 4: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/4.jpg)
Fix #2: use input()
print 'Please input a number of meters'
meters = eval(input()) # get processed input from usercm = meters * 100
print ('That is', cm, 'cm.’)Why not ALWAYS
use eval() ?
![Page 5: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/5.jpg)
Fix #2: use input()
print 'Please input a number of meters'
meters = eval(input()) # get processed input from usercm = meters * 100
print 'That is', cm, 'cm.'Why not ALWAYS
use input() ?
input() always returns input as a string!
eval(s) processes string s as if typed into Python
Input allows you to specify a prompt string
![Page 6: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/6.jpg)
input vs. eval(input())
reply = input('Enter a string and I\'ll tell you what I see.')
for c in reply:
print ('I see a(n) ', c)
interprets what the user types as a
string of characters
reply = eval(input('Enter any list and I\'ll tell you what I see.'))
for c in reply:
print ('I see a(n) ', c)
processes what the user types just as
python would
![Page 7: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/7.jpg)
import randomescape = 0
while escape != 42: print ('Help! Let me out!’) escape = random.choice([41,42,43])
print ('At last!’)
how could we make it easier/harder to escape?
how could we count the number of loops we run?
Give me a break !
Every while loop can be a while True: loop!
I'll figure out later how to get out of this loop!
OK – I'll stop when the guess is correct!
Try it!
![Page 8: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/8.jpg)
I've always thought there were indeed two kinds of people in the world - those who divide the world into
two kinds of people, and those who don't.
The boolean type
There are 10 kinds of people in the world: those who understand binary, and those who don't.
![Page 9: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/9.jpg)
The boolean type
is either True or False .
It's needed in if and while statements:
if test: print ('test was True!’) do something more …
while test: print ('test was True!’) do something more …
You can even convert to the boolean type with bool
bool(42)
bool('hi')
bool([1,2,3])
these both have to be boolean.
![Page 10: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/10.jpg)
A guessing game…
import random
myNumber = random.choice(range(1000))
yourGuess = -1
while yourGuess != myNumber:
yourGuess = eval(input('Guess my number: '))
print ('You got it! It was', myNumber)
This is too difficult!
Notice – we don't know how many times we need
to loop!
![Page 11: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/11.jpg)
Give me a break !
import random
myNumber = random.randrange(0,1000)
while True:
yourGuess = input('Guess my number: ')
if yourGuess == myNumber:
break
print 'You got it! It was', myNumber
Every while loop can be a while True: loop!
I'll figure out later how to get out of this loop!
OK – I'll stop when the guess is correct!
Try it!
![Page 12: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/12.jpg)
Loops!Hey, could you change the
channel?And some more chips would be
nice… .
for c in 'a gratuitously big string':
print ('I see a(n) ', c)
print 'Phew! Can I go watch TV now?'
A three-line program…
this is a for loop
you can use any variable name you
want!
c will become each item in the list
once for each value of c, the computer does everything in the loop (the indented
block)
this can be ANY list, even a variable!
This will not happen until after the for loop is finished.
![Page 13: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/13.jpg)
Loops!Hey, could you change the
channel?And some more chips would be
nice… .
Who (or what) should be doing most of the work around
here ?!!
reply = input('Enter a string and I\'ll tell you what I see.')
for c in reply:
print ('I see a(n) ', c)
What if we input a list here?
this can be ANY list, even a variable!
(and not just a string…)
![Page 14: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/14.jpg)
Accumulating an answer…
Suppose we want to find and print the sum of an arbitrary list:
[ 9, 42, 1, 77, 15, 76, 100, -5, 3.14 ]
How could we use a loop? Try it out!
L = eval(input('Enter a list '))
![Page 15: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/15.jpg)
Accumulating an answer…
L = eval(input('Enter a list '))
sumSoFar = 0
for x in L:
sumSoFar = sumSoFar + x
print ('sum is', sumSoFar)
Finding the sum of a list:
shortcut available!
No return needed if you are just printing the value!
Accumulator!
![Page 16: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/16.jpg)
Typing trouble!
print ('Please input a number of meters’)
meters = input() # get input from usercm = meters * 100 # convert to centimeters
print ('That is', cm, 'cm.’) # print out the result
The type of variable (box) matters!name: meters
type: stringname: cmtype: string
'1' '111…11'(100 of them!)
Variables are registers or storage spaces…
how to fix this?
![Page 17: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/17.jpg)
Smaller of Two…
print 'Please input two numbers'
n1 = eval(input())n2 = eval(input())
if n1 > n2: print ('The smaller number is', n2)
else: print ('The smaller number is', n1)
for the visually minded… ?
![Page 18: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/18.jpg)
minOf2
print a message
get n1
get n2
is n1>n2 ?TRUE FALSE
print that n2 is the min
print that n1 is the min
Flow Chart
if else
print 'Please input two numbers'
n1 = eval(input())n2 = eval(input())
if n1 > n2: print ('The smaller number is', n2)
else: print ('The smaller number is', n1)
![Page 19: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/19.jpg)
Accumulate!
print 'Please input two numbers'
n1 = eval(input())n2 = eval(input())
if n1 > n2: min = n2else: min = n1 print ('The minOf2 is', min)
Accumulator
min
what's different here?
![Page 20: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/20.jpg)
print 'Please input two numbers'
n1 = eval(input())n2 = eval(input())
if n1 > n2: min = n2else: min = n1 print ('The minOf2 is', min)
Smaller of Two…
print a message
get n1
get n2
is n1>n2 ?TRUE FALSE
print min
set min to be n1
if else
set min to be n1
Accumulator
min
![Page 21: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/21.jpg)
One problem Two algorithms
Both are correct.
Which is easier to extend
to minOf3 ?
print a message
get n1
get n2
is n1>n2 ?TRUE FALSE
print that n2 is the min
print that n1 is the min
if else
print a message
get n1
get n2
is n1>n2 ?TRUE FALSE
print min
set min to be n1
if else
set min to be n1
![Page 22: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/22.jpg)
List applications
LightPath, late 1999
"broker than before"
![Page 23: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/23.jpg)
List applications
LightPath, early 2000
![Page 24: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/24.jpg)
broker than before…
LightPath, 2006…
![Page 25: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/25.jpg)
T. T. Securities
The future of securities trading and analysis!
![Page 26: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/26.jpg)
T. T. Securities
Input stock prices for a number of days in a row, and then analyze that data… .
SoftwareStock-price Analysis
• maximum price• day of the max price• minimum price• day of the min price• sum of all of them• average of all of them• standard deviation of all of them• find the best T.T.S. strategy…• mode (most common value - extra)
![Page 27: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/27.jpg)
T. T. Securities
Input stock prices for a number of days in a row, and then analyze that data… .
SoftwareStock-price Analysis
• maximum price• day of the max price• minimum price• day of the min price• sum of all of them• average of all of them• standard deviation of all of them• find the best T.T.S. strategy…• mode (most common value - extra)
![Page 28: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/28.jpg)
Given a list of stock prices
L = [ 20, 21, 22, 100, 570, 35, 10, 3.5 ]
• maximum price• day of the max price• minimum price• day of the min price• sum of all of them• average of all of them• standard deviation of all of them• find the best T.T.S. strategy…• mode (most common value)
Print them nicely…
And print the following data:
Day: 0 Price: 20Day: 1 Price: 21Day: 2 Price: 22Day: 3 Price: 100Day: 4 Price: 570Day: 5 Price: 35Day: 6 Price: 10Day: 7 Price: 3.5
![Page 29: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/29.jpg)
Accumulating a max…
Remember the work of max/min?
Suppose we have to find the max of a HUGE list …
[ 20, 21, 22, 100, 570, 35, 10, 3.5 ]
![Page 30: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/30.jpg)
Accumulating an answer…
L = eval(input('Enter a list '))
maxSoFar = 0
for x in L:
if x > maxSoFar:
maxSoFar = x
print ('max is', maxSoFar)
Finding the max of a list:
something's wrong!printing one's mind!
![Page 31: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/31.jpg)
Accumulating an answer…
L = eval(input('Enter a list '))
maxSoFar = L[0]
for x in L[1:]:
if x > maxSoFar:
maxSoFar = x
print ('max is', maxSoFar)
Finding the max of a list:
Indexing
Slicing
what day?
![Page 32: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/32.jpg)
Accumulating an answer…
[ 20, 21, 22, 100, 570, 35, 10, 3.5 ]
L = eval(input('Enter a list '))
maxSoFar = L[0]
for x in L[1:]: if x > maxSoFar: maxSoFar = x
print ('max is', maxSoFar)
accumulator variable
how many times will this happen?
Finding the maximum
![Page 33: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/33.jpg)
Determine the max's day …
L = eval(input('Enter a list '))
maxSoFar = L[0]
for x in L[1:]:
if x > maxSoFar:
maxSoFar = x
print ('max is', maxSoFar)
print ('the day of the max is', ????
Finding the day of the max of a list:
![Page 34: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/34.jpg)
Slow… Accumulation …
[ 20, 21, 22, 100, 570, 35, 10, 3.5 ]
Printing a list of days and prices
Day: 0 Price: 20Day: 1 Price: 21Day: 2 Price: 22Day: 3 Price: 100Day: 4 Price: 570Day: 5 Price: 35Day: 6 Price: 10Day: 7 Price: 3.5
desired output
what's accumulating here?
![Page 35: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/35.jpg)
Slow Accumulation …
L = eval(input('Enter a list '))
day = 0
for x in L: print ('Day:', day, 'Price:', x)
[ 20, 21, 22, 100, 570, 35, 10, 3.5 ]
Printing a list of days and prices
something's wrong here!
![Page 36: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/36.jpg)
Slow Accumulation …
L = eval(input('Enter a list '))
day = 0
for x in L: print ('Day:', day, 'Price:', x) day = day + 1
[ 20, 21, 22, 100, 570, 35, 10, 3.5 ]
Printing a list of days and prices
This reminds me of another 90's
breakout album…
![Page 37: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/37.jpg)
A good reason not to be a computer!
Finding the standard deviation of a list …
(L[i] - Lav)2
len(L)
URL
(1) find the average,
Lav(2) find the sum of (x-Lav)
2
for x in L(3) divide by the length of
L (4) import math math.sqrt(…)
![Page 38: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/38.jpg)
TTS ?
Find the most profitable strategy for buying and selling the stock among the prices in the array...
Day 0 Price is 90.0Day 1 Price is 10.0Day 2 Price is 60.0Day 3 Price is 42.0Day 4 Price is 75.0Day 5 Price is 5.0
L = [ 90, 10, 60, 42, 75, 5]
Buy on ANY day…
Sell on ANY day…(but, to be realistic, we have to sell AFTER we
buy!)
This is the old 23rd-century
model !
![Page 39: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/39.jpg)
Do you remember?
most crucial
least crucial
Use eval(input())for processed values; input()for strings.
Loops repeat an action:
Loops can run by element or by index:
An accumulator is a variable that may update each iteration.
for x in L: sumSoFar = sumSoFar + x
for i in range(0,len(L)): sumSoFar = sumSoFar + L[i]
for each list elementfor each item indexwhile a test is True
use break to escape from a loop any time!
![Page 40: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/40.jpg)
s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a': N = N + 1print ('N is', N)
What do these loops print? What do they do?
s = 'gattacaaggtaaaatgca' N = 0for i in range(1,len(s)): if s[i] == 'a' and s[i-1] == 't': N = N + 1print ('N is', N)
s = 'gattacaaggtaaaatgca' MAX = 0cur = 0for i in range(0,len(s)): if s[i] == 'a': if s[i-1] != 'a': cur = 1 else: cur = cur + 1 if cur > MAX: MAX = cur print ('Max is', MAX)
s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a' or s[i] == 't': N = N + 1print ('N is', N)
1
2
3
4
![Page 41: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/41.jpg)
Loopy thinking
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
How could we find the longest sequence of 'a's ?
How could we find the number of 'a's ? How about 'a's and 't's?How could we find the number of 'ta's ?
![Page 42: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/42.jpg)
Loopy thinking
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
How could we find the longest sequence of 'a's ?
How could we find the number of 'a's ? How about 'a's and 't's?How could we find the number of 'ta's ?
s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a': N = N + 1
print ('N is', N)
![Page 43: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/43.jpg)
Loopy thinking
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
How could we find the longest sequence of 'a's ?
How could we find the number of 'a's ? How about 'a's and 't's?How could we find the number of 'ta's ?
s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a' or s[i] == 't': N = N + 1
print ('N is', N)
![Page 44: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/44.jpg)
Loopy thinking
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
How could we find the longest sequence of 'a's ?
How could we find the number of 'a's ? How about 'a's and 't's?How could we find the number of 'ta's ?
s = 'gattacaaggtaaaatgca' N = 0for i in range(1,len(s)): if s[i] == 'a' and s[i-1] == 't': N = N + 1
print ('N is', N)
![Page 45: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/45.jpg)
Loopy thinking
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
How could we find the longest sequence of 'a's ?
![Page 46: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/46.jpg)
Planning in "pseudocode"
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Loop through the string:
if we do see an 'a'
if the PREVIOUS letter is NOT an 'a'
if the PREVIOUS letter IS an 'a'
Keep track of CurRun, MaxRun
![Page 47: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/47.jpg)
Planning in "pseudocode"
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Loop through the string:
if we do see an 'a'
if the PREVIOUS letter is NOT an 'a'
if the PREVIOUS letter IS an 'a'
Keep track of CurRun, MaxRun
Start a Run! CurRun = 1
Continue our run! CurRun = CurRun + 1Check for a new maximum…
![Page 48: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/48.jpg)
Planning in "pseudocode"
s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
MAX = 0cur = 0for i in range(0,len(s)): if s[i] == 'a': if s[i-1] != 'a': cur = 1 else: cur = cur + 1 if cur > MAX: MAX = cur print ('Max is', MAX)
Loop through the string:
if we do see an 'a'
if the PREVIOUS letter is NOT an 'a'
if the PREVIOUS letter IS an 'a'
Keep track of CurRun, MaxRun
Start a Run!
Continue our run!
Check for a new maximum…
![Page 49: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/49.jpg)
Do you remember?
All data is stored in memory as a list. most crucial
least crucial
Use eval(input())for processed values; input()for strings.
Loops repeat an action for each list element.
An accumulator is a variable that may update each iteration.
Lists can be indexed and sliced into pieces.
Indexing starts at 0 (zero). L[0] is the first list item. - This alters the usual numeric notions of "first," "second," "third," etc.!
Slicing includes the left side but excludes the right side.
![Page 50: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/50.jpg)
Silicon Dreams
How does a machine think ?
Programming Language
layers of abstractio
n
Machine Instructions
Movement of electrons
Algorithms
its great- great-… grand-child
Shockley’s first
transistor
loadn r0 5 # ploadn r1 2 # pr/wkmul r0 r0 r1 # answer!
p = 5*2
How many problems am I facing in five weeks with two per week ?
![Page 51: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/51.jpg)
Silicon Dreams
How does a machine think ?
Programming Language
layers of abstractio
n
Machine Instructions
Movement of electrons
Python
Algorithms
its great- great-… grand-child
Shockley’s first
transistor
our tasks
computer’s tasks
loadn r0 5 # ploadn r1 2 # pr/wkmul r0 r0 r1 # answer!
p = 5*2
How many problems am I facing in five weeks with two per week ?
![Page 52: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/52.jpg)
Silicon Dreams
How does a machine think ?
Programming Language
layers of abstractio
n
Machine Instructions
Movement of electrons
Python
Algorithms
its great- great-… grand-child
Shockley’s first
transistor
our tasks
computer’s tasks
compile
run
program
loadn r0 5 # ploadn r1 2 # pr/wkmul r0 r0 r1 # answer!
p = 5*2
How many problems am I facing in five weeks with two per week ?
![Page 53: Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f295503460f94c41aa5/html5/thumbnails/53.jpg)
Fast Accumulation …
L = input('Enter a list ')
sumSoFar = 0
for x in L: sumSoFar = sumSoFar + x
print ('sum is', sumSoFar)
ave.? st. dev.?
Finding the sum of a list
[ 20, 21, 22, 100, 570, 35, 10, 3.5 ]