Master Python If-Else and Loops with Simple Examples

Control statements in Python are used to control the flow of a program based on conditions and repetitions. They help programs make decisions and execute code multiple times efficiently. The two main types of control statements are decision-making statements (if-else) and looping statements (for and while loops).


๐Ÿ”น Decision-Making with If-Else

The if-else statement allows a program to execute certain code only when a condition is true.

โœ” Basic If Statement

age = 18
if age >= 18:
print("You are eligible to vote")

The code inside the if block runs only if the condition is true.


โœ” If-Else Statement

marks = 40if marks >= 50:
print("Pass")
else:
print("Fail")

If the condition is false, the else block executes.


โœ” If-Elif-Else Ladder

score = 75if score >= 90:
print("Excellent")
elif score >= 60:
print("Good")
else:
print("Average")

This helps handle multiple conditions in a structured way.


๐Ÿ”น Looping in Python

Loops are used to repeat a block of code multiple times. Python provides two main loops:

  • for loop
  • while loop

๐Ÿ”น For Loop

The for loop is used to iterate over a sequence like a list, tuple, or range.

for i in range(5):
print(i)

This will print numbers from 0 to 4.

Example with a list:

fruits = ["apple", "banana", "cherry"]for fruit in fruits:
print(fruit)

The loop runs once for each item in the list.


๐Ÿ”น While Loop

The while loop executes a block of code as long as a condition is true.

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

The loop continues until the condition becomes false.


๐Ÿ”น Break and Continue Statements

Python also provides control statements to manage loops:

โœ” Break Statement

Used to exit a loop early.

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

The loop stops when i equals 5.


โœ” Continue Statement

Used to skip the current iteration.

for i in range(5):
if i == 2:
continue
print(i)

The number 2 will be skipped.


๐Ÿ”น Why Control Statements Are Important

  • Enable decision-making in programs
  • Allow automation of repetitive tasks
  • Improve efficiency and logic building
  • Help in solving real-world problems using conditions and loops

๐Ÿ”น Real-Life Example

  • If a student scores above 50 โ†’ Pass
  • If not โ†’ Fail
  • Repeat this check for multiple students using loops

This combination of conditions and loops is widely used in applications like games, websites, and data processing systems.


Control statements are the backbone of Python programming. The if-else statements help in decision-making, while for and while loops help in repetition. By mastering these concepts, beginners can build logical thinking and write efficient Python programs for real-world applications.

For More Information and Updates, Connect With Us

Stay connected and keep learning with Python Training 

Leave a Reply

Your email address will not be published. Required fields are marked *

About Us

Luckily friends do ashamed to do suppose. Tried meant mr smile so. Exquisite behaviour as to middleton perfectly. Chicken no wishing waiting am. Say concerns dwelling graceful.

Services

Most Recent Posts

  • All Post
  • Accounting
  • Branding
  • Cybersecurity
  • Data Analytics
  • Development
  • Education
  • Education Technology
  • Health Technology
  • Leadership
  • Management
  • Neuroscience and Technology
  • Programming
  • Programming and Development
  • Programming Languages
  • Technology
  • Technology & Innovation
  • Technology and Creativity
  • Web Development
  • Web Development Guides

Category

ยฉ 2025 Created with Emancipation Edutech Pvt Ltd