If/Else Statements in Python

# If/Else Statements in Python - Basics of Python - Software Development

 Table of contents Introduction Syntax of if/else Statements Examples of if/else Statements Nested if/else Statements Sample Problems ## Introduction

If/else statements are an essential part of programming in Python. They allow you to make decisions based on certain conditions and execute different blocks of code accordingly. In this article, we will explore the basics of if/else statements, understand their syntax, and see how they can be used in various scenarios. By the end, you'll have a solid understanding of how to use if/else statements in your Python programs.

## Syntax of if/else Statements

The basic syntax of an if/else statement in Python is as follows:

if condition:

# Code to execute if the condition is true

else:

# Code to execute if the condition is false

The 'condition' is an expression that evaluates to either 'True' or 'False'. If the condition is 'True', the code block inside the 'if' statement will be executed. Otherwise, if the condition is 'False', the code block inside the 'else' statement will be executed.

## Examples of if/else Statements

Let's look at some examples to better understand how if/else statements work in Python.

Example 1: Checking if a Number is Even or Odd

num = 7

if num % 2 == 0:

print("The number is even")

else:

print("The number is odd")

Output:

The number is odd

In this example, we check if the number 'um' is even or odd. The condition 'num % 2 == 0' checks if 'num' is divisible by 2. If it is, the condition is 'True', and the code block inside the 'if' statement is executed. Otherwise, the code block inside the 'else' statement is executed.

Example 2: Comparing Two Numbers

num1 = 10

num2 = 5

if num1 > num2:

print("num1 is greater than num2")

else:

print("num1 is not greater than num2")

Output:

num1 is greater than num2

In this example, we compare two numbers, 'num1' and 'num2'. If 'num1' is greater than 'num2', the condition 'num1' > 'num2' is True, and the code block inside the if statement is executed. Otherwise, the code block inside the 'else' statement is executed.

## Nested if/else Statements

In addition to simple if/else statements, you can also nest them inside each other to handle more complex conditions. This allows you to create multiple levels of decision-making.

Example 1: Nested if/else Statements

num = 15

if num > 10:

if num % 2 == 0:

print("The number is greater than 10 and even")

else:

print("The number is greater than 10 but odd")

else:

print("The number is not greater than 10")

Output:

The number is greater than 10 but odd

In this example, we check if the number 'num' is greater than 10. If it is, we further check if it's even or odd. Depending on the conditions, different code blocks are executed.

## Sample Problems

Now that we've covered the basics, let's try some sample problems to reinforce your understanding. Here are a few problems along with their solutions:

Problem 1: Check if a Year is a Leap Year
Write a program that checks if a given year is a leap year or not. A leap year is divisible by 4 but not divisible by 100, except if it's divisible by 400.

year = 2024

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):

print(year, "is a leap year")

else:

print(year, "is not a leap year")

Output:

2024 is a leap year

Write a program that takes a student's score as input and prints their corresponding grade based on the following conditions:

• Score greater than or equal to 90: A
• Score between 80 and 89: B
• Score between 70 and 79: C
• Score between 60 and 69: D
• Score below 60: F

score = 85

if score >= 90:

elif score >= 80:

elif score >= 70:

elif score >= 60:

else:

Output:

Problem 3: User Authentication

print("Authentication successful")

else:

print("Authentication failed")

Sample output:

Authentication successful

Authentication failed

### Conclusion

In this article, we explored the basics of if/else statements in Python. We learned their syntax, saw various examples, and understood how they can be used to make decisions based on conditions. By practicing with sample problems, you can further enhance your understanding and apply if/else statements in real-world scenarios. Keep experimenting and exploring the power of if/else statements in your Python programming journey.

The document If/Else Statements in Python | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development

## Basics of Python

49 videos|38 docs|18 tests

## Basics of Python

49 videos|38 docs|18 tests Explore Courses for Software Development exam Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;