For Loops in Python | Python for Beginners
Key Takeaways
The video demonstrates the use of For Loops in Python, covering the basics of iteration, sequences, and variables, as well as more advanced topics like nested for loops and dictionary iteration.
Full Transcript
hello everybody today we're going to be learning about for loops in python the for loop is used to iterate over a sequence which could be a list a tuple an array a string or even a dictionary here's the list that we'll be working with throughout this video and i have this little diagram right here which kind of explains how a for loop works the for loop is going to start by looking at the very first item in our sequence or our list and that's going to be our one right here it's going to ask is this the last element in our list and it is not so it's going to go down to this body of the for loop now we can have a thousand different things that can happen in the body of the for loop as we're about to look out in just a second then it's going to go up to the next element and ask is this the last element reached so it'll be no again because we'll be going to the 2 and then the 3 and then the 4 and the 5. once it reaches the 5 it'll go to the body of the for loop and then when it asks if that's the last element the answer would be yes because it's iterated through all the items within the list and then we would exit the loop and the for loop would be over now that may not have made perfect sense but let's actually start writing out the syntax of a for loop so we can understand this better to start our for loop we're going to say 4 and then we're going to give it a temporary variable for this for loop so it's a variable as it iterates through these numbers it's going to assign the variable to that number so for this one we're just going to say number because it's pretty appropriate these are all numbers and then we're going to say in integers now right here you can put just about anything this could be the list this could be a tuple this could be a string even but that is what we're going to iterate through so we're saying for the variables each of these numbers within this list of integers and then we're going to write a colon this is the body of code that's going to actually be executed when we run through and iterate through our list so for our first example we're going to start off super simple and all we're going to do is say print open parentheses and say number as it iterates through the 1 2 3 4 and 5 number becomes our variable that is going to be printed so during that first loop r1 will be printed because that will be assigned right here then through the next iteration the two will be assigned and it'll be put right here in each loop until the very end so let's hit shift enter and as you can see it did exactly that now in this body and i'll copy and paste this down here in this body we really can do just about anything we want we don't even have to use this variable number right here we can just print yep if we wanted to and what it's going to do is for each iteration all five of those every time it loops through it's going to print off yep so let's hit shift enter and it printed it off for us so really we weren't even using the numbers within the list we're really just using it as almost a counter now let's copy this integers once again let's go right up here and let's go copy this for loop that we wrote now we do not have to call this number this can be anything you want any variable name that you'd like to name it we could call it jelly and we can do [Music] jelly plus jelly i think you're getting the picture right when it loops through that one it's doing one plus one and when it loops through the two it's doing two plus two that is basically how a for loop works now for a dictionary it's going to handle it a little bit differently so let's create a dictionary really quickly so we'll say ice cream dictionary is equal to we're gonna do a squiggly brackets so we're gonna say name and we're gonna say colon we need to assign our value for that item so i'm going to say alex freeberg we'll do our next one separated by a comma and we'll say weekly intake and i'll say 5 scoops per week the next one we will do is favorite ice creams and for this one we're doing something a little bit different for this we're going to have a list within this dictionary so we'll say within our list of my favorite ice creams we'll say mint chocolate chip and i'll just do mcc for that and we'll separate that out by a comma and we'll say chocolate so now we have this dictionary ice cream and within it we have my name my weekly intake and my favorite ice creams with a list in there as well let's hit shift enter and now we're gonna start writing our for loop now the for loop is gonna look very similar but to call a dictionary it's just a little bit different so we're gonna say for the cream in ice underscore cream underscore dictionary dot values and then we're gonna do parentheses and then a colon now we're gonna print the cream so in order to indicate what we actually want to pull we have to specify within the dictionary what we want are we pulling the item are we pulling the value we need to specify this so that's why we have this dot values right here so let's run this and see what we get so as you can see we are pulling in the values right here that's why we're pulling in alex freeburg five and mint chocolate chip slash chocolate now we are able to call both of those both the key and the value so let's go right down here and we can do both the key and the value so we can pull two things at one time and we're going to do this by saying dot items so we could also do dot key if we just wanted to do a key but we want to do items when we do both of them so we're going to go right down here and say four key and value in ice cream dictionary dot items print and let's write key and then we'll do a comma and then let's give it a little arrow or something like that uh something like this and then we'll do a comma and we'll say value and let's print this off and see what we get so it's looping through and for each key and value it's saying here is the key so that's the name then we have weekly intake then we have favorite ice creams it's giving us a little arrow and then we're also printing off the value so we have name alex freeberg weekly intake five favorite ice creams mint chocolate chip and chocolate so now let's talk about nested for loops we've looked at for loops we understand how they work and why they do what they do but what about a nested for loop a for loop within a for loop for this example let's create two separate lists let's create flavors and let's make that a list by making it a bracket and we'll do vanilla the classic chocolate and then cookie dough all great flavors so that's our first list and then we're going to say toppings and we'll do a bracket for that as well and we'll say hot fudge and then we'll do oreos and then we'll do marshmallows say you spell marshmallows i think it's an e that looks wrong i might be spelling it wrong but that's okay so let's save this by clicking shift enter and now we have our flavors and our toppings so now let's write our first four loops we're going to say 4 1 as in our number 1 for loop we're going to say in flavors and we'll do a colon we'll click enter now we can write our second for loop so we're going to say 4 2 in toppings and then we'll do a colon and enter and then we're going to say print and we'll do an open parentheses and then we're going to say 1 so we're printing the 1 in flavors and then we'll say 1 comma i'm going to say topped with comma two so what this is essentially going to do is we're gonna say four one we're gonna take the very first one in flavors and then we're gonna loop through all of two as well so we're gonna loop through hot fudge oreos and marshmallows and once we print that off then we will loop all the way back to flavors and look at the next iteration or the next sequence within the first for loop so let's run this really quickly and see what we get so as you can see it goes vanilla vanilla vanilla and vanilla is topped with the hot fudge the oreos and the marshmallows and then we start iterating through our second one in our first for loop so there's that hierarchy so we're iterating completely through this one before we actually go to the very first for loop and start iterating through that one again now that is essentially how a nested for loop works these nested folds can get very complicated in fact for loops in general can get very complicated the more you add to it and the more you're wanting to do with it but that is basically how a for loop and a nested for loop works thank you guys so much for watching be sure to like and subscribe below and i'll see in the next video [Music] you
Original Description
Take my Full Python Course Here: https://bit.ly/48O581R
In this series we will be walking through everything you need to know to get started in Python! In this video we learn about For Loops in Python.
Code in GitHub: https://github.com/AlexTheAnalyst/PythonYouTubeSeries/blob/main/Python%20Basics%20101%20-%20For%20Loops%20%2B%20Nested%20For%20Loops.ipynb
Favorite Python Courses:
📖Python for Everybody Specialization - https://imp.i384100.net/jWdkA0
📖Data Analysis with Python - https://coursera.pxf.io/BXY3Wy
____________________________________________
SUBSCRIBE!
Do you want to become a Data Analyst? That's what this channel is all about! My goal is to help you learn everything you need in order to start your career or even switch your career into Data Analytics. Be sure to subscribe to not miss out on any content!
____________________________________________
RESOURCES:
Coursera Courses:
📖Google Data Analyst Certification: https://coursera.pxf.io/5bBd62
📖Data Analysis with Python - https://coursera.pxf.io/BXY3Wy
📖IBM Data Analysis Specialization - https://coursera.pxf.io/AoYOdR
📖Tableau Data Visualization - https://coursera.pxf.io/MXYqaN
Udemy Courses:
📖Python for Data Analysis and Visualization- https://bit.ly/3hhX4LX
📖Statistics for Data Science - https://bit.ly/37jqDbq
📖SQL for Data Analysts (SSMS) - https://bit.ly/3fkqEij
📖Tableau A-Z - http://bit.ly/385lYvN
*Please note I may earn a small commission for any purchase through these links - Thanks for supporting the channel!*
____________________________________________
SUPPORT MY CHANNEL - PATREON/MERCH
🙌Patreon Page - https://www.patreon.com/AlexTheAnalyst
💻Alex The Analyst Shop - https://teespring.com/stores/alex-the-analyst-shop
____________________________________________
Websites:
💻Website: AlexTheAnalyst.com
💾GitHub: https://github.com/AlexTheAnalyst
📱Instagram: @Alex_The_Analyst
____________________________________________
0:00 Intro
1:09 Syntax of For Loop
3:03 Variable Nam
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Alex The Analyst · Alex The Analyst · 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
Top 3 Data Analyst Skills in 2020
Alex The Analyst
Truth About Big Companies | Told by a Fortune 500 Data Analyst
Alex The Analyst
Data Analyst Salary | 100k with No Experience
Alex The Analyst
Working at a Big Company Vs Small Company | Told by a Fortune 500 Data Analyst
Alex The Analyst
Data Analyst Resume | Reviewing My Resume! | Fortune 500 Data Analyst
Alex The Analyst
Data Analyst Resume | Complete Guide To Creating A Data Analyst Resume | Tips + Templates + Examples
Alex The Analyst
Switching Careers to Become a Data Analyst | How I Made the Switch
Alex The Analyst
Working With a Recruiter to Land Your First Job as a Data Analyst | LinkedIn Recruiters
Alex The Analyst
Data Analyst Salary in 2020
Alex The Analyst
Data Analyst Resume | Reviewing YOUR Data Analyst Resumes!
Alex The Analyst
Data Analyst Fact Check | 84k Average Starting Salary?? | The Career Force 2020 Data Analyst Salary
Alex The Analyst
SQL Basics Tutorial For Beginners | Installing SQL Server Management Studio and Create Tables | 1/4
Alex The Analyst
SQL Basics Tutorial For Beginners | Select + From Statements | 2/4
Alex The Analyst
SQL Basics Tutorial For Beginners | Where Statement | 3/4
Alex The Analyst
SQL Basics Tutorial For Beginners | Group By + Order By Statements | 4/4
Alex The Analyst
Day in the Life of a Data Analyst | Fortune 500 Edition
Alex The Analyst
Intermediate SQL Tutorial | Inner/Outer Joins | Use Cases
Alex The Analyst
Intermediate SQL Tutorial | Unions | Union Operator
Alex The Analyst
Intermediate SQL Tutorial | Case Statement | Use Cases
Alex The Analyst
Intermediate SQL Tutorial | Having Clause
Alex The Analyst
Intermediate SQL Tutorial | Updating/Deleting Data
Alex The Analyst
Day in the Life of a Data Analyst | Fortune 500 Edition (During Quarantine)
Alex The Analyst
Data Analyst Interview Questions | Phone + In-Person Interview Questions
Alex The Analyst
SQL Interview Questions and Answers for Beginners | Data Analyst Interview Questions
Alex The Analyst
Data Analyst Interview Questions | What To Say vs What NOT To Say
Alex The Analyst
Data Analyst Interviews | Salary Negotiation
Alex The Analyst
Data Analyst Q&A LIVE
Alex The Analyst
Intermediate SQL Tutorial | Aliasing
Alex The Analyst
Data Scientist vs Data Analyst | Which Is Right For You?
Alex The Analyst
Best Online Courses for Data Analysts
Alex The Analyst
Best Free Online Courses for Data Analysts
Alex The Analyst
Data Analyst vs Business Analyst | Which Is Right For You?
Alex The Analyst
Scraping Data Off Twitter Using Python | Twitterscraper + NLP + Data Visualization
Alex The Analyst
Data Analyst Question and Answer | Answering Your YouTube Questions
Alex The Analyst
What Does a Data Analyst Actually Do?
Alex The Analyst
Data Analyst Bootcamps | Are They Worth It?
Alex The Analyst
Top 5 Reasons Not to Become a Data Analyst
Alex The Analyst
Data Analyst Career Path | How to Become a Data Analyst + What to Do Next
Alex The Analyst
Live Data Analyst Q&A #3
Alex The Analyst
Top 5 Reasons Not to Lie on Your Resume
Alex The Analyst
The Hiring Process from an Interviewer's Perspective | Alex The Analyst Show | Episode 1
Alex The Analyst
Top 5 Reasons Data Analytics is a Good Career Choice
Alex The Analyst
How I Changed Careers to Become a Data Analyst | Alex The Analyst Show | Episode 2
Alex The Analyst
Top 5 Reasons You'll Be a Good Data Analyst
Alex The Analyst
Self Taught vs Boot Camp vs Degree | Alex The Analyst Show | Episode 3
Alex The Analyst
Covid and the Data Analyst Job Market | Alex The Analyst Show | Episode 4
Alex The Analyst
Data Analyst Expectations vs Reality
Alex The Analyst
Imposter Syndrome in Tech | Alex The Analyst Show | Episode 5
Alex The Analyst
Top 10 Coursera Courses for Data Analysts
Alex The Analyst
Working at a Startup vs Fortune 500 Company | Alex The Analyst Show | Episode 6
Alex The Analyst
Data Analyst Certifications | Are They Worth It? | Alex The Analyst Show | Episode 7
Alex The Analyst
Top 10 Udemy Courses for Data Analysts
Alex The Analyst
Asking My Wife Your Questions About Me | Alex The Analyst Show | Episode 8
Alex The Analyst
Data Analyst Q&A LIVE #4
Alex The Analyst
Data Analyst Skills Path | What Skills You NEED to Know
Alex The Analyst
What is Analytics Consulting? With John Ariansen | Alex The Analyst Show | Episode 9
Alex The Analyst
Solving LeetCode SQL Interview Questions | Part 1/3
Alex The Analyst
What is No Code Analytics? | Alex The Analyst Show | Episode 10
Alex The Analyst
Top 3 Tips on Using LinkedIn to Land a Job
Alex The Analyst
Completely Unrealistic Jobs on LinkedIn | Alex The Analyst Show | Episode 11
Alex The Analyst
More on: ML Maths Basics
View skill →Related Reads
📰
📰
📰
📰
One Decision Can Change Your Career: Why Thousands of Students Are Choosing Browsejobs to Learn…
Medium · Data Science
Why Do US Stock Minute Bar Backtests Fail to Match Live Trading Results?
Dev.to · James Tao
Simulating Trade Outcomes with Parkinson Volatility
Medium · Data Science
Automating 1099-NEC Generation for Freelance Bookkeepers: The Data Hygiene Framework
Dev.to AI
Chapters (3)
Intro
1:09
Syntax of For Loop
3:03
Variable Nam
🎓
Tutor Explanation
DeepCamp AI