Loop and While Loop 08-01-2025
Loop and While Loop 08-01-2025
hello
hello
hello
hello
hello
hello,
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
range(stop) => stop number will not be included in the sequence range(5) => 0, 1, 2, 3, 4
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
hello
hello
hello
hello
hello
Enter your no : 5
5
10
15
20
25
30
35
40
45
50
sum1=0
for i in range(1,6):
a=int(input("Enter your no: "))
sum1=sum1+a
print("sum after adding ",a,"is",sum1)
---------------------------------------------------------------------------
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)
In [46]: # write a program to take 5 no from user and display whether the no is even or odd
while loop
while is used to repeat the execution of the statement based on the condition being True
while condition:
statement to be repeated
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)