Python For Else Statement - Python Quick Tips
Skills:
Python for Data80%
Key Takeaways
This video covers the for-else statement in Python, explaining how it works and providing examples of its usage.
Full Transcript
hello everyone and welcome back to other Python quick tips video in this video I'm going to be talking about the for else statement now essentially the way if for else works is you have a for loop and then directly underneath that in the same indentation level you have an else statement but what does this else statement do I'm going to illustrate that to basic examples here so essentially the way this else statement works is it checks if you broke out of the loop using the break keyword or if you reach the end of the loop now that's extremely useful because sometimes you exit the loop and you want to know how you exited did you break from finding some kind of statement or seeing that break keyword or did you actually go through the entire loop and you ended because there was no more elements or no more numbers to loop through so there's two way you can two ways you can end the loop by breaking or just by running out of things to look for so essentially the way the sella statement works is if you break out of the loop it will not run but if you run through the entire loop and it does not witness this break statement then it will run whatever is in this else statement so this basic example you here I have a list called my list you can see the elements that are inside of it and we have this look for which is a number that we're gonna be looking for so essentially what my for loop is doing here is looking for this number and if it finds it what it's gonna do is say found whatever the number is and then break so if it finds it is not gonna print did not find but if for some reason we loop through this entire loop we cannot find this number which is 45 then it's gonna print out did not find so let me run this here and you can see that if I scroll up a bit we get did not find 45 as 45 is not in this list now if I put 100 here then you'll see that we got found a hundred and this else statement did not run so only runs when we loop through the list and we exit it without this break keyword so essentially we finish all of the possible things we can loop for and we didn't break all right so now I'm gonna go to example two so let's just delete this for now so example two is a little bit more complicated but shows another way that you can actually use this so what this does is essentially look for duplicate elements in two lists so what we're doing is looping through list 1 which is this list here then we're looping through list 2 and then what we're gonna do is check whether or not an element exists in both list one and list two and if it does not we're gonna say it's only in list 1 otherwise we're gonna say it's in both lists so let me just run this and show you what I mean and it'll walk through it slowly so essentially what happens here is we get a bunch of print statements that says one is only in list 1 2 is in both lists and so on you can see how that works and the reason that this works is again because we have this 4 else statement and again this is indented inside of another for loop so every time that this for loop runs if we broke out we'll just print this and we won't do this else statement but if we didn't break which means we didn't find for example 1 or we didn't find 3 or 6 or whatever element in list 2 then we're gonna print X is only in list 1 which is gonna be that element that we were looping through so toe is that is healthy for else statement works pretty straightforward essentially like I've said a hundred times now if you break this else does not run if you get through the loop and you do not break out of it using the break keyword this else statement will run super useful one can definitely save you a lot of time and make you avoid having to use some like exterior lists and stuff like that that you might use before that has been quite Python quick tips for today if you guys enjoyed please make sure you leave a like and let me know what you want to see in future videos
Original Description
This python quick tips video covers the for else block. Explaining what happens when you place an else statement after a for loop.
Playlist: https://www.youtube.com/watch?v=MIqvaX_Q0c8&list=PLzMcBGfZo4-kl1reiBsCHsxQS19CyB7Ne
◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python
https://tech-with-tim.teachable.com/p...
📸 Instagram: https://www.instagram.com/tech_with_tim
🌎 Website https://techwithtim.net
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/pr2k55t
📝 LinkedIn: https://www.linkedin.com/in/tim-rusci...
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
💵 One-Time Donations: https://www.paypal.com/donate/?token=...
💰 Patreon: https://www.patreon.com/techwithtim
◾◾◾◾◾◾
⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡
Tags:
- Tech With Tim
- Python Tutorials
- Python For Else Statement
- Python for else
- For else python
- Else after for loop python
#Python #PythonQuickTips
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Python for Data
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Building LSTMs with PyTorch and Lightning AI Part 7: Resuming Training with Checkpoints
Dev.to · Rijul Rajesh
How AI Learns with Less Labeled Data
Medium · AI
Comparing Sarvam-30B and Qwen2.5–14B on Spider Text-to-SQL: An Active-Parameter Perspective
Medium · LLM
Claude Sonnet 5 closes the gap to Opus without the Opus bill
Medium · LLM
🎓
Tutor Explanation
DeepCamp AI