From 0fdac8ee4e03ad9b7b2290490c469bdce20cf0d0 Mon Sep 17 00:00:00 2001 From: Asabeneh Date: Wed, 7 Jul 2021 05:44:03 +0300 Subject: [PATCH] second version loops --- 10_Day_Loops/10_loops.md | 43 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/10_Day_Loops/10_loops.md b/10_Day_Loops/10_loops.md index f157fef..9c22696 100644 --- a/10_Day_Loops/10_loops.md +++ b/10_Day_Loops/10_loops.md @@ -9,10 +9,8 @@ Author: Asabeneh Yetayeh
- First Edition: Nov 22 - Dec 22, 2019 + Second Edition: July, 2021
- - [<< Day 9](../09_Day_Conditionals/09_conditionals.md) | [Day 11 >>](../11_Day_Functions/11_functions.md) @@ -30,12 +28,15 @@ - [For Else](#for-else) - [Pass](#pass) - [💻 Exercises: Day 10](#-exercises-day-10) + - [Exercises: Level 1](#exercises-level-1) + - [Exercises: Level 2](#exercises-level-2) + - [Exercises: Level 3](#exercises-level-3) # 📘 Day 10 ## Loops -Life is full of routines. In programming we also do lots of repetitive tasks. In order to handle repetitive task programming languages provide loops. Python programming language also provides the following types of two loops: +Life is full of routines. In programming we also do lots of repetitive tasks. In order to handle repetitive task programming languages use loops. Python programming language also provides the following types of two loops: 1. while loop 2. for loop @@ -57,6 +58,7 @@ count = 0 while count < 5: print(count) count = count + 1 +#prints from 0 to 4 ``` In the above while loop, the condition becomes false when count is 5. That is when the loop stops. @@ -109,7 +111,7 @@ while count < 5: The above while loop only prints 0, 1, 2, but when it reaches 3 it stops. -- Continue: With the continue statement we can stop the current iteration, and continue with the next: +- Continue: With the continue statement we can skip the current iteration, and continue with the next: ```py # syntax @@ -179,7 +181,7 @@ for iterator in tpl: **Example:** ```py -numbers = (0,1,2,3,4,5) +numbers = (0, 1, 2, 3, 4, 5) for number in numbers: print(number) ``` @@ -282,7 +284,7 @@ In the example above, if the number equals 3, the step *after* the condition (bu ### The Range Function -The _range()_ function is used to loop through a set of code a certain number of times. The _range(start,end, step)_ takes three parameters: starting, ending and increment. By default it starts from 0 and the increment is 1. The range sequence needs at least 1 argument (end). +The _range()_ function is used list of numbers. The _range(start, end, step)_ takes three parameters: starting, ending and increment. By default it starts from 0 and the increment is 1. The range sequence needs at least 1 argument (end). Creating sequences using range ```py @@ -299,7 +301,7 @@ print(st) # {0, 2, 4, 6, 8, 10} ```py # syntax -for iterator in range(start, end, increment): +for iterator in range(start, end, step): ``` **Example:** @@ -316,7 +318,7 @@ We can write loops inside a loop. ```py # syntax for x in y: - for t in s: + for t in x: print(t) ``` @@ -347,7 +349,7 @@ If we want to execute some message when the loop ends, we use else. ```py # syntax -for iterator in range(start, end, increment): +for iterator in range(start, end, step): do something else: print('The loop ended') @@ -373,10 +375,12 @@ for number in range(6): pass ``` -🌕 You established a big milestone, you are unstoppable. Keep going! You have just completed day 10 challenges and you are 10 steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle. +🌕 You established a big milestone, you are unstoppable. Keep going! You have just completed day 10 challenges and you are 10 steps a head in to your way to greatness. Now do some exercises for your brain and muscles. ## 💻 Exercises: Day 10 +### Exercises: Level 1 + 1. Iterate 0 to 10 using for loop, do the same using while loop. 2. Iterate 10 to 0 using for loop, do the same using while loop. 3. Write a loop that makes seven calls to print(), so we get on the output the following triangle: @@ -423,20 +427,29 @@ for number in range(6): 6. Iterate through the list, ['Python', 'Numpy','Pandas','Django', 'Flask'] using a for loop and print out the items. 7. Use for loop to iterate from 0 to 100 and print only even numbers 8. Use for loop to iterate from 0 to 100 and print only odd numbers -9. Use for loop to iterate from 0 to 100 and print the sum of all numbers. + +### Exercises: Level 2 + +1. Use for loop to iterate from 0 to 100 and print the sum of all numbers. ```sh The sum of all numbers is 5050. ``` -10. Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds. +1. Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds. ```sh The sum of all evens is 2550. And the sum of all odds is 2500. ``` -11. Go to the data folder and use the countries.py file. Loop through the countries and extract all the countries containing the word _land_. -12. This is a fruit list, ['banana', 'orange', 'mango', 'lemon'] reverse the order using loop. +### Exercises: Level 3 + +1. Go to the data folder and use the countries.py file. Loop through the countries and extract all the countries containing the word _land_. +1. This is a fruit list, ['banana', 'orange', 'mango', 'lemon'] reverse the order using loop. +2. Go to the data folder and use the [countries_data.py](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/countries-data.py) file. + 1. What are the total number of languages in the data + 2. Find the ten most spoken languages from the data + 3. Find the 10 most populated countries in the world 🎉 CONGRATULATIONS ! 🎉