What is for-else and while-else in python?



What is for-else and while-else in python?..

Answer / Rahul Tiwari

For-else and while-else statements are used to execute a block of code when the loop does not terminate naturally. The block will be executed only if the loop completes its iteration without being interrupted by a break statement:n```pythonnfor i in range(10):n if i == 5:n breaknelse:n print('Iteration completed')nn # Output: Iteration completednIteration completedn...nIteration completednwhile True:n number = input('Enter a number (type "q" to quit): ')n if number == 'q':n breakn print(number ** 2)nn # Output:nEnter a number (type "q" to quit): 5n25nEnter a number (type "q" to quit): q"```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

Is monkey patching considered good programming practice?

1 Answers  


Explain supported data types in python?

1 Answers  


How to import modules in python?

1 Answers  


Different file processing modes supported by python?

1 Answers  


What is meant by r strip() in python?

1 Answers  


Can I use python instead of javascript?

1 Answers  


What is a lambda form?

1 Answers  


Explain garbage collection with python?

1 Answers  


How do I sort a list in python 3?

1 Answers  


Explain about python operators?

1 Answers  


Explain about raising error exceptions?

1 Answers  


How can I wait until we receive data using python socket?

1 Answers  


Categories