Introduction to For Loops in Python (Python Tutorial #5)
Key Takeaways
This video tutorial introduces for loops in Python, covering how to iterate through lists, use the range function, and perform calculations such as summing numbers and finding multiples of certain numbers. Specific tools and techniques demonstrated include using for loops with lists, the range function, and the modulo operator.
Full Transcript
hey guys in this video I'm gonna give you a quick introduction to four loops in Python and to download the sample file for this video just go to CS loader dot io / Python 5 so suppose you have this lists a equals square brackets banana comma Apple and Microsoft then what if you wanted to iterate through each element of this list meaning what if you wanted to do something with each item of this lists so one way to do that for example if you wanted to print each element would be to say print a square brackets 1 which selects of course the first item banana and print a square brackets 1 and print a square brackets 2 so this method works but it's kind of cumbersome because we need to repeat the same statement over and over again and that would be a lot of work for example if this list had 100 elements so instead we can use something called a for loop so you can write for element in a the lists : 4 spaces print element and this whole block says for each element in a do the following which is printing printing each element let's see if it works and it does it prints out banana Apple and Microsoft and you can have multiple statements in this for block as well by writing print element again and note that we have 4 spaces in front of this line as well just like we saw in an if block and when we run this so we see that each element is printed twice ok let's take a look at another example here we have B square brackets 20 comma 10 comma 5 2 print each element of this list we can write for element in B just like before print element and actually this word element is something we can choose so we can use pretty much anything we want so let's write for e in B print E and once we run the cell each almond is printed and what if you wanted to find the sum of this lists one way to do that would be to initialize a new variable called let's say total and let's initialize it to zero and in this for loop we want to add each element e to total we can do that for example with total equals total plus e let's get rid of this print statement and so what this four-block does is it'll go through each element in B so e will be initially 20 and then 10 and then 5 and then for each e we're going to add it to total so total will be originally 0 and when e is equal to 20 we'll have total the new value of total is equal to the old value of total which is their plus 20 so total will be 20 and when e is equal to 10 the old value of total will be 20 and E will be 10 so the new value will be 30 so at the end we should have total being equal to 35 which is some of these lists let's check that by printing total after the for loop and let's run this cell and we see 35 which is expected so what if instead you wanted to find the sum of 1 2 3 & 4 one way to do this would be to create a new lists with these four numbers but there's actually a better way of doing that and that is to use the range function in Python and to use it you can just write range parentheses 1 comma 5 this means create a range of numbers starting at 1 through 5 but not including 5 and this method is better for example if we wanted to find the sum of 1 to 100 you probably don't want to write 1 through 100 explicitly because that would be a lot of work so like I said range of 1 through 5 creates a range of numbers starting from 1 till 5 but not including 5 and it's sort of like a list and to see what's inside you can actually convert it to a lists with the lists function so I just wrote lists parentheses and inside the parentheses of 1 comma 5 so we're creating a new range and then putting it into the list function to convert it to a list let's put it to a new variable C and then print what's inside C with print C as you can see range of 1 5 is sort of like the lists 1 2 3 and 4 and you can use this range in a for loop so you can just write for I in range 1 comma 5 : let's say print I and again this word I is something we chose so this should print each element in this range 1 2 3 and 4 and it does so what if you wanted to find the sum of these four numbers you can do that just like before with total to equals 0 and that initializes this new variable called total 2 to 0 and then we can add each I these items 1 2 3 and 4 by writing total 2 equals total 2 plus I and actually there's a shortcut for it and that's total 2 plus equals I that says the new value of total 2 should be the sum of the old value of total 2 plus I and that way we'll be able to add 1 2 3 & 4 2 total 2 which is at the beginning 0 so we should get 10 because 1 plus 2 Plus 3 plus 4 is 10 so let's check that by printing total 2 and we have 10 okay let's take a look at another example here we're going to use range of 1 comma 8 so that's the range of numbers starting from 1 to 8 but not including 8 so if we convert it to a list with the lists function and then once we print it we'll see 1 to 7 now out of these numbers what if we wanted to find the sum of only the multiples of 3 so here the only multiples of 3 we have are 3 & 6 so we want to add them up and find the number 9 it's kind of silly in this small example but it could be useful when the range is much larger so to do that we're going to define a new variable let's call it total 3 to 0 just like before and then we're gonna run a for loop with for I in range of 1 comma 8 and here we want to be able to say if I is a multiple of 3 so we want to check for each number I if that number is a multiple of 3 and if that's true then add I to total three ways total 3 plus equals I and to check if something is a multiple of 3 we need to learn something called a modulo operator here's an example of a modulo operator let's say print 4 % 3 this says divide four-way 3 and this modulo operator % gives us the remainder of that division so 4 divided by 3 is of course 1 with the remainder 1 so we should be able to print the remainder which is 1 and we did what if you had print 5 mod 3 or 5 percent 3 this should give us 2 because the remainder is 2 and it did what if you had 1 mod 3 this will give us 1 because the remainder would be 1 and what if you had 6 mod 3 the remainder in this case is 0 so this will give us 0 ok so to check if I is a multiple of 3 we just need to do i % 3 or I mod 3 if this is equal to 0 then I is a multiple of 3 so then if that's the case add I to total 3 with total 3 plus equals I and then we're done let's print total 3 and just to remind ourselves range of 1 comma 8 is the numbers 1 through 7 and the mall 4:03 are three and six so as the some of those which you get nine here and we just did okay here's a little task for you to practice where you just learn can you compute the sum of all multiples of three and five there are less than 100 so to get all the positive integers that are less than 100 you can just do range of 1 comma 100 and you can check what it looks like with the list function and you can print this list with the print function and you get the positive integers 1 through 99 so what you want to do here is add up 3 which is a multiple of 3 5 6 and so on and you'll notice that there are some numbers that are both multiples of 3 and 5 for example 15 which is a multiple of 3 and 5 so if you're able to solve this problem just let me know in the comment below and if you're new here you should join dozer gang by subscribing to this channel and I didn't come up with this name those were gang one of my viewers did so thanks the anonymous for that and of course to download the sample file for this file you can just go to the CS dojo dot io / price of 5 and as usual I'm YK from CS dojo and I'll see you guys in the next video
Original Description
For loops Python tutorial.
This entire series in a playlist: https://goo.gl/eVauVX
Keep in touch on Facebook: https://www.facebook.com/entercsdojo
Download the sample file: https://www.csdojo.io/python5
Subscribe to my newsletter: https://www.csdojo.io/news
Support me on Patreon: https://www.patreon.com/csdojo
Playlist
Uploads from CS Dojo · CS Dojo · 33 of 60
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
▶
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
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI