Linked Questions

19 votes
8 answers
14k views

Pythonic ways to use 'else' in a for loop [duplicate]

I have hardly ever noticed a python program that uses else in a for loop. I recently used it to perform an action based on the loop variable condition while exiting; as it is in the scope. What is ...
lprsd's user avatar
  • 87.8k
15 votes
2 answers
4k views

Why is the purpose of the "else" clause following a "for" or "while" loop? [duplicate]

I am a Python beginner. I find that the else in for-else and while-else is completely unnecessary. Because for and while will finally run to else, and we can use the usual lines instead. For example: ...
Jimmy's user avatar
  • 353
7 votes
3 answers
5k views

What is the use of "else" after "for" loop in Python? [duplicate]

It seems both of the below codes are printing the same, then what is the need of "else" block after "for" loop in python. Code 1: for i in range(10): print i else: print "after for loop" ...
Rajesh Kumar's user avatar
  • 1,308
3 votes
1 answer
5k views

What is the method for 'If loop does NOT break, run code'? [duplicate]

I am trying to write a for-loop to go through values, and if by the end of the loop it did not break, I want it to run some code. For example: for i in range(10): if someCondition: break #...
TheRaidGaming's user avatar
4 votes
4 answers
644 views

What is the benefit(s) of having 'else clause' for the while loop in python? [duplicate]

Any code after while loop will execute when the condition in the while loop becomes False. It is the same for the code in the 'else clause' section of while loop in python. So What's the advantage of ...
Kamran Bigdely's user avatar
3 votes
1 answer
1k views

Else block without if block in python [duplicate]

I found the Python Code snippet online to print the range of the prime number but the last else block is making no sense to me as it doesn't has corresponding if block Note The Indentation of else ...
Hasnain Ali's user avatar
2 votes
2 answers
387 views

Python for-else relationship [duplicate]

I can run the below python script without errors. for n in range(3): print n else: print "done" But I am puzzled about the else without a matching if. It does not make sense. Can some one ...
Srini's user avatar
  • 23
2 votes
3 answers
2k views

How to fix a problem with input() in python? [duplicate]

I'm creating my first program on python. The objective is to get an output of trip cost. In the below code I want python to throw an error and ask user to retry if the input is not a part of the ...
ragethewolf's user avatar
2 votes
1 answer
3k views

Necessity of break in nested if statement in a for loop [duplicate]

Why is the break needed in the following code to skip processing of the else statement? Why doesn't the if evaluation exit the program by itself, without continuing to process the else statement as ...
Lucien Semyon's user avatar
0 votes
3 answers
1k views

Order of execution in nested loops in python [duplicate]

I cant understand the difference between those two code blocks, first the correct one is: number_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] prime_list=[] for i in number_list: for j in ...
ErrorWhile's user avatar
3 votes
3 answers
172 views

Why does this `else` block work yet it is not on the same level as the `if` case? [duplicate]

This code runs pretty well and generates the wanted list of prime numbers. But the else block that prints our prime numbers is out of block, but it works anyway, can someone explain it to me? for num ...
Kaka Ruto's user avatar
  • 5,183
1 vote
1 answer
687 views

Alignment of else: under if: in python [duplicate]

I would like to understand why the following python code to find all prime numbers less than n works: def prime(n): for q in range(2,n): for i in range(2,q): if q%i==0: ...
Valerio's user avatar
  • 285
-2 votes
1 answer
1k views

How does putting an if statement inside a loop and an else statement outside the loop work together (in python) [duplicate]

L=eval(input("Enter a list of elements(number): ")) n=len(L) print("The original list: ") print(L) x=int(input("Enter an element to be searched in the list: ")) for i in ...
Ritam De's user avatar
0 votes
1 answer
971 views

If-else indentation [duplicate]

I am playing with python3.5 right now. I was working on a simple code, to find the prime numbers less than a given integer. I solved the code more by mistake and I don't know why. This is the code: n=...
Claudiu B's user avatar
2 votes
2 answers
214 views

Why it skips else block [duplicate]

We know Python loops have extra features that are not available in most other programming languages. You can put an else block immediately after a loop's repeated interior block. for i in range(3): ...
mhhabib's user avatar
  • 3,137

15 30 50 per page
1
2 3 4 5
9