Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1Python If-Else Statements
If-else statements are control structures in Python that allow you to make decisions based on certain conditions. They help you execute specific blocks of code depending on whether a condition is true or false. Let's explore the syntax and usage of if-else statements in Python with examples.
Syntax
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
Example 1: Checking a Condition
Consider the following example:
age = 20
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
In this example, we have an if-else statement that checks whether the variable "age" is greater than or equal to 18. If the condition is true, it prints "You are eligible to vote." Otherwise, it prints "You are not eligible to vote."
Example 2: Nested if-else Statements
You can also have nested if-else statements, where one if-else statement is nested within another. Here's an example:
num = 10
if num > 0:
print("The number is positive.")
else:
if num < 0:
print("The number is negative.")
else:
print("The number is zero.")
In this example, the outer if-else statement checks whether the variable "num" is greater than 0. If it is, it prints "The number is positive." If the condition is false, it goes to the inner if-else statement to check whether the number is less than 0 or equal to 0, and prints the corresponding message.
Example 3: Multiple Conditions with elif
You can use the "elif" keyword to specify multiple conditions in an if-else statement. Here's an example:
num = 5
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
In this example, the if-else statement first checks if the number is greater than 0. If it is true, it prints "The number is positive." If the condition is false, it moves to the next condition specified by elif and checks if the number is less than 0. If that condition is true, it prints "The number is negative." If none of the conditions are true, it executes the else block and prints "The number is zero."
Conclusion
If-else statements are powerful constructs in Python that allow you to make decisions based on conditions. By understanding the syntax and usage of if-else statements, you can control the flow of your program and execute specific code blocks based on different scenarios.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners