Total Pageviews

Saturday, April 18, 2020

WHILE LOOP IN PYTHON

1. BASIC CLICK
2. Strong Number in Python CLICK
3. WAP to calculate the sum and product of digit of a number. CLICK



📘 while Loop in Python (Explanation + 20 Programs)


🎯 What is a while loop?

A while loop is used to repeat a block of code as long as a condition is True.


📌 Definition

A while loop executes a set of statements repeatedly until the given condition becomes False.


🧠 Syntax

while condition:
# code block

⚡ How it works

  1. Check condition
  2. If True → execute code
  3. Update variable
  4. Repeat
  5. Stop when condition becomes False

📘 Example

i = 1

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

📌 Output

1
2
3
4
5

🔥 Difference: while vs for

Featurewhile loopfor loop
ConditionYesNo (range-based)
UsageUnknown iterationsKnown iterations
ControlManualAutomatic

🐍 20 BASIC PROGRAMS USING while LOOP


1. Print 1 to 10

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

2. Print 10 to 1

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

3. Even Numbers (1–20)

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

4. Odd Numbers (1–20)

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

5. Sum 1 to 10

i = 1
sum = 0

while i <= 10:
sum += i
i += 1

print(sum)

6. Factorial

n = 5
fact = 1
i = 1

while i <= n:
fact *= i
i += 1

print(fact)

7. Multiplication Table

n = 5
i = 1

while i <= 10:
print(n, "x", i, "=", n*i)
i += 1

8. Reverse Number

n = 123
rev = 0

while n > 0:
digit = n % 10
rev = rev * 10 + digit
n //= 10

print(rev)

9. Palindrome Number

n = 121
temp = n
rev = 0

while n > 0:
rev = rev * 10 + n % 10
n //= 10

if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")

10. Sum of Digits

n = 123
sum = 0

while n > 0:
sum += n % 10
n //= 10

print(sum)

11. Count Digits

n = 12345
count = 0

while n > 0:
count += 1
n //= 10

print(count)

12. Fibonacci Series

a, b = 0, 1
i = 1

while i <= 10:
print(a)
a, b = b, a + b
i += 1

13. Print Characters

s = "Python"
i = 0

while i < len(s):
print(s[i])
i += 1

14. Find Largest Number

arr = [10, 50, 20, 80]
i = 0
max = arr[0]

while i < len(arr):
if arr[i] > max:
max = arr[i]
i += 1

print(max)

15. Find Smallest Number

arr = [10, 50, 20, 80]
i = 0
min = arr[0]

while i < len(arr):
if arr[i] < min:
min = arr[i]
i += 1

print(min)

16. Print List Elements

arr = [1, 2, 3, 4]
i = 0

while i < len(arr):
print(arr[i])
i += 1

17. Sum of List

arr = [10, 20, 30]
i = 0
sum = 0

while i < len(arr):
sum += arr[i]
i += 1

print(sum)

18. Count Even Numbers

i = 1
count = 0

while i <= 50:
if i % 2 == 0:
count += 1
i += 1

print(count)

19. Simple Guess Game

secret = 5
guess = 0

while guess != secret:
guess = int(input("Enter number: "))

print("Correct!")

20. Infinite Loop (Example)

# while True:
# print("Hello")

📌 Summary

  • while loop runs until condition becomes False
  • Used when iterations are unknown
  • Requires manual update of variable
  • Very useful in number problems and logic building

🧠 Important Questions

Short Questions

  1. What is while loop?
  2. Write syntax of while loop.
  3. Difference between while and for loop.
  4. What is infinite loop?
  5. Give one example.

Long Questions

  1. Explain while loop with example.
  2. Write program for factorial using while loop.
  3. Write program for reverse number using while loop.
  4. Explain Fibonacci series using while loop.
  5. Write 10 programs using while loop. 

LIST IN PYTHON

📘 List in Python 

🎯 What is a List in Python?

A list in Python is a collection of items that is:

  • Ordered (index-based)
  • Changeable (mutable)
  • Allows duplicate values
  • Can store multiple data types

📌 Definition

A list is a data structure in Python used to store multiple values in a single variable.


🧠 Syntax

my_list = [10, 20, 30, "Python", True]

📊 Example List

numbers = [1, 2, 3, 4, 5]

📍 Indexing in List

Element1234
Index0123

⚡ 1. Creating a List

fruits = ["apple", "banana", "mango"]
print(fruits)

Output:

['apple', 'banana', 'mango']

⚡ 2. Accessing Elements

fruits = ["apple", "banana", "mango"]
print(fruits[1])

Output:

banana

⚡ 3. Negative Indexing

fruits = ["apple", "banana", "mango"]
print(fruits[-1])

Output:

mango

⚡ 4. append() → Add Element at End

nums = [1, 2, 3]
nums.append(4)
print(nums)

Output:

[1, 2, 3, 4]

⚡ 5. insert() → Add at Specific Position

nums = [1, 2, 4]
nums.insert(2, 3)
print(nums)

Output:

[1, 2, 3, 4]

⚡ 6. extend() → Add Multiple Items

a = [1, 2]
b = [3, 4]
a.extend(b)
print(a)

Output:

[1, 2, 3, 4]

⚡ 7. remove() → Remove Value

nums = [1, 2, 3, 4]
nums.remove(3)
print(nums)

Output:

[1, 2, 4]

⚡ 8. pop() → Remove by Index

nums = [10, 20, 30]
nums.pop(1)
print(nums)

Output:

[10, 30]

⚡ 9. clear() → Remove All Items

nums = [1, 2, 3]
nums.clear()
print(nums)

Output:

[]

⚡ 10. sort() → Ascending Order

nums = [3, 1, 4, 2]
nums.sort()
print(nums)

Output:

[1, 2, 3, 4]

⚡ 11. sort(reverse=True)

nums = [3, 1, 4, 2]
nums.sort(reverse=True)
print(nums)

Output:

[4, 3, 2, 1]

⚡ 12. reverse()

nums = [1, 2, 3]
nums.reverse()
print(nums)

Output:

[3, 2, 1]

⚡ 13. len() → Length of List

nums = [1, 2, 3, 4]
print(len(nums))

Output:

4

⚡ 14. max()

nums = [10, 50, 20]
print(max(nums))

Output:

50

⚡ 15. min()

nums = [10, 50, 20]
print(min(nums))

Output:

10

⚡ 16. sum()

nums = [1, 2, 3, 4]
print(sum(nums))

Output:

10

⚡ 17. Loop through List

nums = [10, 20, 30]

for i in nums:
print(i)

Output:

10
20
30

⚡ 18. Check Element in List

nums = [1, 2, 3]

print(2 in nums)

Output:

True

⚡ 19. Copy List

a = [1, 2, 3]
b = a.copy()
print(b)

Output:

[1, 2, 3]

⚡ 20. List Slicing

nums = [10, 20, 30, 40, 50]
print(nums[1:4])

Output:

[20, 30, 40]

📌 Summary

✔ List = collection of multiple values
✔ Mutable (can change)
✔ Indexed (0-based)
✔ Supports many built-in methods


🧠 Important Exam Questions

Short Questions

  1. What is a list in Python?
  2. Is list mutable?
  3. What is indexing?
  4. Difference between append and insert?
  5. What is slicing?

Long Questions

  1. Explain list in Python with example.
  2. Write list methods with examples.
  3. Explain append, remove, pop methods.
  4. Write program using list operations.
  5. Explain list slicing with example.