Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused about python looping

Tags:

python

I'm confused about the iteration process in python

I have:

numbers = [0,1,2,3,4,5]
for i in numbers:
    v = numbers
    print v

Here is the end of loop but, in C , C++ , Java we know the line inside the { } will be executed repeatedly, or if not given curly braces then the next statement after the loop, but I'm confused here. How many lines will be executed in the iteration? Does it depend on indent?

like image 819
ssi-anik Avatar asked Sep 08 '26 14:09

ssi-anik


1 Answers

In Python, the indentation, rather than brackets, determines block scope, so in this example both the indented lines will be executed once per iteration of the loop.

This is the single weirdest thing about Python for programmers coming from the C/Java language families, but you get used to it fairly quickly.

like image 96
Russell Borogove Avatar answered Sep 11 '26 04:09

Russell Borogove



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!