1 00:00:00,000 --> 00:00:04,840 [MUSIC] 2 00:00:04,840 --> 00:00:05,740 Hey there. 3 00:00:05,740 --> 00:00:07,690 I know that loops, both the for and 4 00:00:07,690 --> 00:00:10,640 the while varieties, can be a bit tricky to understand. 5 00:00:10,640 --> 00:00:12,890 We're used to doing things repetitively in real life, but 6 00:00:12,890 --> 00:00:16,760 sometimes translating things from real life to programming can be a bit weird. 7 00:00:16,760 --> 00:00:18,880 So let's do a bit more practice with loops so 8 00:00:18,880 --> 00:00:21,770 they will hopefully make a bit more sense. 9 00:00:21,770 --> 00:00:23,890 I have two files here in the workspace. 10 00:00:23,890 --> 00:00:27,720 The first one is named for.py and that is for practicing for loops. 11 00:00:27,720 --> 00:00:32,230 And the second one, while.py, is for practicing while loops. 12 00:00:32,230 --> 00:00:36,450 So I'm gonna show you the problems for each of them in the appropriate videos. 13 00:00:36,450 --> 00:00:40,390 You can then stop the video and write the code yourself to solve the problems. 14 00:00:40,390 --> 00:00:43,230 If you get stuck or you just wanna see how I solved my problem, 15 00:00:43,230 --> 00:00:46,390 then just watch the next video and you'll see how I did it, all right? 16 00:00:46,390 --> 00:00:48,480 Let's get started with for loops. 17 00:00:49,560 --> 00:00:52,252 For a brief refresher on for loops, 18 00:00:52,252 --> 00:00:57,471 they're loops that work their way through the items in an iterable. 19 00:00:57,471 --> 00:01:05,096 So you'd have for and then some_variable in [1, 2, 3], all right? 20 00:01:05,096 --> 00:01:06,500 Something like that. 21 00:01:06,500 --> 00:01:10,160 For each iteration of the loop, each trip through it, it will take the next item 22 00:01:10,160 --> 00:01:15,860 from the iterable, and it will put it into whatever variable name we've given. 23 00:01:15,860 --> 00:01:19,224 And then it will execute the code that's inside the loop and 24 00:01:19,224 --> 00:01:22,063 the loop will end when the last item is processed. 25 00:01:25,086 --> 00:01:29,230 Here in for.py, I have a few problems outlined in the comments. 26 00:01:29,230 --> 00:01:34,090 And at the top of the file there is a tuple of people, their birthdays, whether 27 00:01:34,090 --> 00:01:37,140 or not they like to celebrate their birthday, and how old they're going to be. 28 00:01:37,140 --> 00:01:38,918 If you're not sure what a tuple is, 29 00:01:38,918 --> 00:01:42,014 it's just like a list except you can't change its contents. 30 00:01:42,014 --> 00:01:43,733 It's actually the reason why I'm using a tuple. 31 00:01:43,733 --> 00:01:45,730 So that you don't accidentally change anything. 32 00:01:45,730 --> 00:01:47,770 One less thing for you to worry about. 33 00:01:47,770 --> 00:01:49,350 So let's look at these problems. 34 00:01:49,350 --> 00:01:53,880 For the first one, called Celebrations, I want you to loop through each person 35 00:01:53,880 --> 00:01:58,280 that's in the tuple, and if they celebrate their birthday, print out their name. 36 00:01:58,280 --> 00:02:01,230 Print out like, Happy Birthday Sam. 37 00:02:01,230 --> 00:02:05,040 Although Sam doesn't celebrate, so that wouldn't be the right one to print. 38 00:02:05,040 --> 00:02:07,822 For the second problem right here, Problem 2: Half Birthdays, 39 00:02:07,822 --> 00:02:10,060 you're going to have to do some math. 40 00:02:10,060 --> 00:02:13,930 Loop through the tuple again and for each person figure out their half birthday. 41 00:02:13,930 --> 00:02:16,070 If you're not familiar with a half birthday, 42 00:02:16,070 --> 00:02:19,580 it's the day that's exactly six months after or before your birthday. 43 00:02:19,580 --> 00:02:22,050 So it's your birthday on the other side of the year. 44 00:02:22,050 --> 00:02:25,161 A little extra reason to eat cake never hurt, right? 45 00:02:25,161 --> 00:02:30,576 For example, for Kamal here, their birthday would be October 29th, 46 00:02:30,576 --> 00:02:33,415 since they were born in April 29, or 47 00:02:33,415 --> 00:02:38,320 29/10, keeping this day slash month format. 48 00:02:38,320 --> 00:02:40,590 The third problem is similar to the first. 49 00:02:40,590 --> 00:02:45,371 Loop through the tuple again and for each person, if their birth month is between 50 00:02:45,371 --> 00:02:49,390 9 and 6, so 9 to 12 or 1 to 6, then print out their name. 51 00:02:49,390 --> 00:02:52,250 So if they have their birthday during a typical school year. 52 00:02:54,250 --> 00:02:54,890 And finally, for 53 00:02:54,890 --> 00:02:59,020 the fourth program, you can probably guess, the first step is a loop. 54 00:02:59,020 --> 00:03:02,180 Then if the person celebrates their birthday and they're ten years old or 55 00:03:02,180 --> 00:03:05,710 less, print out however many stars their age is. 56 00:03:05,710 --> 00:03:09,100 For example, if they're turning nine, you'd print out nine stars. 57 00:03:09,100 --> 00:03:11,930 Feel free to use any and all Python techniques that you're comfortable with 58 00:03:11,930 --> 00:03:13,130 for this practice session. 59 00:03:13,130 --> 00:03:17,330 The only thing you should definitely try and use for every problem is a for loop. 60 00:03:17,330 --> 00:03:21,110 You might need to turns strings into numbers, create ranges, and more though. 61 00:03:21,110 --> 00:03:22,760 I'll see you in the next video for my solution.