Cross_Column

Sunday, 18 January 2026

Python Operators, Conditional Statements (if, else, elif), Loops (for & while)




Hello Friends,

Python Operators

Arithmetic Operators

a = 10
b = 5

print(a + b)
print(a - b)
print(a * b)
print(a / b)

Comparison Operators

print(a > b)
print(a == b)
================================================================================

Conditional Statements in Python

Conditional statements control the flow of execution.

Example

marks = 75

if marks >= 60:
    print("First Division")
elif marks >= 40:
    print("Second Division")
else:
    print("Fail")
==================================================================================

Loops in Python

For Loop

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


Python for Loop

The for loop in Python is used to iterate (loop) over a sequence such as a list, tuple, string, or range of numbers. It executes a block of code repeatedly for each item in the sequence.


Syntax of for Loop

for variable in sequence:
    statements
  • variable – takes value of each element in the sequence
  • sequence – list, tuple, string, or range
  • statements – code executed for each iteration

Example 1: for Loop with range()

The range() function generates a sequence of numbers.

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

Output:

1
2
3
4
5

Explanation:
The loop runs 5 times. The variable i takes values from 1 to 5.


Example 2: for Loop with List

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

for fruit in fruits:
    print(fruit)

Output:

apple
banana
mango

Explanation:
Each element of the list is assigned to the variable fruit one by one.


Example 3: for Loop with String

name = "Python"

for char in name:
    print(char)

Output:

P
y
t
h
o
n

Explanation:
Strings are sequences of characters. The loop iterates over each character.


Example 4: for Loop with range(start, stop, step)

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

Output:

0
2
4
6
8
10

Explanation:
The loop starts at 0, ends at 10, and increments by 2.


Example 5: Nested for Loop

A nested for loop means one loop inside another.

for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)

Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Example 6: for Loop with break

The break statement stops the loop immediately.

for i in range(1, 10):
    if i == 5:
        break
    print(i)

Output:

1
2
3
4

Example 7: for Loop with continue

The continue statement skips the current iteration.

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

Output:

1
2
4
5

Example 8: for Loop with else

The else block executes when the loop finishes normally.

for i in range(1, 4):
    print(i)
else:
    print("Loop completed successfully")

Output:

1
2
3
Loop completed successfully

Real-Life Example

Printing student marks:

marks = [65, 72, 80, 90]

for mark in marks:
    print("Student scored:", mark)

Common Mistakes

  • Missing colon : after for statement
  • Incorrect indentation
  • Using wrong variable name inside loop

Summary

  • for loop is used to iterate over sequences
  • range() is commonly used with for loops
  • Supports break, continue, and else
  • Very useful in automation and data processing

====================================================================

While Loop

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

No comments:

Post a Comment

Few More

Python String Handling

Hello Friends, String Handling in Python A string in Python is a sequence of characters enclosed within single quotes ( ...