Loops in Python
Here we wil learn about loops in python and their syntax.
- Loops in Python
- While Loops
- Infinite Loops
- For Loops
- Break and Continue Keyword
- Step argument
- For loop and string
Loops in Python
Looping means repeating something over and over until a particular condition is satisfied. Here we will learn about following topics under loops in python:
- While Loops
- Sum 1 to 10
- Adding number
- Infinite Loops
- For Loops
- Sum 1 to 10
- Break and Continue Keyword
- Break
- Continue
- Step argument
- For loop and string
i = 1
while i <= 10:
print("hello anisha")
i=i+1
- Sum 1 to 10
In this program we will sum number from 1 to 10 using while loop.
total = 0
i = 1
while i <= 10:
total = total + i
i = i + 1
print(total)
- Adding number
In this program we will add a number
num = input("enter a number ")
total = 0
i = 0
while i < len(num):
total = total + int(num[i])
i = i+1
print(total)
Infinite Loops
A continuous repetitive conditional loop known as a "infinite loop" in Python continues to run until an outside factor intervenes in the execution flow, such as a lack of CPU memory, a failed feature or error code that terminated the execution, or a new feature in one of the legacy systems that requires code integration.
i = 0
while i <= 10:
print("error")
# to stop the infinite loops we have to press ctrl + c
for i in range (10):
print("hello")
- Sum 1 to 10
In this program we will sum number from 1 to 10 using for loop.
total = 0
for i in range(1,11):
total = total + i
print(total)
- Break
for i in range (1,11):
if i == 5:
break
print(i)
- Continue
for i in range (1,11):
if i == 5:
continue
print(i)
for i in range(1,11,2):
print(i)
name = " anisha"
for i in range(len(name)):
print(name[i])