Placeholder Image

字幕表 動画を再生する

  • Hey, guys, in this video, I'm gonna give you a quick introduction to four loops and python have to download the sample fire for this video.

  • Just go to see a soldier that I owe slash Python five.

  • So suppose you have this list a ecos scar back.

  • It's banana comma, Apple and Microsoft.

  • Then what if you wanted to reiterate through each almond off this list?

  • Meaning what if you wanted to do something with each item off this lists?

  • So one way to do that, for example, if you wanted to print each almond, would be to say, print s car buckets one which select, ofcourse, the first item banana.

  • I'm print a score buckets one on print.

  • A square brackets, too.

  • 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 four loop so you can write four element in eight.

  • The lists colon four spaces, prints element on this whole block says for each element in a do the falling which is printing, printing each Armand.

  • Let's see if it works and it does.

  • It prints out Banana, Apple and Microsoft, and you can have multiple statements in this four block as well by writing print element again and note that we have four spaces in front of this line as well, just like we saw in a If block on when we run this.

  • So we see that each element is printed twice.

  • Okay, let's take a look at another example.

  • Here we have B scar buckets, 20 comma tank on my Fife to print each element off this list.

  • We can write for Element and be just like before print.

  • Come in.

  • Actually, this ward element is something we can choose, so we can use pretty much anything we want.

  • So let's write for E M B Print E.

  • On.

  • Once we run the cell, it's almond is printed.

  • And what if you wanted to find the sum off this lists?

  • One way to do that would be, too in a size.

  • A new viable, called Let's Say total.

  • Let's initialize it to zero.

  • And in this four loop, we want to add each element e to total we can do that.

  • For example, with total ecos total plus e let's get rid of this Prince Damon.

  • And so what this four block does is ill.

  • Go through each almond and be so hee will be initially 20 and then 10 on their five and then for each e, we're going to add it to total.

  • So total will be originally zero and when he is equal to 20 will have total The new value of total is equal to the old value of total which is there plus 20.

  • So total will be 20 on when Easy go to 10 the old value of total will be 20 and he 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 off this list.

  • Let's check that by printing total after the four loop on, let's run this cell and we see 35 which is expected.

  • So what if instead, you wanted to find the some off 123 on Fort.

  • One way to do this would be to create and 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 entities one comma five.

  • This means create a range of numbers starting at one through five.

  • But not including five on this method is better.

  • For example, if you wanted to find the some off one through 100 you probably don't want to write one through 100 explicitly because that would be a lot of work.

  • So, like I said, range of One through five creates a range of numbers starting from one till five, but not including five on.

  • It's sort of like a list and to see what's inside, you can actually converted to a lists with the lists function, so I just wrote least parentheses on inside.

  • The parentheses range off one comma five.

  • 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, viable see and then print.

  • What's inside.

  • See with print, See.

  • As you can see, range off 15 is sort of like the lists.

  • 123 and four and you can use this range in a four loop so you can just write four I in range one comma five Colon Let's say print I And again, this ward I is something we chose.

  • So this should print each element in this range 1 to 3 on four.

  • And it does.

  • So what if you wanted to find the some off these four numbers?

  • You can do that just like before with total to ICO sterile.

  • And that initialize is this new viable called Total 2 to 0.

  • And then we can add each eye thes items 123 and four by writing Total two equals total two.

  • Plus I on.

  • Actually, there's a shortcut for it, and that's total to plus equals I that says the new value of total two should be the sum of the old value of total two plus I.

  • And that way we'll be able to add 123 and four to total to, which is at the beginning zero.

  • So we should get 10 because one plus two plus three plus four is 10.

  • So let's check that by printing total to on we have 10.

  • Okay, let's take a look at another example.

  • Here, we're going to use range off one comma eight.

  • So that's the range of numbers starting from one to eight, but not including it.

  • So if we converted to a lists with the lists function and they once we printed, we'll see one through seven.

  • Now, out of these numbers, What if you wanted to find the some off on Lee the multiples of three.

  • So here, the only multiples of three we half our three and six.

  • So we want to add them up and find the number nine.

  • 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 viable Let's call it Total three 20 just like before.

  • And they were gonna run a four loop with four.

  • I deranged off long comma eight, and here we want to be able to say, if I is a multiple off three, so we want to check for each number I.

  • If that number is a multiple of three on, If that's true, then add I to total three with total three plus equals I and to check if something is a multiple of three, we need to learn something called a modular operator.

  • Here's an example of a modular operator.

  • Let's say Prince for percent three.

  • This says divide for where?

  • Three On this modular operator percent gives us the remainder off that division.

  • So four divided by three is of course, one with the remainder one.

  • So we should be able to print the remainder, which is one on we did.

  • What if you had a print five mod three or 5%?

  • Three.

  • This should give us too, because the remainder is too.

  • And it did.

  • What if you had one mod three.

  • This will give us one because the remainder would be one on what if you had six mod three.

  • The remainder in this case is sterile.

  • So this will give us there.

  • Okay, so to check, if I is a multiple of three, we just need to do I percent three or I'd model three.

  • If this is equal to zero, then I's a multiple of three.

  • So then, if that's the case, add I to total three with total three plus equals I and then we're done Let's print total three.

  • And just to remind ourselves range of one comma eight is the numbers one through seven on the multiples of three are three and six.

  • So as to some of those, we should get nine here on.

  • We just did.

  • Okay, here's a little task for you to practice where you just learned Can you compute the sum off 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 off one comma, 100 and you can check what it looks like with the list function on.

  • You can print this lists with the print function and you get the positive integers one through 99.

  • So what you want to do here is out up three, which is a multiple of 356 and so on.

  • You notice that there are some numbers that are both multiples off three and five, for example 15 which is a multiple of three and five.

  • 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 on.

  • I didn't come up with this name.

  • Does your gang?

  • One of my viewers did.

  • So thanks the anonymous for that.

  • And of course, to download the sample fire for this foul, you can just go to see a soldier that I owe slash Price of five on.

  • As usual.

  • I've y que from c s social on.

  • I'll see you guys in the next video.

Hey, guys, in this video, I'm gonna give you a quick introduction to four loops and python have to download the sample fire for this video.

字幕と単語

動画の操作 ここで「動画」の調整と「字幕」の表示を設定することができます

A2 初級

PythonのFor Loops入門 (Pythonチュートリアル #5) (Introduction to For Loops in Python (Python Tutorial #5))

  • 1 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語