1 / 16

Python Iteration chapter 7 READ!!!

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 w = ‘hello’

Download Presentation

Python Iteration chapter 7 READ!!!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PythonIterationchapter 7 READ!!! From Think Python How to Think Like a Computer Scientist

  2. The for loop • We already looked at an instruction that does iteration. • It was the for command w = ‘hello’ • for i in range(5): for s in range(5) • print i print w[s] • >>> >>> • 0 h • 1 e • 2 l • 3 l • 4 o • >>> >>>

  3. What does this mean x = x + 1 • x = 10 • print x • x = x + 1 • print x • x = x + 1 • print x • x = x + 2 • print x #output is 10 11 12 14 Increments x by 1 Increments x by 2

  4. The While Loop • x = 1 • while x < 10: # this is the condition • print x, • x = x + 1 • #OUTPUT is • 1 2 3 4 5 6 7 8 9 #The body of the loop (repeated until the # while condition is false) What if we forget this instruction!

  5. What? is 1+2+3+…+n • Mathematically we have • S = 1 + 2 + 3 + … + (n-1) + n • 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

  6. 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

  7. 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?

  8. What does this function do???? • def sequence(n): • while n!=1: • print n, • if n%2 == 0: • n = n/2 • else: • n = n * 3 + 1 • sequence(101) • >>> • 101 304152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 • >>> Will this always terminate?

  9. Homework: Calculate the percentage of evens in the sequence • This is the sequence for 101 • >>> • 101 304152 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.

  10. Here is my output ????

  11. 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 . • defsqroot(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 >>>

  12. Read a set of numbers from the keyboard and do something to them • s=0 • ct=0 • whileTrue: • 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

  13. Determine if a number is prime • # This is a very inefficient algorithm. Can you do it better? • defprime(n): • if n < 2 : • return False • ans = True • for i in range(2,n): • if n%i == 0 : • ans=False • returnans • print prime(5) • print prime(64) • print prime(101) • print prime (1001) >>> True False True False >>>

  14. 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,

  15. 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

  16. Example problems • Read in integers until 0 is entered, then print the sum, average, largest and smallest of the entered numbers • 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. • Do exercise 7.5 ( Ramanujan’s pi formula) • Write a function called prime(p) that returns true if p is prime and false otherwise. Test it carefully • (a little tougher) Print out the prime factorization of a number. • 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.

More Related