2

I am attempting to trace the execution of a piece of code that contains a for loop with two if conditionals. But I need help understanding exactly how for loops are executed in python.

Please consider the following example:

  numAs = 0
  numEs = 0
  aStr1 = 'abcdefge'
   def someFunc(aString):
1.     for i in range(len(aString)): 
2.           if s[i] == 'a': 
3.               numAs += 1 
4.               continue 
5.           if s[i] == 'e': 
6.               numEs += 1 
7.               if numEs > numAs:  
8.                    break 
9. print(someFunc(aStr1))

Question: Using aStr as a parameter how many times will line 1 execute in the above code?

My understanding is that line 1. of this piece of code: for i in range(len(aString)), will only be executed once. While lines 2 and 5 will be executed multiple times depending on the string that is passed. When the function encounters the continue statement it goes back to line 2 and runs it. Please confirm or correct my thinking.

Thanks

5
  • What definition of the continue statement have you read? Can you provide a link or a reference to the tutorial you're using? It's not clear what you've read, so it's not clear what you're really asking. Commented Nov 7, 2011 at 3:45
  • the book i am reading is 'Python Programming in Context' by Brad Miller. This question is not from the book i'm just trying to understand exactly how iteration works. I've made a few edits to my question. hopefully it is now more clear. Commented Nov 7, 2011 at 4:07
  • Not related to the topic at hand, but you could just do for c in aString. Right now you are creating a list of n integers (n being the length of the string, retrieving integers from that list one at a time, and using them to index into a string, when you could just iterate directly over the characters of the string. Commented Nov 7, 2011 at 5:36
  • "exactly how iteration works"? What do you mean by "exactly"? It seems like you're asking several things about looping, some of which are trivial and some of which obvious. Can you be more clear on what's confusing you. Commented Nov 8, 2011 at 2:56
  • question has already been answered Commented Nov 9, 2011 at 13:02

1 Answer 1

3

Your understanding of what the code does is correct.

FWIW, it is easy to follow the execution of a script using python -m trace --trace some_script.py or you can see the execution line counts with python -m trace --count some_script.py.

For example, the latter call to trace produces:

    1: def someFunc(aString):
            global numAs, numEs
    1:      s = aString
   10:      for i in range(len(aString)): 
    9:            if s[i] == 'a': 
    2:                numAs += 1 
    2:                continue 
    7:            if s[i] == 'e': 
    1:                numEs += 1 
    1:                if numEs > numAs:  
                           break

    1: numAs = 0
    1: numEs = 0
    1: someFunc('flammable')
Sign up to request clarification or add additional context in comments.

2 Comments

okay so this is saying that the line for i in range(len(aString)): was ran ten times?
Yes, it ran ten times (including the final iteration that determined that there was no more work to do).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.