Welcome back my fellow noob programmer!
In this chapter of Ultimate Python, I will be talking about loops and lists.
What is a loop?
When a task is repeated until a certain condition is matched, it is called a loop. There are two types to loops in python For and Loop.
We provide a condition to the while loop, the task contained in the loop will keep running as long as the specified condition is satisfied.
Lets write a program which writes birthday diary of guy who dies at the age of 23:
age = 10 year = 1985 while age < 23: print 'Its the year %d and I am %d years old.' % (year, age) age = age + 1 year = year + 1 else: print 'Author died at the age of %d' % age
In the first two lines, I declared the variables age and year.
Then I started a while loop, which has a condition that age must be lower than 23. If the age is lower than 23, code contained in the while is executed.
At present, value of the age variable is 10 so the code the while loop will work.
It will print Its the year 1985 and I am 10 years old. after that, it increases the value of the variables age and year by one. Which means now age = 11 and year = 1986. As the value of age is still less than 23, while loop gets executed again and the value of age gets increased by one again. The loop keeps executing till the value of age is less than 23 and once the while loop is closed, our else statement comes into play.
Take a look at the output of this program:
Its the year 1985 and I am 10 years old. Its the year 1986 and I am 11 years old. Its the year 1987 and I am 12 years old. Its the year 1988 and I am 13 years old. Its the year 1989 and I am 14 years old. Its the year 1990 and I am 15 years old. Its the year 1991 and I am 16 years old. Its the year 1992 and I am 17 years old. Its the year 1993 and I am 18 years old. Its the year 1994 and I am 19 years old. Its the year 1995 and I am 20 years old. Its the year 1996 and I am 21 years old. Its the year 1997 and I am 22 years old. Author died at the age of 23
Thats all what while loop does. Provide it a condition and it will keep running as long as the condition is satisfied. Thats pretty much it!
For Loop and Lists in Python
For loop is the man! Its the real deal! For loop is the loop you will be using most of the times. But before we go any further, lets see what a list is.
A list is a collection of integers or strings. Elements of a list can be removed, changed, used easily. You can add stuff to a list on the go. So basically its a mini database.
Take a look this code which uses a list:
fruits = ['mango','apple','banana','cherry','orange'] print 'First fruit is ' + fruits print 'Third fruit is ' + fruits
First fruit is mango Third fruit is banana
Lets break down the code
- fruits is the name of our list. A list is always enclosed within [ ] and its elements are separated by , (commas). Our elements are strings so we enclosed them within ‘ ‘ (single quotes) but in case you are using integers feel free to write them with single quotes.
- We are printing strings in the second and third line. fruits means that we want the first element from a list named fruits. You must be thinking if we want the first element then it should be fruits not fruits. Well this is how things work here, you have to count from 0. Similarly fruits fetches the third element from the list named fruits.
Lists can do more than that when combined with For loop. So lets do more!
For loops are basically used to perform the same task for more than 1 arguments. Confused? Take a look at this code
fruits = ['mango','apple','banana','cherry','orange'] for name in fruits: print 'Fruit name: ' + name
Fruit name: mango Fruit name: apple Fruit name: banana Fruit name: cherry Fruit name: orange
Lets break down the code:
- fruits is a list
- for loop picks the first element from the list and assigns its value to a variable named name. After that it prints the variable name whose value is the first element of the list. After printing it, for loop picks the next element from the fruits list and does the same printing thing again. It keeps going until it reaches the end of the list. This process is called iteration.
Which loop is better?
We can’t compare them as they serve different purposes.
While loop should be used when you want to do a task for a certain number of times. Like you may use a while loop when you want to do a task 10 times.
For loop should be used when you want to do a task for different arguments. Like you may use a for loop when you have a list of websites and you want to check if they are online, one by one.
Getting a date
Lets say you are a creep. You are the guy who keeps messaging girls even they aren’t interested in you. You are the asshole who keeps commenting ‘Nice pic’ on everything a girl uploads.
So you wrote a lengthy romantic proposal and sent it to like 1000s of girls. As its nearly impossible to go through all those text manually so you wrote a program which downloaded all the replies and stored them in list.
Now all we have to do is to check all those messages for positive responses. So I wrote a program for it:
messages = ['Christi: who are you?', 'Mindy: good try but I don\'t date assholes', 'Emily: fuck off', 'Jack: lol', 'Rose: i love you too', 'Riya: awwwww alright we can hangout some time', 'Elise: i have a boyfriend', 'Stacey: Thats adorable but sorry I am committed', 'Jack: Are you gay bro?'] for text in messages: if 'fuck' in text: continue elif 'boyfriend' in text or 'friend' in text: continue elif 'sorry' in text: continue elif 'you too' in text or 'yes' in text: print 'Positive Response Found: ' + text continue elif 'hang' in text or 'coffee' in text or 'movie' in text: print 'Positive Response Found: ' + text else: pass
Lets break down the code:
- messages is a list which contains received messages or responses to your proposal.
- We started the for loop in second line which picks elements from the list one by one by assigning them a name text.
- Then we check if the word fuck is present in the text. It can be used to detect texts like fuck off or fuck off you creep! If a match is found, the continue statement gets executed which tells the program to stop processing the current element and import next element from the list.
- In the next line we are looking for the words boyfriend and friend so we can detect messages like I have a boyfriend or We are just friends. Did you notice how I used two conditions in single if statement? That saves time and space.
- Going further, if the word sorry is found in the text, continue statement is executed.
- In the next line, we are checking if the text contains words such as you too or yes so we can detected potential positive responses to our proposal. If a text contains any of these two words it gets printed.
- In the end there’s an else condition which gets executed when none of the above conditions are satisfied. Else condition executes the pass statement. Pass statement is used to do nothing, literally. So when no condition is matched, our program does nothing and for loop ends for the current element and next element gets imported.
Positive Response Found: Rose: i love you too Positive Response Found: Riya: awwwww alright we can hangout some time
Congrats! You got two dates 😉 Now please stop asking Bob and vegene pics.
Thats all for now! We will learn more in upcoming chapters of Ultimate Python. Till then keep learning! Keep coding!
Also Read: Getting A Girlfriend : The Hacker’s Way