Skip to main content

Loops

Range function​

Range () :

The range function is used to generate a sequence of numbers within a specified range.

Syntax :

range(start, stop, step)

Start :

The start parameter specifies the starting value of the sequence. By default is 0.

Stop :

The stop parameter specifices the ending value of the sequence.

Step :

The step parameter define the incrementation or decreamentation. By default number is 1.

Example 1 :

Example 1
MyNumber=range(10)
print(MyNumber)

Output :

range(0, 10)

Example Explanation :

In the above example, we can create one variable. The name of that variable is MyNumber. Then we can create a range() function and pass the parameter and the value of that parameter is 10. In the last line, we can print the value of the variable.

Example 2 :

Example 2
MyNumber=range(2,10)
print(MyNumber)

Output:

range(2, 10)

Example Explanation :

In the above example, we can create one variable. The name of that variable is MyNumber. Then we can create a range() function and pass two parameters and the value of that parameters are 2 & 10. In the last line, we can print the value of the variable.

Example 3 :

Example 3
MyNumber=range(10,20,2)
print(MyNumber)

Output :

range(10, 20, 2)

Example Explanation :

In the above example, we can create one variable. The name of that variable is MyNumber. Then we can create a range() function and pass three parameters and the value of that parameters are 10, 20 & 2. 10 is the start value, 20 is the stop value and 2 is the step value means increment or decrement value. In the last line, we can print the value of the variable.

Loops -​

In loops you can repeate n number of line code again and again.

  1. for loop
  2. while loop

For Loop​

For loops are used when you have a block of code which you want to repeat a fixed number of times.

Syntax :

for var_name in range(10)

Example :

Example 1
print("Hello Student")
print("Hello Student")
print("Hello Student")
print("Hello Student")
print("Hello Student")
print("Hello Student")
print("Hello Student")
print("Hello Student")
print("Hello Student")
print("Hello Student")

Output :

Hello Student
Hello Student
Hello Student
Hello Student
Hello Student
Hello Student
Hello Student
Hello Student
Hello Student
Hello Student

Example :

Example 2
for i in range(5):
print("Hello World !!")

Output :

Hello World !!
Hello World !!
Hello World !!
Hello World !!
Hello World !!

Example Explanation :

In the above example, In 1st line for is a keyword. i is a variable and in the range() function we can pass value 5. The for statement initiates a loop that will repeat for a specified number of times, in this case, five times. The range(5) function generates a sequence of numbers from 0 to 4, which will be used by the for loop to control how many times the loop is executed. The indented block of code following the for statement will be executed once for each value generated by the range(5) function. In this case, the block of code contains a single "print" statement that will output the message Hello World !!.

Example :

Example 3
for i in range(4):
print(i," Hello Student")

Output :

0 Hello Student
1 Hello Student
2 Hello Student
3 Hello Student

Example Explanation :

In the above example, In 1st line for is a keyword. i is a variable and in the range() function we can pass value 4. The for statement initiates a loop that will repeat for a specified number of times, in this case, five times. The range(4) function generates a sequence of numbers from 0 to 3, which will be used by the for loop to control how many times the loop is executed. The indented block of code following the for statement will be executed once for each value generated by the range(4) function. In this case, the block of code contains a single print statement that will output the message Hello Student.

Example :

Example 4
n = int(input("How many time do you print :"))
for i in range(n):
print(i," Hello world")

Output :

How many time do you print : 5
0 Hello world
1 Hello world
2 Hello world
3 Hello world
4 Hello world

Example Explanation :

In the above example, In 1st line, we can create a variable. The name of that variable is n and we can take input from the user. The for statement initiates a loop that will repeat for a specified number of times, in this case, n times. The range(n) function generates a sequence of numbers from 0 to n, which will be used by the for loop to control how many times the loop is executed. The indented block of code following the for statement will be executed once for each value generated by the range(n) function. In this case, the code block contains a single "print" statement to output the message Hello world message.

Example :

Example 5
for i in range(5,7):
print("This is task-1")
print("This is task-2")

Output :

This is task-1
This is task-2
This is task-1
This is task-2

Example :

Example 6
for i in range(5, 55, 5):
print(i)

Output :

5
10
15
20
25
30
35
40
45
50

Example Explanation :

In the above example, the range() function generates a sequence of numbers from the starting point 5 up to, but not including, the ending point 55, incrementing by the step value 5 on each iteration of the loop. So, in each iteration of the loop, the value of i will be incremented by 5, and the loop will print the value of i. The loop will run 10 times, printing the values 5, 10, 15, 20, 25, 30, 35, 40, 45, and 50.

Example :

Example 7
n = int(input("Enter n :"))
for i in range(0,n,2):
print(i)

Output :

Enter n : 10
0
2
4
6
8

Example Explanation :

In the above example, we can take a input from user. The for statement initiates a loop that will repeat for a specified number of times, in this case, n number of times. The range(0, n, 2) function generates a sequence of numbers from 0 to n, and increse the value from 2 which will be used by the for loop to control n number of value times the loop is executed.

Example :

Example 8
for i in range(5):
print(i, "copying adhar card")

Output :

0 copying adhar card
1 copying adhar card
2 copying adhar card
3 copying adhar card
4 copying adhar card

Example Explanation:

In the above example, the for statement initiates a loop that will repeat for a specified number of times, in this case, five times. The range(5) function generates a sequence of numbers from 0 to 4, which will be used by the for loop to control how many times the loop is executed. The indented block of code following the for statement will be executed once for each value generated by the range(5) function. In this case, the block of code contains a single "print" statement that will output the message i copying adhar card.

Example :

example 9
num = int(input("Enter n :"))
for i in range(num):
if i%2==0:
print(i)

Output :

Enter n :10
0
2
4
6
8

Example Explanation :

In the above example, we are taking a input from user.The for statement initiates a loop that will repeat for a specified number of times, in this case, n number of times. The range(num) function generates a sequence of numbers from 0 to num, then we are applying here if condition this condition is true then print i or even number.

Break & Continue Keyword​

Example :

example 10
for val in range(10):
if val == 2:
break
print(val)

Output :

0 1

Example Explanation :

In the above example, The for statement initiates a loop that will repeat for a specified number of times, in this case, ten times. The range(10) function generates a sequence of numbers from 0 to 9, we are applying if condition "if val == 2" then we will use break keyword for break the loop. so output is only print 0 & 1.

Example :

example 11
for val in range(10):
if val == 2:
continue
print(val)

Output :

0
1
3
4
5
6
7
8
9

Example Explanation :

In the above example, The for statement initiates a loop that will repeat for a specified number of times, in this case, ten times. The range(10) function generates a sequence of numbers from 0 to 9, we are applying if condition "if val == 2" then we will use continue keyword for skip the specific number of the loop. so output is only print 0,1,3,4,5,6,7,8,9.

else keyword in python​

Example :

example 12
for i in range(10):
print(i)
else:
print("Loop executed successfully!!")

Output :

0
1
2
3
4
5
6
7
8
9
Loop executed successfully!!

Example Explanation :

In the above example, The for statement initiates a loop that will repeat for a specified number of times, in this case, ten times. The range(10) function generates a sequence of numbers from 0 to 9.then print the value of i so output is print 0 to 9 and also print else statement that is Loop executed successfully!!.

Example :

example 13
for i in range(5):
if i == 2:
break
print(i)
else:
print("Loop executed successfully!!")

Output:

0
1

Example :

In the above example, The for statement initiates a loop that will repeat for a specified number of times, in this case, five times. The range(5) function generates a sequence of numbers from 0 to 4, we are applying if else condition "if val == 2" then we will use break keyword for break the loop. so output is only print 0 & 1.

Example :

example 14
for i in range(5):
if i == 2:
continue
print(i)
else:
print("Loop executed successfully!!")

Output :

0
1
3
4
Loop executed successfully!!

Example :

In the above example, The for statement initiates a loop that will repeat for a specified number of times, in this case, five times.

The range(5) function generates a sequence of numbers from 0 to 4, we are applying if else condition then check the condition "if val == 2" then we will use continue keyword for skip the specific number of the loop. then print the i number of values and then go to the else part and print Loop executed successfully!!.

Example :

example 15
number  = int(input("Please enter any Number :"))
fact = 1
for i in range(1, number+1):
fact = fact * i
print(fact)

Output :

Please enter any Number :5
120

Example Explanation :

In the above example, we can take a input from user and stored in the number variable in second line we are created fact = 1.

The for statement initiates a loop that will repeat for a specified number of times, in this case, n number of times we are passesd in the range starting from 1 & stop point number + 1. Again we are stored in the fact variable fact * i and print fact.

Example :

example 16
students = ["Dhanashri", "Sahil","Rahul","Chaitali"]
for student in students:
print("Sending mail to..",student)

Output :

Sending mail to.. Dhanashri
Sending mail to.. Sahil
Sending mail to.. Rahul
Sending mail to.. Chaital

Example Explanation :

In the above example, we can create one variable. The name of that variable is the students list and stored multiple data in their list, here apply for loop in this loop create another variable student and pass the students list then finally we can print the student name. So, output print for each student name of the students list.

Example :

example 17
marks  = [90, 80, 70, 56, 78]
sum = 0
for mark in marks:
sum = sum + mark
print(sum)

Output :

374

Example Explanation :

In the above example, we declare one mark variable and stored different marks. create a sum variable and its value is 0, here apply for loop in this loop create one variable mark and pass the marks list.

Again create the sum variable and stored it in the sum variable in addition to the previous sum variable and marks. In the last, we print sum.

Example :

example 18
tuple1 = (10, 20, 30, 40, 50)
for tup in tuple1:
print(tup)

Output:

10
20
30
40
50

Example Explanation :

In the above example, we are creating one tuple that is tuple1 storing different multiple data in that. Here apply for loop in this loop create a tup variable and pass the tuple1 tuple then print this tup.

Exapmle :

example 19
courses = {
"c": 499,
"c++": 499,
"python": 499,
"icp":999,
"dsa": 999
}
for i in courses.keys():
print(i)

Output:

c
c++
python
icp
dsa

Example Explanation :

In the above example, create one dictionary that is courses here we are using for loop in this loop create the i variable and pass courses. keys. then print the i variable so, we get all the keys of the courses dictionary.

Example :

example 20
courses = {
"c": 499,
"c++": 499,
"python": 499,
"icp":999,
"dsa": 999
}

for i in courses.values():
print(i)

Output :

499
499
499
999
999

Example Explanation :

In the above example, create one dictionary that is courses here we are using for loop in this loop create i variable and pass courses.values. Then print the i variable so, we get all values of the courses dictionary.

Example :

example 21
progress = {
"vedika": 90,
"Aniket": 91,
"pranali": 93,
"Prajakta": 94
}

for k,v in progress.items():
print(k,"scored",v,"marks")

Output :

vedika scored 90 marks
Aniket scored 91 marks
pranali scored 93 marks
Prajakta scored 94 marks

While loop​

While loop is used to repeat a specific block of code number of times. While loop is called an Entry Control Loop. The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.

Syntax :

syntax.py
while test_expression:
Body of while

Example :

example1.py
i = 0
while i<5:
print(i)
i = i+1

Output :

0
1
2
3
4

Example Explanation :

In the above example, we declare one variable that's value is 0. here, apply the while loop if the while loop condition will be true then its enter in loop and print i increase by 1.

Example :

Write a program to Count the Number of Digits Present In a Number.

example2.py
num = int(input("Enter Any Number :"))
count = 0
while num>0:
num = num // 10
count = count + 1
print("Total Digits: ",count)

Output :

Enter Any Number :63465346
Total Digits: 8

Example Explanation :

In the above example, we are created one variable that is num in that variable taking input from a user, declare the count variable and its value 0 use here while loop in that num>0 condition if this condition will be true then enter in the loop. num variable store num // 10 value // is floor by 10, we are increasing count value by 1 then print this count so, in last, we get total digit for the corresponding number.

Example :

Write a program to reversed the Present Number.

example3.py
num = int(input("Enter Any Number :"))
rev = 0
while num>0:
rem = num % 10
rev = (rev*10)+rem
num = num // 10
print("Reversed Number : ",rev)

Output :

Enter Any Number :483675
Reversed Number : 576384

Example Explanation :

In the above example, we can created one variable. The name of that variable is num in this variable taking input from a user, declare the rev variable and its value 0 use here while loop in that num>0 condition if this condition will be true then enter in the loop. in the 4th line, we are declared rem variable is assigned num % 10, in the 5th line we declare the rev variable is assigned rev multiply by 10 and add rem to that rev value .and in 6th line we are declared num variable again and its assign num // 10 so, in last we get reversed number.