0% found this document useful (0 votes)
80 views16 pages

4 - Class 8 ICSE Python WHILE - Loop - Worksheet

Class 8 ICSE python

Uploaded by

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

4 - Class 8 ICSE Python WHILE - Loop - Worksheet

Class 8 ICSE python

Uploaded by

shekharbiswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python ICSE Class VIII Worksheet:

WHILE and WHILE-ELSE Loops


Section A: Practice Questions

A1. Easy WHILE Loop Examples


1. Print numbers from 1 to 5 - done
2. Print the first 5 even numbers.
3. Print "Hello" 3 times.
4. Print numbers from 10 to 1 in reverse.
5. Print the squares of numbers from 1 to 5.

A2. Easy WHILE-ELSE Loop Examples


1. Print numbers from 1 to 3, and then print "Done!"
2. Print the first 4 odd numbers, and then print "Finished!"
3. Print countdown from 3 to 1 and then "Lift off!"
4. Print all numbers from 1 to 5 and then print "Loop Ended!"
5. Use a while-else to show how a loop works without breaking.

A3. Challenging WHILE Loop Problems


. Sum of first 10 natural numbers.
2. Count how many digits are in a number (e.g., 5234).
3. Reverse a given number (e.g., 123 → 321).
4. Find factorial of a number.
5. Display a pattern like:
*
**
***

A4. Challenging WHILE-ELSE Loop Problems


1. Print prime numbers from 2 to 20 using while-else.
2. Ask user for a password and break if correct, else print "Access Denied".
3. Find the first number divisible by both 3 and 5 in a range using while-else.
4. Use while-else to print if a number is a palindrome.
5. Search for a number in a list using while-else.

A5. Challenging JUMP statements Loop Problems

AProgram to print the even numbers between 1 and 25 by using Jump


statements( Break or continue
Python Programs with Output start

Initialize i=1

False
 Code: Is I <=5?

i = 1 Print i True
while i <= 5:
print(i)
i += 1 I =I +1

 Output:

1 Go to check
2
3
4
5

End
start

Initialize i=2
count=0

Is count
<=5?

 Code:

i = 2 Print i
count = 0
while count < 5: I =I +2
print(i)
i += 2 count =count +1
count += 1

 Output: Go to check

2
4
6 End
8
10

 Code:
i = 1
while i <= 3:
print('Hello')
i += 1

 Output:

Hello
Hello
Hello
 Code:

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

 Output:

10
9
8
7
6
5
4
3
2
1

 Code:

i = 1
while i <= 5:
print(i * i)
i += 1
 Output:

1
4
9
16
25

 Code:

i = 1
while i <= 3:
print(i)
i += 1
else:
print('Done!')

 Output:

1
2
3
Done!

 Code:

i = 1
count = 0
while count < 4:
print(i)
i += 2
count += 1
else:
print('Finished!')

 Output:

1
3
5
7
Finished!

 Code:
i = 3
while i > 0:
print(i)
i -= 1
else:
print('Lift off!')

 Output:

3
2
1
Lift off!

 Code:

i = 1
while i <= 5:
print(i)
i += 1
else:
print('Loop Ended!')

 Output:

1
2
3
4
5
Loop Ended!

 Code:

i = 1
while i <= 3:
print('Count:', i)
i += 1
else:
print('No break, loop completed.')

 Output:

Count: 1
Count: 2
Count: 3
No break, loop completed.
 Code:

i = 1
total = 0
while i <= 10:
total += i
i += 1
print('Sum:', total)

 Output:

Sum: 55

 Code:

num = 5234
count = 0
while num > 0:
num //= 10
count += 1
print('Digits:', count)

 Output:

Digits: 4

 Code:

num = 123
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
print('Reversed:', rev)

 Output:

Reversed: 321

 Code:

num = 5
fact = 1
while num > 0:
fact *= num
num -= 1
print('Factorial:', fact)

 Output:

Factorial: 120

 Code:

i = 1
while i <= 3:
print('*' * i)
i += 1

 Output:

*
**
***
 Iterates numbers from 2 to 20.

 For each number, it checks if it's divisible by any number less than itself (starting
from 2).

 If not divisible, it's a prime.

 The else attached to the inner while loop runs only if no break occurs, i.e., the
number is prime

 Code:
num = 2
while num <= 20:
i = 2
while i < num:
if num % i == 0:
break
i += 1
else:
print(num, 'is prime')
num += 1



 Output:

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime

 Code:

password = 'open123'
attempts = 0
while attempts < 3:
entry = input('Enter password: ')
if entry == password:
print('Access Granted')
break
attempts += 1
else:
print('Access Denied')

 Output:

(Depends on user input)


 Code:

num = 10
while num <= 50:
if num % 3 == 0 and num % 5 == 0:
print('Found:', num)
break
num += 1
else:
print('Not found')

 Output:

Found: 15

 Code:

original = 121
num = original
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
else:
if rev == original:
print('Palindrome')
else:
print('Not a Palindrome')

 Output:

Palindrome

 Code:

numbers = [3, 8, 11, 4, 9]


i = 0
key = 4
while i < len(numbers):
if numbers[i] == key:
print('Found at index', i)
break
i += 1
else:
print('Not Found')
 Output:

Found at index 3
Problem : Program to print the even numbers between 1 and 25 by using Jump
statements( Break or continue)

What the programme is expected to do

The loop starts from 1 and runs till 25.


If the number is odd, it skips the rest of the loop using continue.
If it's even, it prints the number.

Flow chart

num = 1
while num <= 25:
if num % 2 != 0:
num += 1
continue
print(num)
num += 1

Rinku

for number in range(0, 26, 2):

if (number//2) == 0 :

continue

print("Number is", number)

print("Out of loop")

n = int(input("Enter a number:"))

if n == 0 or n == 1:

print(n, "is not a prime number")

elif n > 1:

for i in range(2, n):


if (n % i == 0):

print(n, "is a prime number")

print(i, "times", n/i, "is", n)

break

else:

print(n, "is a prime number")

You might also like