This article brought to you comprehensive notes on Loops in Python AI Class 9. Loops are a very important part of the programing language. So lets we begin!
Topics Covered
Comprehensive notes on Loops in Python AI Class 9
Loops are used to execute some repetitive statements in python until the condition is not satisfied. As you know, many cycles are there which are running around us based on some criteria. Similarly, in python when we have such things like this we can use python loops.
Loops are a collection of statements executed repeatedly until the condition evaluates to false. It is also known as iteration or iterative statements. It can be also considered as step by step execution of python statements until the condition evaluates to false. In the next section of Comprehensive notes on Loops in Python AI Class 9, I am going to talk about the parts of the loop.
Parts of Loop
There are three parts of Loops:
- Iterator – The starting value point
- Condition – Specify the condition
- Update Statement – Changing the value for the next step
Types of Loop
Python support two kinds of loops.
- While: A while loop is used when you have different repetitive statements with a single condition. When the condition gets false, your loop will terminate itself. While loop may have else block too quite often.
- For: The easy and popular loop is for a loop. It is very easy to write and understand because all the parts of loops are written in the same line in for loop. It will reduce the lines of codes as three parts of loops are written in a single line.
While loop
The while syntax is as follows:
iterator
while <condition>:
statement(s)
update_statement
Here in the syntax:
- iterator – Iterator means that starting point
- while condition – Here you can write the condition
- statement(s) – The statements which you are going to execute repeatedly
- Update Statement – The artithmatic statement which changes the value for the next step
Observe this code which display a series of numbers from 1 to 10.
i=1
while i<=10:
print(i)
i = i + 1
print("End of loop")
This code will print the number from 1 to 10. In the while loop, the condition will be tested first and then it will enter inside the body of the loop and execute the statement. If update statement is missing or not available there then the loop is infinite loop.
Watch this video for more understanding:
While loop video explanation in Python IDLE:
Reverse number program in Python:
Reverse Number based programs:
In the next section of Loops in Python AI Class 9, I am going to discuss about the for loop.
For Loop
The for loop is used to execute the sequence of statements executed in a series. It is the most popular loop among python developers. It is also work in a similar way as while loop is working. Observe the syntax of for loop:
for <variable> in <sequence>:
body loop
So here variable is a variable from and then you can take a sequence to execute the statements. The in operator is used to take check the value in the specified sequence. For the sequence, you can use the range function. Observe this example in which for loop is used for Loops in Python AI Class 9 :
for i in range(1,10,1):
print(i)
In the above code, the range function is used. The range() function takes three basic parameters. The first is a start, the second is a stop and the third is a step. So in the above code, the loop starts with 1, stops with 10 and taking one step in every execution. The output will be 1 to 10 numbers only.
Watch these videos for more understanding for the topic Loops in Python AI Class 9:
I hope this article helped you for understanding the basic of Loops in Python AI Class 9. Thank you for visiting my blog.
Feel free to ask your doubts in the comment section.