Your Python videos are great. There is a lot of information on the basics already out there, but you bring down the more complex functionality to a level that is manageable/understandable and doesn't already require you to be a coding buff to utilise or see the value in. It's definitely your niche, which is why I subscribed.
This is on my PCEP tomorrow I think since I have seen many practice questions with this. I think of it as it simply outputs this when you're adding a number but the loop won't for one iteration, but your video explains it far better than that. Thank you'
I'd never even heard of an else statement on a for loop before, but having watched this video a couple of times now, I can think of several use cases I have for it. Thanks, and keep 'em (python stuff) coming.
Damn, why do i find this now, just wrote a piece of code where this would have been much more efficient, I was forced to use found = False, and then when the loop finds a match I had to do found = True, then break and then write "if found: xxx else: " , this would have been much more easier. Oh well thanks alot, I will be using it in the future, and when I plan to refactor my existing code. Thanks Alot for all the Awesome Tutorials :)
my_list = ['Corey', 'Rick', 'John'] positions = [index for index,name in enumerate(my_list) if name=='Steve'] index = positions[0] if positions else -1 print(index)
first thing how can we use else after for ...we cannot use it in c++ and 2nd else indentation is not under if so after break why didnt else print respond
Hi! I never did a for loop in python like "for i, value in enumerate(to search)". I may be sounding dumb but where can I learn more about that? Thanks!
Why don't we just write like this: def find_index(to_search, target): for i, value in enumerate(to_search): if value == target: return i else: return -1 This result is same.
+Ethan Kim That works too. I was trying to show how the control flow worked with the break statement and that the else clause is actually skipped when a break occurs. If I was to return i when the target was found then it would have exited the function at that point and I wouldn't have been able to demonstrate that the else clause wasn't triggered. So really it's just to get the concept across and not written to be the most efficient. But your way is definitely more clean. Thanks for the comment.
+Ethan Kim "break" will only break you out of the for-loop. "return" will return a value and exit whatever function you are in. So if you have things to do within the function after the for-loop, then break is what you want. I don't know if I explained that very well, but here is a link that does a better job with examples: stackoverflow.com/questions/28854988/what-is-the-difference-between-return-and-break-in-python
+Corey Schafer Thank you again, so if for loop doesn't have 'break' such as copying list to list then it doesn't need 'else', we just need to put that line at the bottom, correct? Anyway, sorry for Pirates down :)
my_list = [1, 2, 3, 4, 5, 6, 7, 9] for number in my_list: print(number) if number == 6: break else: print("This is the else statement") The output is 1 Idk why the output is 1, can anybody help me with it.
Please if you have time to help me figure out how to solve this problem. This is essentially a pointless program. Here we have a list of names where the user searches the names in the list. If the name is not in the list it works ok. However, if the name is found, we first get the information that the name was found, but then there is a problem. Namely, the else statement is also executed and if we have two same names in the list then the print statemet of the if block is executed twice and I want it executed only once. How can I handle this. Thank you very much in advance. # check the names in the list name_list = ['Dani', 'Tom', 'Mark', 'John', 'Mark'] user = input('Which name you are looking for in the list: ') # if user enter Mark for i in name_list: if user in i: print(i) else: print('There is no such word in the text') _____________________________________________ Which name you are looking for in the list: Mark The name Mark is in the list The name Mark is in the list There is no such word in the text
You just need a "break" after the "print(i)" statement, which pulls you out of the loop if the name is found (the first time it's found!) and also prevents the statement in the "else" from being executed.
Your Python videos are great. There is a lot of information on the basics already out there, but you bring down the more complex functionality to a level that is manageable/understandable and doesn't already require you to be a coding buff to utilise or see the value in. It's definitely your niche, which is why I subscribed.
Thanks a lot Corey, each time I have to prepare my self for an interview I come back to your videos!! Saludos from Argentina
This is on my PCEP tomorrow I think since I have seen many practice questions with this. I think of it as it simply outputs this when you're adding a number but the loop won't for one iteration, but your video explains it far better than that. Thank you'
I'd never even heard of an else statement on a for loop before, but having watched this video a couple of times now, I can think of several use cases I have for it. Thanks, and keep 'em (python stuff) coming.
Damn, why do i find this now, just wrote a piece of code where this would have been much more efficient, I was forced to use found = False, and then when the loop finds a match I had to do found = True, then break and then write "if found: xxx else: " , this would have been much more easier. Oh well thanks alot, I will be using it in the future, and when I plan to refactor my existing code. Thanks Alot for all the Awesome Tutorials :)
my_list = ['Corey', 'Rick', 'John']
positions = [index for index,name in enumerate(my_list) if name=='Steve']
index = positions[0] if positions else -1
print(index)
You sir will make me into a software engineer
Abdunetly thanks Corey
Thanks! I actually have a lot of applications for this!
Please, make more videos, such as "sys" module or creating some project. PLEASE!
great explaination sir..... thank you
Thank you Corey 😀👍
Well explained with examples! Had to hit that Subscribe button!
I think the video is useful. A suggestion would be to create a video for the most recent version of Python and a link.
informative sir
Good as always. Thanks.
These are amazing. I just became a Patreon member :)
Thanks!
Great video brother, good explanation.
Best explanation. Thamks
it was exactly what i needed ....thank you 😍
I love all your videos and this one too!
Nice !!
Wow you are very very good
In finding the index, problem after break-in else statement why do we need ( -1 ). I didn't understand the concept.
your explanations are so clear! thank youu
first thing how can we use else after for ...we cannot use it in c++ and 2nd
else indentation is not under if so after break why didnt else print respond
this is amazing......
That helped, Thanks!
in loops else == nobreak; does it mean it only works with blocks with break statements?
yep
Thank you so much Corey for your wonderful videos;
would you please make a video on how to customize your IDE like the one you are working on?
If you search my channel for Sublime Text then I have a video on how I set it up.
Hi!
I never did a for loop in python like "for i, value in enumerate(to search)".
I may be sounding dumb but where can I learn more about that?
Thanks!
It’s a function called enumerate. It’s a way to return the index and the element at the same time.
Else Clauses on Loops will help to get rid of flags.
Tbh the last example is impractical. Instead of braking why not just return i? I bet it would be use to clean up the loop’s potential mess
Why don't we just write like this:
def find_index(to_search, target):
for i, value in enumerate(to_search):
if value == target:
return i
else:
return -1
This result is same.
+Ethan Kim That works too. I was trying to show how the control flow worked with the break statement and that the else clause is actually skipped when a break occurs. If I was to return i when the target was found then it would have exited the function at that point and I wouldn't have been able to demonstrate that the else clause wasn't triggered.
So really it's just to get the concept across and not written to be the most efficient. But your way is definitely more clean. Thanks for the comment.
+Corey Schafer Thank you for quick response. If so, which case we SHOULD use for~else statement?
+Ethan Kim "break" will only break you out of the for-loop. "return" will return a value and exit whatever function you are in. So if you have things to do within the function after the for-loop, then break is what you want. I don't know if I explained that very well, but here is a link that does a better job with examples:
stackoverflow.com/questions/28854988/what-is-the-difference-between-return-and-break-in-python
+Corey Schafer Thank you again, so if for loop doesn't have 'break' such as copying list to list then it doesn't need 'else', we just need to put that line at the bottom, correct? Anyway, sorry for Pirates down :)
my_list = [1, 2, 3, 4, 5, 6, 7, 9]
for number in my_list:
print(number)
if number == 6:
break
else:
print("This is the else statement")
The output is 1
Idk why the output is 1, can anybody help me with it.
Top
Please if you have time to help me figure out how to solve this problem. This is essentially a pointless program. Here we have a list of names where the user searches the names in the list. If the name is not in the list it works ok. However, if the name is found, we first get the information that the name was found, but then there is a problem. Namely, the else statement is also executed and if we have two same names in the list then the print statemet of the if block is executed twice and I want it executed only once. How can I handle this. Thank you very much in advance.
# check the names in the list
name_list = ['Dani', 'Tom', 'Mark', 'John', 'Mark']
user = input('Which name you are looking for in the list: ')
# if user enter Mark
for i in name_list:
if user in i:
print(i)
else:
print('There is no such word in the text')
_____________________________________________
Which name you are looking for in the list: Mark
The name Mark is in the list
The name Mark is in the list
There is no such word in the text
You just need a "break" after the "print(i)" statement, which pulls you out of the loop if the name is found (the first time it's found!) and also prevents the statement in the "else" from being executed.