0

in Python:

ele = 0
for ele in range(0,4):
    print('-')
print(ele)

I realized that this will print 3 instead of 4 in the end, which is different from C-style for loop. is while the option to achieve C-style behavior?

3
  • Why not print(ele+1)? Commented Mar 20, 2020 at 7:56
  • 3
    Why can't you just use for ele in range(1,5):? Commented Mar 20, 2020 at 7:59
  • What is it you are trying to achieve? If you want to know whether or not the loop stopped prematurely because of a break, you can use for .... else ... where the else clause will only be executed if the loop wasn't terminated using a break. (In C you would check the value of ele and if it is 4, you'd know the loop wasn't terminated using a break, hence this suggestion) Commented Mar 20, 2020 at 8:10

6 Answers 6

1

Try else. Statements under else will be executed exactly once after the code gets out of the loop.

for ele in range(0,4):
    print('-')
else:
    ele=ele+1
print(ele)

Output:

-
-
-
-
4
Sign up to request clarification or add additional context in comments.

Comments

1

You can add an independent variable to count the loop iterations with enumerate():

N = 4
items = range(0, N)
i = 0
for i, x in enumerate(items, 1):
    print(i, ":", x)
print("number of loop iterations done:", i)

You should make sure to reset the value before the loop, as it is not set to zero, when the loop did not iterate even once.

For reference:

Comments

0

In python the range function works differently, if you want to print something N times try this:

ele = 0
for i in range(N):
    print("-")
print(ele)

Also is not the only approach you could take for printing, you can also:

print("-\n"*(N-1) + "-" + ele)

The range() function works in different ways:

  1. range(N) which means a list from 0 to N-1, i.e, a list containing N elements
  2. range(A,B) which means a list from integer A to integer B
  3. range(A,B,k) which means a list starting from integer A ending before B and with increments k

You can also refer to the official documents: docs.python.org/3/library/functions.html#func-range

Hope this helps :D!

Comments

0
ele = 0
for ele in range(0,4):
    print('-')
print(ele+1)

1 Comment

this wont work for me, as i intend result to stay 0 if loop is not executed
0

for ele in range(param1, param2), it means param1 <= ele < param2

if you want to get the value of param2, you might change the value bigger than param2.

Comments

0

A for with a range is roughly equivalent to a for on a list, i.e. your loop could also be written as:

for ele in [0, 1, 2, 3]:
    print('-')

If you want ele to be equal to the number of loops after executing the for instruction, you could also write:

for ele in [1, 2, 3, 4]:
    print('-')

which can be rewritten using range as:

for ele in range(1, 5):
    print('-')

since range goes from the first parameter to the number immediately before the second parameter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.