In Python programming, loops are used to execute a block of code repeatedly. Instead of writing the same statement many times, you can use a loop to perform the same task automatically.

For example, suppose you want to print numbers from 1 to 5. Without a loop, you would need to write five print() statements. With a loop, you can accomplish the same task with just a few lines of code.

Python provides two main types of loops:

  • for loop

  • while loop

Python also provides statements such as break, continue, and pass to control how a loop behaves.

Loops in Python

Example

age = 20

if age >= 18:

print("You are eligible.")

Here, Python checks whether age >= 18 is true. Since the condition is true, the message is displayed.

Conditional statements are an important part of Python because they help programs make decisions and respond differently to different inputs.

The if statement executes a block of code only when a specified condition is true. This means that the code within the block will not run unless the condition evaluates to true, allowing for controlled execution and logic flow in programming.

Syntax

if (condition):

    # code to execute

The condition is followed by a colon (:), and the code inside the if block must be properly indented.

Input

temperature = 30

if temperature > 25:

print("It is a warm day.")

Output

Example

marks = 75

if marks >= 40:

print("You passed the exam.")

It is a warm day.

1. The if Statement

Since 30 > 25 is true, Python executes the print() statement.

Important Point

Python uses indentation to identify the statements belonging to an if block.

Correct:

if age >= 18:

print("Adult")

Incorrect:

if age >= 18:

print("Adult")

Output

You passed the exam.

The if-else statement allows a program to choose between two possible actions.

  • If the condition is true, the if block runs.

  • If the condition is false, the else block runs.

Syntax

if condition:

    # code when condition is True

else:

    # code when condition is False

Input

age = 16

if age >= 18:

print("You can vote.")

else:

print("You cannot vote yet.")

Output

Example with Numbers

number = 10

if number % 2 == 0:

print("The number is even.")

else:

print("The number is odd.")

You cannot vote yet.

2. The if-else Statement

The condition age >= 18 is false, so Python executes the else block.

The % operator returns the remainder after division. If the remainder is 0, the number is even.

Output


The number is even....

Sometimes a program needs to check more than two conditions. Python provides elif, which means "else if".

The if-elif-else structure allows Python to test multiple conditions in sequence.

Syntax

if condition1:

    # code

elif condition2:

    # code

elif condition3:

    # code

else:

    # code

Python checks the conditions from top to bottom. As soon as it finds a true condition, its corresponding block is executed.

Input

marks = 82

if marks >= 90:

print("Grade A+")

elif marks >= 75:

print("Grade A")

elif marks >= 60:

print("Grade B")

else:

print("Needs improvement")

Output

Example: Checking Temperature

temperature = 15

if temperature >= 35:

print("Very hot")

elif temperature >= 25:

print("Warm")

elif temperature >= 15:

print("Cool")

else:

print("Cold")

Grade A

3. The if-elif-else Statement

Because 82 >= 75 is true, Python executes the second condition.

How elif Works

You can use multiple elif statements when several possible conditions need to be checked.

if condition1:

...

elif condition2:

...

elif condition3:

...

else:

...

Output

Cool

A nested if statement means placing one if statement inside another if statement.

Nested conditions are useful when one decision depends on another decision.

Input

age = 20

has_id = True

if age >= 18:

if has_id:

print("Entry allowed.")

Output

Example: Nested if with else

age = 17

if age >= 18:

if age >= 21:

print("Eligible for the activity.")

else:

print("Age requirement is not met.")

else:

print("You are underage.")

Entry allowed.

4. Nested if Statements

First, Python checks whether the person is at least 18. If that condition is true, it then checks whether the person has an ID.

Nested if statements should be used carefully. If too many levels are created, the program can become difficult to read and maintain.

Output

You are underage.

5. Multiple Conditions

Python allows you to combine multiple conditions using logical operators.

The three commonly used logical operators are:

Operator

Meaning

and

True when all conditions are true

or

True when at least one condition is true

not

Reverses the Boolean result

Using and

The and operator requires both conditions to be true in order for the overall expression to yield a true result. It is crucial to understand how this operator functions within logical expressions and how it can affect the outcome of decision-making processes in programming.

Input

Output

age = 25

has_license = True

if age >= 18 and has_license:

print("You can drive.")

You can drive.

Using or

The or operator returns true when at least one condition is true.

Input

Output

day = "Sunday"

if day == "Saturday" or day == "Sunday":

print("It is the weekend.")

It is the weekend.

Using not

The not operator reverses a condition, which can often be very useful in programming. It allows us to check for the opposite of a specified condition, making our code more flexible and helping to control the flow based on different scenarios.

Input

Output

is_raining = False

if not is_raining:

print("You can go outside.")

You can go outside.

Combining Conditions

Multiple logical operators can also be used together.

Output

Input

age = 22

has_ticket = True

is_member = False

if age >= 18 and (has_ticket or is_member): print("Access granted.")

else:

print("Access denied.")

Access granted.

Parentheses can make complex conditions easier to understand.

6. Short-Hand if

Regular if

Short-Hand if

age = 20

if age >= 18:

print("Adult")

age = 20

if age >= 18: print("Adult")

Output

Adult

Short-hand if statements can be useful for very simple conditions. However, for longer or more complex logic, the normal multi-line format is usually easier to read.

Python allows a simple if statement to be written in a single line when only one statement needs to be executed.

This is known as a short-hand if or one-line if.

Next >>

<<Back

Contact

Reach out for AI courses and support

Email

Phone

careers@ahkacademy.in

Mo:- +91-7769920076

}

© 2025. All rights reserved.