Table of Contents
Loop in Python
- Loop is a command which means repeat again an again using logics.
- In python loops are two types:
- while loop
- for loop
While loop in python
- It is an entry-controlled looping statement.
- It is used to repeat a block of statements until the condition becomes True.
Syntax:
while (condition):
statement
increments/decrements
Example of while loop
Print 0 to 10 number
Program to find the number of digits of Number in python
#Method 1 Program to find the Fibonacci sequence of nth number
Print 0 to 10 number
#program to print 0 to 10 number using while loop
i=0
while i<=10:
print(i)
i=i+1
Program to find the number of digits of Number in python
#Write the program to find the number of digits of a Number in python
print("Enter the number")
x=int(input())
c=0
while x!=0:
x=x//10
c=c+1
print("The number of digits is ",c)
#Method 1 Program to find the Fibonacci sequence of nth number
#Method 1 Program to find the Fibonacci sequence of nth number
print("enter the number")
x=int(input())
i=2
a=0
b=1
print(a)
print(b)
while i<=x:
r=a+b
a=b
b=r
i=i+i
For loop in python
- For is one type of python keyword.
- A for loop is used for iterating the statements.
- For loop is used in a list, a tuple, a set, a string, and a dictionary.
Example
Use of for loop
y=[1,2,3,4,5,6,7] #This is a list
for x in y:
print(x)
Output
1
2
3
4
5
6
7
2
3
4
5
6
7