ELSE in Python WHILE Loops - Python Tutorial for Beginners || Tutorial 41

Опубликовано: 31 Март 2021
на канале: NG Allrounder
25
0

Python File Of this Tutorial:- https://drive.google.com/file/d/1cb8Y...
🎓 Welcome back to NG Allrounder, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ ELSE in Python WHILE Loops

The common construct is to run a Loop, and search for an item. If the item is found, we break out of the loop, using the break statement. But, there are 2 scenarios in which the loop may end:

The first one is when the item is found and break is encountered.
The second scenario is that the loop ends without encountering a break statement.

Now we may want to know which one of these, is the reason for a loop’s completion. One method is to set a flag, and then check it once the loop ends. Another, is to use an optional else clause, at the end of for loop.

The ELSE clause in Python will be executed only if the loop terminates naturally, through the exhaustion of the iterable object. On the other hand, if the loop terminates prematurely, with break, the else clause will not be executed - at all.

--

Here is the basic structure, of a while-else loop. First, let's assume you declared a list with some colours inside. Now, you want to search for a specific item - which in that case, is the colour blue.

If you find it, you want to break this loop, then continue with the next statements, after the loop. But, on the other hand, if you didn't find that item, you want to execute some code first, then and only then, execute the same code that if you found it, and proceed with the rest of your code.

As you may have guessed, the function not_found_in_iterable() will never be called in this example, since the while loop will terminate prematurely - as soon as it encounters the item 'blue'.

colours = ['red', 'green', 'blue', 'yellow']

while colours:

colour = colours.pop()

Found it!
if colour == 'blue':
print("Found", colour)
break

else:
Didn't find anything!
not_found_in_iterable()

always_executed()


Let's play this video, stick around and watch until the end of this video! 👍🏻

NG Allrounder 🎓
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
Relevancy dies in a heartbeat, and I would love to sustain this channel in the long run. Every subscriber counts and it really means the world if you were one of them. So please SUBSCRIBE to my CHANNEL.


Most Engrossing Videos🎬
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
*Complete Courses*
Complete Python Course:-
   • Complete Python Tutorials in Hindi(2021)  


Complete C++ Course:-
   • C++ Tutorials in Hindi Using Turbo C++  
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
*tags*
else in while loop python,
else in loop python,
else in python loop,
else in while loop,

python for beginners,
python basics,
loop else statement in python,
python else while loop,
while else python,
python while else loop example,
python continue loop if error,
while loop with else,
execute code after while loop,
python tutorial for beginners,
python tutorial,
python else loop,
python else in loop,
python else in while loop,
python while else,
Python loops

*Tags*
#Python #Tutorial #pythoncourse


Смотрите видео ELSE in Python WHILE Loops - Python Tutorial for Beginners || Tutorial 41 онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь NG Allrounder 31 Март 2021, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 25 раз и оно понравилось 0 людям.