Else keyword conditions with loops in python | Prime number with for loop | list slicing in Python

Опубликовано: 16 Май 2026
на канале: Programming Guru
46
5

#else_condition_with_loops #list_slicing_in_python #prime_number_with_for_loops #loopsinpython #pythonforbeginners #pythonforabsolutebeginners #pythonbysohaibjahangir

Else keyword conditions with loops in python | Prime number with for loop | list slicing in Python
________________________________
following are the topics discussed in the above lecture
00:00 introduction
00:15 how to use Else condition with loops in python
03:35 how to find prime numbers with for loops in Python
09:00 how to do lilt slicing in python
12:25 conclusions
_____________________________________________

1.While and For Loops in python
   • While and For Loops in python | python loo...  

2.PYTHON GUIDE | MASTER PYHTON IF statment
   • PYTHON GUIDE | MASTER PYHTON IF STATMENT W...  

3.Python Dictionary| Mastering Python Dictionaries in 7 Minutes |Ultimate guide to Python Dictionaries
   • Python Dictionary| Mastering Python Dictio...  

4. ITERATING OVER PYTHON DICTIONARIES | PYTHON BREAK CONTINUE & PASS | PYTHON FOR ABSOLUTE BEGINNERS
   • ITERATING OVER PYTHON DICTIONARIES | PYTHO...  

In this video we're going to learn about
the else keyword that we've used previously
with if statements but now we're going
to use it with a loop.
Let's look at this code that you saw in the last video.
We've got our cars, which is a list of strings.
It contains multiple okay strings and a faulty one.
Then we've got a for loop.
Where we iterate for status in cars.
And then if the status is faulty
we're going to print, stopping the production line
and we're going to break.
So in this case, when we encounter the break keyword,
we completely terminate the loop.
Then, if it wasn't faulty,
we're going to print that this car is okay.
And we're going to ship it away to the customer.
Imagine you wanted to print something out,
if all of the cars were shipped successfully.
Because at the moment you're going to
print something out when you find a faulty one.
But you can't print something out
when there are no faulty ones.
A naive approach, something that a new programmer might do
is might say, all successful is true.
And then, if a car status is faulty,
then you set it to false.
All successful is false.
Because clearly, one of them was faulty.
So you set all successful to false,
and now after the loop you can say,
if all successful,
then print all cars built successfully.
No faulty cars.
This would be a naive approach.
And an approach that involves a lot of coding
in order to not use something that's already
built into the language.
What we do here is we set a variable, to true,
and then only when we encounter a problem
do we set it to false.
Then we break.
And then after the loop,
if they were all successful, i.e. we did not break,
we did not encounter a faulty car,
then we print that they were all built successfully.
But actually, the loops in Python
already come with something like this.
There is a way to run a piece of code
if the loop did not encounter a break.
And that, is actually the else keyword
that we've seen before.
Now we've got the for loop,
then the for loop body,
and then this else statement
directly in the same indentation level as the for loop.
Which tells Python that they are related.
Remember, it's the same thing for if statements,
but now the else is in the same line as the for loop.
And what this means in Python,
is that if the loop completely ran
through all of the elements,
without encountering a break,
or an error, then we run this code here.
That's what it means.
So we no longer need this variable here.
We can just do this.
And you'll see that nothing gets printed out.
All cars built successfully.
No faulty cars, does not appear because
we stop the production line halfway through.
But if we remove the faulty, then you'll see that
all cars were built successfully.
No faulty cars.
So the else keyword can actually be applied to loops,
both the for loop and the while loop.
And it means that this code here will run
if the loop runs to completion,
i.e. there are no breaks and no errors.
This is actually something that not a lot of people know.
That you can use the else keyword on loops,
but it can save you quite a few lines of code,
and it can make your code a bit easier
to read, as well.
So I hope you've learned something in this video.
____________________________
#continue_in_python
#python
#Learn_Python
#python_tutorial_for_beginners
#python_tutorial
#while_Loop
Else keyword conditions with loops in python | Prime number with for loop | list slicing in Python
Else keyword conditions with loops in python
list slicing in Python
Prime number with for loop
else with while loop in python
for loop with else in python
python slice list by value
what is slicing in python with example
list slicing in python exercises
how to do list slicing in python
list slicing in python definition
negative list slicing in python
prime number program in python using for loop
python find prime numbers in range
python for absolute beginners
python