0% found this document useful (0 votes)
40 views2 pages

352 - Whileloop - Colab

The document contains Python code examples demonstrating the use of while loops for various tasks, including printing 'hello' three times, counting from 1 to 10, counting down from 10 to 1, calculating the sum of the first n natural numbers, and printing even numbers from 1 to 20. Each code snippet is accompanied by its output. The document serves as a tutorial for understanding while loops in Python.
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)
40 views2 pages

352 - Whileloop - Colab

The document contains Python code examples demonstrating the use of while loops for various tasks, including printing 'hello' three times, counting from 1 to 10, counting down from 10 to 1, calculating the sum of the first n natural numbers, and printing even numbers from 1 to 20. Each code snippet is accompanied by its output. The document serves as a tutorial for understanding while loops in Python.
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

2/19/25, 4:05 PM 352_whileloop - Colab

count=0
while(count<3):
count=count+1
print("hello")

hello
hello
hello

Start coding or generate with AI.

i=1
while(i>=0):
i=i+2
print(i)

Show hidden output

#print numbers from 1 to 10


i=1
while i<=10:
print(i)
i+=1

1
2
3
4
5
6
7
8
9
10

#print numbers from 10 to 1


i=10
while i>=1:
print(i)
i-=1

10
9
8
7
6
5
4
3
2
1

#Sum of First n Natural Numbers


n=int(input("Enter a number"))
sum=0
i=1
while i<=n:
sum+=i
i+=1
print(f"The sum of first {n}natural numbers is {sum}")

Enter a number10
The sum of first 10natural numbers is 55

#print even numbers from 1 to 20


i=0
while i<=20:
print(i)
i+=2

0
2

https://colab.research.google.com/drive/1mA9c3Jx9ktOTWNg2N0c6861kjxVZfko7 1/2
2/19/25, 4:05 PM 352_whileloop - Colab
4
6
8
10
12
14
16
18
20

https://colab.research.google.com/drive/1mA9c3Jx9ktOTWNg2N0c6861kjxVZfko7 2/2

You might also like