Skip to main content

- For loop in Python -

Loops:

Loops are control structures used to repeat a given section of code 

● a certain number of times or 

● until a particular condition is met. 

How do we keep ourselves fit nowadays? We must be doing some exercises ..right?  

The steps followed in doing the exercise are repeated many times ..isn’t it? Depending on the fitness level of the person, he can choose to repeat a step 10,20, or even 100 times. Now suppose, you want to write a program to simulate exercise steps. Is it feasible to write the same steps again and again? Also, how many times does it has to be repeated changes from person to person. Looping structures come to our rescue in such scenarios. Duplicating lines can be eliminated by repeating them a required number of times.

for’ loop

‘for loops’ are used when you have a block of code that you want to repeat a fixed number of times. A series of numbers within a given range.



 Note : 
● A range has 3 parameters -start, end, step. Here start and step is optional. 
● If the start is not specified, by default it starts from 0. 
● The end value is not included in the series.
● Start can be greater than stop. But in such cases, the step will be a negative number.
Let us see, how the for loop works?

For 1st iteration value of i is taken as 0, 2nd iteration value of i is 1, and so on.

Let's start writing some programs using for loop.  We all like going on outings with our family, Imagine you are going on a long drive with your family.

A program to play the first 10 songs in the playlist on a long drive. 





After some time, you get bored of listening to the songs from the same band. So you decided to change the sequence. 
Let us Modify the above program to play alternate songs starting from 1 till song no 20.


But song 11 is the least favorite of your brother. He stops playing the songs when song 11 started. 
Let us Modify the code to include this condition.













Output:




The car has stopped since the signal is red. Countdown from 10sec has started. 
 Program to simulate this countdown.



























Once the signal turned green, the car started accelerating at a steady speed from 0km/pr to 70 km/hr with a speed increase of 10km/hr in between. Simulate this




















Program to play the game of BINGO. The rules of the game are to count to 30. But instead of multiples of 3, you both should be saying BINGO. Simulation of this game: 






















  




Program to simulate the structure using for loop. 

Comments

Popular posts from this blog

- Object Oriented Programming -

In OOP, we can represent an entity using a class.  A class bundles data (features/properties of entity - color, height, width) and functionality (movement, expression) together.  An instance of a class is known as an object.  Which is the only cartoon series that had predicted that Trump would be elected as the president of the USA and also predicted the coronavirus spread?   Answer: Simpsons.  Simpsons is a very popular animated show. You can build something similar with python.  But have you ever wondered, how Simpsons look the same in all the series? Only his movement looking and, expression change but his main features like color, height, and face look exactly the same all the time.  This is because we create a ‘blueprint’ of Simpsons and whenever we have to simulate him on screen we use this blueprint. This keeps a record of all the features of Simpsons and recreates it whenever we want.  This is where Object-Oriented Programming(OOP) h...

-List Slicing in Python-

 List Slicing : Slicing helps in creating a new list from an existing list.  Syntax list_name [start :end:step] Suppose you have a list of Avengers, arranged in the order of your preference.   avengers=[“Iron Man”,”Thor”,”Hulk”,”Spider Man”,”Captain America”,”Black Panther”,”Hawk Eye”,”Vision”,”Black Widow”] Later you decide to split them into a separate list like  favourite=[“Iron Man”,”Thor”,”Hulk”]                                                   less_favourite =[”Spider Man”,”Captain America”,”Black Panther”]                             least_favourite=[”Hawk Eye”,”Vision”,”Black Widow”]  Now, we have 3 lists formed from the main list. Would you want to create these lists again from the beginning? This is where list slicing helps us. Here is the code to  G...

- If structure in Python -

Decision Control Structures: The control structures help in making decisions when a program is being executed.  if Statement  : Depending on certain conditions, few statements are executed.  if-else Statement : D epending on a condition;( if true or false), statements are executed.  Elif : It’s a combination of else followed by if.  We all have made many decisions in life. (Here, decisions do not mean a big decision.)Most of our decisions depend on different conditions..isn’t it? some situations in which you had to make a decision?  ● What to order in a restaurant?  ● What to do in my free time?  ● Which movie to watch?  ● Which subject to take an elective?  ● During exams, which question to attempt first? Depending on where you are going, the dress you select to wear will also change ..isn’t it? So let's try an activity. Select the dress, according to a given condition. 1. If going to a party  2. If going out for playing  3. I...