0% found this document useful (0 votes)
7 views4 pages

Loop and While Loop 08-01-2025

The document provides an overview of loops in Python, specifically focusing on 'for' and 'while' loops. It includes examples of how to use these loops to print messages, generate multiplication tables, calculate sums, and determine if numbers are even or odd. Additionally, it explains the structure and functionality of the 'range()' function and demonstrates both finite and infinite loops.

Uploaded by

nirajjoshi072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Loop and While Loop 08-01-2025

The document provides an overview of loops in Python, specifically focusing on 'for' and 'while' loops. It includes examples of how to use these loops to print messages, generate multiplication tables, calculate sums, and determine if numbers are even or odd. Additionally, it explains the structure and functionality of the 'range()' function and demonstrates both finite and infinite loops.

Uploaded by

nirajjoshi072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1/8/25, 10:57 AM loop and while loop 08-01-2025

In [4]: # write a program to print hello times


# 1st method
print("hello")
print("hello")
print("hello")
print("hello")
print("hello")

hello
hello
hello
hello
hello

In [7]: print("hello",end=", ")

hello,

Loops==iterate your statements


Loops are the special blocks in python that will allow the statements to be executed multiple
times based on either the counter value or the condition

Types of Loops for loop while loop for loop for loop is used to iterate over sequences such
as lists, strings, dictionaries or range() function result

for val in sequence: statement to be repeated

range() function Return a sequence of numbers

range(stop) => stop number will not be included in the sequence range(5) => 0, 1, 2, 3, 4

range(start, stop) => start = 0 (default) range(1, 5) => 1, 2, 3, 4 range(3, 8) => 3, 4, 5, 6, 7

range(start, stop, step) => step is used to specify the difference between two numbers
generated, by default step = 1 range(1, 10, 2) => 1, 3, 5, 7, 9 range(10, 100, 10) => 10, 20,
30, 40, 50, 60, 70, 80, 90

range for reverse value range(10, 0, -1) => 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

In [10]: # 2nd method to solve the problem


# for loop

for i in range (1,6): #range(start,stop)


print("hello")

hello
hello
hello
hello
hello

In [12]: for i in range (10): # range (stop)


print("hi")

localhost:8888/nbconvert/html/Desktop/NHANU/python/python jupitor/loop and while loop 08-01-2025.ipynb?download=false 1/4


1/8/25, 10:57 AM loop and while loop 08-01-2025
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi

In [25]: # write a program to give complete table of a no taken from user


a=int(input("Enter your no: "))
for i in range(1,13):
print(a,"x",i,"=",a*i)

Enter your no: 6


6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
6 x 11 = 66
6 x 12 = 72

In [22]: a=int(input("Enter your no : "))


for i in range(1,11):
print(a*i)

Enter your no : 5
5
10
15
20
25
30
35
40
45
50

In [41]: # write a program to give sum of 10 num taken from user

sum1=0
for i in range(1,6):
a=int(input("Enter your no: "))
sum1=sum1+a
print("sum after adding ",a,"is",sum1)

print(sum1,"is sum after adding",i,"numbers")

localhost:8888/nbconvert/html/Desktop/NHANU/python/python jupitor/loop and while loop 08-01-2025.ipynb?download=false 2/4


1/8/25, 10:57 AM loop and while loop 08-01-2025

---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
Input In [41], in <cell line: 4>()
3 sum1=0
4 for i in range(1,6):
----> 5 a=int(input("Enter your no: "))
6 sum1=sum1+a
7 print("sum after adding ",a,"is",sum1)

File C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py:1075, in K
ernel.raw_input(self, prompt)
1071 if not self._allow_stdin:
1072 raise StdinNotImplementedError(
1073 "raw_input was called, but this frontend does not support input re
quests."
1074 )
-> 1075 return self._input_request(
1076 str(prompt),
1077 self._parent_ident["shell"],
1078 self.get_parent("shell"),
1079 password=False,
1080 )

File C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py:1120, in K
ernel._input_request(self, prompt, ident, parent, password)
1117 break
1118 except KeyboardInterrupt:
1119 # re-raise KeyboardInterrupt, to truncate traceback
-> 1120 raise KeyboardInterrupt("Interrupted by user") from None
1121 except Exception:
1122 self.log.warning("Invalid Message:", exc_info=True)

KeyboardInterrupt: Interrupted by user

In [46]: # write a program to take 5 no from user and display whether the no is even or odd

for i in range (5):


a=int(input("Enter your no: ") )
if a%2==0:
print(" No is Even Number ")
else:
print(" No is Odd Number")

Enter your no: 1


No is Odd Number
Enter your no: 4
No is Even Number
Enter your no: 5
No is Odd Number
Enter your no: 7
No is Odd Number
Enter your no: 5
No is Odd Number

while loop
while is used to repeat the execution of the statement based on the condition being True

while condition:
statement to be repeated

localhost:8888/nbconvert/html/Desktop/NHANU/python/python jupitor/loop and while loop 08-01-2025.ipynb?download=false 3/4


1/8/25, 10:57 AM loop and while loop 08-01-2025

In [53]: # finite while loop

i=1

while i<11:
print(i,"Welcome")
i+=1 #i=i+1

1 Welcome
2 Welcome
3 Welcome
4 Welcome
5 Welcome
6 Welcome
7 Welcome
8 Welcome
9 Welcome
10 Welcome

In [ ]: #Infinite whill loop === remove incrementation i=i+1 to see how infinite while loop

In [63]: # write a program to give sum of no entered by user till the no is positive

s=0
a=int(input("Enter your no: "))

while a>0:
s=s+a
a=int(input("Enter Your NO : "))
print(s,"is a final output after adding the",a)

Enter your no: 1


Enter Your NO : 2
Enter Your NO : 3
Enter Your NO : 4
Enter Your NO : 5
Enter Your NO : -1
15 is a final output after adding the -1

localhost:8888/nbconvert/html/Desktop/NHANU/python/python jupitor/loop and while loop 08-01-2025.ipynb?download=false 4/4

You might also like