PYTHON ITERATION CHAPTER 7 READ!!!

advertisement
PYTHON
ITERATION
CHAPTER 7 READ!!!
FROM
THINK PYTHON
HOW TO THINK LIKE A COMPUTER SCIENTIST
THE FOR LOOP
We already looked at an instruction that does iteration.
It was the for command
for i in range(5):
w = ‘hello’
for s in range(5)
print i
print w[s]
>>>
>>>
0
h
1
e
2
l
3
l
4
o
>>>
>>>
WHAT DOES THIS MEAN
X=X+1
x = 10
print x
x=x+1
print x
x=x+1
print x
#output is
10
11
12
14
Increments x by 1
x=x+2
print x
Increments x by 2
THE WHILE LOOP
x=1
while x < 10:
print x,
x=x+1
# this is the condition
#The body of the loop (repeated until the
# while condition is false)
#OUTPUT is
123456789
What if we forget this instruction!
WHAT? IS 1+2+3+…+N
Mathematically we have
+
2
+
3
+ … + (n-1) + n
S=
1
S=
n + (n-1) + (n-2) + … +
2
+1
2S = (n+1)+(n+1)+ (n+1) + … (n+1)+(n+1)
2S = n(n+1)
S = n(n+1)/2
 Sum of the numbers from 1 to n
S = 100(101)/2 = 5050  Sum of the numbers from 1 to 100
LETS DO THIS IN PYTHON
( IN TWO WAYS)
#using a for
s=0
for i in range(100)
s=s+i+1
 Again, why do I have a 1 here
Because range(100) is [0,1,2,3,4,…,99]
print ‘The sum from 1 to 100 is’,s
#using a while loop
i=1
s=0
while i<=100:
s=s+i
i=i+1
print ‘The sum from 1 to 100 is’,s
ADD EVEN NUMBERS
FROM 2 TO 100
i=2
# first number is 2
s=0
while i<=100:
# stop looping at 100
s=s+i
i=i+2
# increment by 2
print ‘The sum of evens from 2 to 100 is’,s
What about the odds.?
What about the sum of squares from 1 to 100?
WHAT DOES THIS
FUNCTION DO????
def sequence(n):
while n!=1:
print n,
if n%2 == 0:
n = n/2
Will this always
terminate?
else:
n=n*3+1
sequence(101)
>>>
101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5
16 8 4 2
>>>
HOMEWORK: CALCULATE THE
PERCENTAGE OF EVENS IN THE
SEQUENCE
This is the sequence for 101
>>>
101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10
5 16 8 4 2
>>>
Its percentage is 18/25.0 =72.0
What about all the values from 2 to 101?
Calculate and graph the values in excel.
100
97
94
91
88
85
82
79
76
73
70
67
64
61
58
55
52
49
46
43
40
37
34
31
28
25
22
19
16
13
10
7
4
1
HERE IS MY OUTPUT
????
120
100
80
60
40
20
0
CALCULATING
SQUARE ROOT
A very old method for calculating the square root of a number is to use Newton’s method.
Suppose we would like to find the square root of a. We begin using a guess x and use the
following formula to get a closer estimate y of the square root .
𝒂
𝒙+
𝒙
𝒚=
𝟐
def sqroot(a):
x = a/2.0 # our initial estimate
while abs(a-x**2)> .0000001:
x = (x + a/x)/2.0
return x
print sqroot(4)
print sqroot(16)
print sqroot(25)
>>>
2.0
4.0
5.00000000002
>>>
READ A SET OF NUMBERS FROM THE
KEYBOARD AND DO SOMETHING TO
THEM
s=0
ct=0
while True:
x = raw_input('Enter a number ') # Remember x is a str!
if x == 'done':
break
# The break instruction exits the loop
# Do something to the values of x
ct = ct+1 #count the numbers
s = s+ float(x)
#accumulate them and convert x to a float
# Display the results
print 'The average is ',float(s)/ct
DETERMINE IF A NUMBER IS
PRIME
# This is a very inefficient algorithm. Can you do it better?
def prime(n):
if n < 2 :
return False
ans = True
for i in range(2,n):
if n%i == 0 :
ans=False
return ans
print prime(5)
print prime(64)
print prime(101)
print prime (1001)
>>>
True
False
True
False
>>>
PRIMENESS OF
1000001?
Suppose I would like to determine if 11, 101,1001,10001,
100001, 1000001, and so on are prime. How can I do this?
First lets just print these.
for i in range(1, 20):
v = 10**i + 1
print v,
READ IN A STRING AND COUNT
SPECIFIC CHARACTERS
#Count the number of a’s in the string
x = raw_input('Enter a dna sequence ( i.e. actggect) ')
ct_a=0 #variable we count with
for i in range(len(x)):
if x[i]=='a':
ct_a=ct_a + 1
print ct_a
EXAMPLE PROBLEMS
1. Read in integers until 0 is entered, then print the sum,
average, largest and smallest of the entered numbers
2. Read in real numbers from the keyboard and calculate
and print the square root of the numbers using Newton’s,
method. See chapter 7. Stop when ‘done’ is typed.
3. Do exercise 7.5 ( Ramanujan’s pi formula)
4. Write a function called prime(p) that returns true if p is
prime and false otherwise. Test it carefully
5. (a little tougher) Print out the prime factorization of a
number.
6. Write a function that receives and input a dna string and
a char ( a,c,t,g) and then returns the number of copies of
that character that exist in the string.
Download