For statement
The syntax of the 'for' statement is
for variable in list:
statementsLet's break down all the new words and what they mean:
- The for keyword:
This is the starting point that tells Python you want to create a loop - The variable part: This is a variable name you choose It temporarily holds each value from the list as the loop runs You can name it anything (following Python variable naming rules) Example: item, x, number, etc.
- The in keyword: It specifies that your variable will take values FROM the list/sequence Always comes between the variable and the list
- The list part: This is the sequence of items you want to loop over. Examples: [1, 2, 3], "hello", range(5)
- The colon ":": Marks the end of the for loop declaration Tells Python that the next line will contain the code to be repeated
- The statements part: These are the lines of code you want to repeat Must be indented (typically 4 spaces) Will be executed once for each item in the list
Let us practice what we learnt.
avengers=["iron man","spiderman","hulk","captain america","thor"]
for x in avengers:
print(x)In the above code, avengers is a
The loop will run
The above program will print the name of the avengers one at a time. Changes the names, remove some super heroes, add some super heores and run the program. see how the loop perfroms.
With these two new concepts lets see how we can solve the largest number problem. The English code can be
Store numbers in a list.
Let us pick the first number. Let's call it as largest.
for every number in list do the below
if number is greater than largest
Make number as largest
What steps 4 and 5 are doing is making sure that we always have the largest number in our hand. If the next number we pick is larger than the one in hand, we drop the one in our hand and keep the new number. If not, we continue to next number. Finally:
Print largest.
Now lets do the actual Python code with the correct syntax.
First step was: Store numbers in a list. Choose the correct approach in Python.
Both are correct. But here we would like the list approach as we can add as many numbers as we want without creating so many variables.
Second step was: Let us pick the first number. Let's call it as largest.
This has two statements.
- Let us pick the first card. In python we pick the first number using
- Let's call it as largest. In python we call it assignment.
We do assignment like
Third step was: For every number in list
In Python, we can write this as:
Fourth step was were: If number is greater than largest
In Python, this becomes:
if number >
Fifth step was: , make it the largest
In Python, this becomes:
largest=
Putting it all together, here's the complete Python code:
numbers=[5,1,3,8,4]
largest=numbers[0]
for number in numbers:
if number > largest:
largest=number
print(largest)Notice how close the Python program is to our general language. You just need to be careful with the indentation and the syntax.
Let's do a quick revision. In Python language we have learnt:
- Assignment
- Conditionals
- Loops
With these three constructs you can write almost any program you want. Programming is really so simple. Just these 3 statements and you can make the computer do whatever you want.
Let's see if you can solve the following problem by yourself.
Problem: Take 10 numbers in a list. Take a number you have to search in another variable. Search for the second number in the list. If the number is found, print found. If not, print not found.
Discuss with your parents or friends. Take your time. Work slowly one step at a time. This is one of the most common problem in computer science. So if you get this you have done a great job. Once you are done, let's go to the next chapter.