Total Pageviews

Saturday, April 18, 2020

FOR LOOP IN PYTHON

1. BASIC - CLICK



🎯 What is a for loop?

A for loop in Python is used to repeat a block of code for each item in a sequence (like list, string, range, etc.).


📌 Definition

A for loop is used to iterate over a sequence (list, tuple, string, range) and execute code repeatedly.


🧠 Basic Syntax

for variable in sequence:
# code block

🔢 1. Using range() (Most Important)

Example 1: Print 1 to 5

for i in range(1, 6):
print(i)

Output:

1
2
3
4
5

Example 2: Print Even Numbers

for i in range(2, 11, 2):
print(i)

Output:

2
4
6
8
10

📌 range() Explanation

range(start, stop, step)
ParameterMeaning
startStarting number
stopEnding number (not included)
stepIncrement value

📚 2. Looping through a List

fruits = ["apple", "banana", "mango"]

for fruit in fruits:
print(fruit)

Output:

apple
banana
mango

🔤 3. Looping through a String

for letter in "Python":
print(letter)

Output:

P
y
t
h
o
n

🔁 4. Using for with if (Very Important)

Example: Print numbers greater than 5

for i in range(1, 11):
if i > 5:
print(i)

Output:

6
7
8
9
10

🧠 5. Nested for Loop

(Loop inside loop)

Example: Multiplication Table

for i in range(1, 4):
for j in range(1, 4):
print(i * j, end=" ")
print()

Output:

1 2 3
2 4 6
3 6 9

⚡ Advantages of for Loop

  • Easy to use
  • Reduces code length
  • Works with sequences
  • Useful for arrays, lists, strings

❌ Disadvantages

  • Not suitable for unknown repetition count (use while loop instead)

🆚 for loop vs while loop

Featurefor loopwhile loop
UsageKnown rangeUnknown condition
StructureSimpleMore flexible
ControlAutomatic iterationManual control

📌 Real-Life Example

  • Counting students in class 👨‍🎓
  • Printing attendance list 📋
  • Iterating shopping items 🛒

📘 Summary

  • for loop is used for repeating tasks
  • Works with range, lists, strings, etc.
  • Very important for programming and algorithms
  • Commonly used in sorting and searching programs

❓ Important Questions

Short Questions

  1. What is a for loop?
  2. What is range() function?
  3. Write syntax of for loop.
  4. What is nested for loop?
  5. Give one example of for loop.

Long Questions

  1. Explain for loop with example.
  2. Write program using for loop to print numbers 1 to 10.
  3. Explain nested for loop with example.
  4. Compare for loop and while loop.
  5. Write a program using for loop for multiplication table.


1. Print 1 to 10

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

2. Print 10 to 1 (Reverse)

for i in range(10, 0, -1):
print(i)

3. Print Even Numbers (1 to 20)

for i in range(2, 21, 2):
print(i)

4. Print Odd Numbers (1 to 20)

for i in range(1, 21, 2):
print(i)

5. Sum of 1 to 10

sum = 0
for i in range(1, 11):
sum += i
print(sum)

6. Multiplication Table of 5

for i in range(1, 11):
print(5, "x", i, "=", 5*i)

7. Factorial of a Number

n = 5
fact = 1

for i in range(1, n+1):
fact *= i

print(fact)

8. Fibonacci Series

a, b = 0, 1

for i in range(10):
print(a)
a, b = b, a + b

9. Square of Numbers (1 to 10)

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

10. Cube of Numbers (1 to 10)

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

11. Count Even Numbers (1 to 50)

count = 0

for i in range(1, 51):
if i % 2 == 0:
count += 1

print(count)

12. Sum of Even Numbers (1 to 20)

sum = 0

for i in range(2, 21, 2):
sum += i

print(sum)

13. Print Characters of String

for ch in "Python":
print(ch)

14. Reverse Loop Printing List

arr = [10, 20, 30, 40]

for i in range(len(arr)-1, -1, -1):
print(arr[i])

15. Find Maximum in List

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

for i in arr:
if i > max:
max = i

print(max)

16. Find Minimum in List

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

for i in arr:
if i < min:
min = i

print(min)

17. Print All Elements of List

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

for i in arr:
print(i)

18. Sum of List Elements

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

for i in arr:
sum += i

print(sum)

19. Print Multiples of 3 (1 to 30)

for i in range(3, 31, 3):
print(i)

20. Print Pattern (Stars)

for i in range(1, 6):
print("*" * i)







No comments:

Post a Comment