Loop Control Statements in C
Introduction to HTML
How to use the Github API
The image tag, anchor tag and the button tag
Ordered and Unordered Lists in HTML
The division tag
HTML Forms
Tables in HTML
Introduction to C Programming
Introduction to Python
Varibles and Datatypes in Python
Operators in Python
Typecasting in Python
Input and Output in Python
If Else in Python
Loops in Python
Break, Continue and Pass in Python
Python practice section 1
Lists in Python
Tuple in Python
Loops in Python
Loop means to repeat any task again and again. An example of loop is to daily wake up and go to college. In Python, we have mainly two types of loops. These are:
- while loop
- for loop
Jump to specific sections:
While loop Python
Syntax:while (condition_1): this block will execute until condition_1 is trueExample:
# variable is assigned with value 0
i = 0
# This loop will run until 'i' is less than 5
# We need to update 'i' after every iteration, otherwise
# i will be always less than 5 and the loop will become an infinite loop
while (i<5):
print("I am rapid coder")
# increase i by 1
i = i+1
Output:
I am rapid coder I am rapid coder I am rapid coder I am rapid coder I am rapid coder
For loop in Python
We use 'for' loop to iterate over any sequence(list, tuple, dictionary, etc).
Syntax:
for var in sequence: block of codeExamples:
# This is a list, we will learn about it in detail in further articles
lst = ["Rapid","coders","akshay"]
for i in lst:
# i will containe all values of lst one by one
print(i)
Output:
Rapid coders akshay
# This is a dictionary, this is used to store key-value pairs
dic = {
1 : "Rapid",
2 : "coders",
3 : "akshay",
}
for i in dic:
print(i,end=" = ")
print(dic[i])
Output:
1 = Rapid 2 = coders 3 = akshay
range() function in python
It is used to generate a sequence of numbers.
Syntax:
range(start,stop,step_size or jump size)In the range function start and step_size is optional, if we will not specify then by default start will be zero and step_size will be 1. Let us understand the range function using code:
# range() function returns range object, it can be iterated using for loop
print(range(5))
print(type(range(5)))
# We will use type casting to convert it into a list for better understanding
# This will give a sequence from 0 to 9
# here start = 0 and step_size = 1 by default
# 10 will not be included in the sequence
a = range(10)
a = list(a)
print("a = ",end="")
print(a)
# here start = 5
b = range(5,10)
b = list(b)
print("b = ",end="")
print(b)
# here start = 5, stop = 20 and step_index = 2
c = range(5,20,2)
c = list(c)
print("c = ",end="")
print(c)
# here start = 5, stop = 50 and step_index = 5
d = range(5,50,5)
d = list(d)
print("d = ",end="")
print(d)
Output:
range(0, 5) <class 'range'> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b = [5, 6, 7, 8, 9] c = [5, 7, 9, 11, 13, 15, 17, 19] d = [5, 10, 15, 20, 25, 30, 35, 40, 45]For loop using range
print("Loop 1")
for i in range(5):
print(i)
print("Loop 2")
for i in range(5,10):
print(i)
print("Loop 3")
for i in range(5,20,5):
print(i)
Output:
Loop 1 0 1 2 3 4 Loop 2 5 6 7 8 9 Loop 3 5 10 15
Else with loops
We can use an optional else with for or while loop. Code inside the else block will only execute when the loop is exhausted, if we break the loop or any exception arise then else will not be executed.
Syntax(else with for):
for var in sequence: block of code else: This code executes only when the loop exhaustsSyntax(else with while):
while (condition): block of code else: This code executes only when the loop exhaustsExample:
for i in range(5):
print(i)
else:
print("Done with Loop")
for i in range(5):
print(i)
if(i==2):
break
else:
# This will not run because the loop is not exhausted
# It is breaked using 'break' keyword
# We will learn about 'break' in further article
print("Not Done with Loop")
Output:
0 1 2 3 4 Done with Loop 0 1 2
In the same way else works with while loops, Try else with while loops yourself.