Discover Serenity: Your Guide to Mindfulness Apps
Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps
Discover How Smartwatches Measure Blood Pressure: Explained Simply
13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!
Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo
Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!
Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals
Affordable VR Adventures: The Best Budget VR Headsets
Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying
Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More
Discover Exciting New Games on NVIDIA GeForce NOW!
Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games
The Future of iPhones: Apple's Bold Step into AI with DarwinAI
Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!
Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!
Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches
Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024
Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!
Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent
Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer
Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!
JavaScript Essentials: Your Quick Start to Web Coding
Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!
Gaming Joy Awaits: Explore the Best PS5 Games of 2024
Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!
Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit
Feel the Beat: Exploring Top Over-Ear Headphones
Explore the Web Development Strategies in 2024: A Developers Handbook
Explore Must-Play Nintendo Switch Games in 2024!
Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle
Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World
Discover Waze's cool new features for safer and happier drives!
Discover the Top Picks: Best Smartwatches for Your Lifestyle
Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!
Sound Revolution: Discover the Best Bluetooth Speakers of 2024!
Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks
Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024
Crack the Code: Your Guide to Computer Programming Magic in 2024
Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity
10 Best Tech Companies in Silicon Valley
Top 10 Web Development Interview Questions you can...
Learn how to Answer Tell me about Yourself
5 Books You Need to Read Right Now
25 Practical Ways to Earn Money Online
Translate this page in your preferred language:
Article by: Manish Methani
Last Updated: October 6, 2021 at 10:04am IST
Python's conditional statements are used for decision-making in programs. One of the most commonly used conditional statements is the 'if-else' statement. The 'if-else' statement is used when you want to execute a block of code based on certain conditions. In this tutorial, we will discuss the 'if-else' statement in Python, along with practical codes, examples, and explanations.
The syntax for the 'if-else' statement in Python is as follows:
if condition: # Execute this block of code if the condition is true else: # Execute this block of code if the condition is false
2. Practical Examples of If-Else Statement in Python:
Example 1: Comparing values
x = 10 y = 20 if x > y: print("x is greater than y") else: print("y is greater than x")
Output: y is greater than x
Explanation: In this example, we have two variables x and y. We are comparing their values using the 'if-else' statement. Since x is not greater than y, the output will be "y is greater than x".
Example 2: Checking user input
age = int(input("Enter your age: ")) if age >= 18: print("You are eligible to vote") else: print("You are not eligible to vote")
Output: You are eligible to vote
Explanation: In this example, we are taking user input for their age. We are then using the 'if-else' statement to check whether the user is eligible to vote or not. If the user's age is greater than or equal to 18, the output will be "You are eligible to vote". Otherwise, the output will be "You are not eligible to vote".
Example 3: Checking if a number is even or odd
num = int(input("Enter a number: ")) if num % 2 == 0: print("The number is even") else: print("The number is odd")
Output:
Enter a number: 10 The number is even
Explanation: In this example, we are taking user input for a number. We are then using the 'if-else' statement to check if the number is even or odd. If the number is divisible by 2, it is even. Otherwise, it is odd.
The if-else-elif statement is a decision-making statement in Python programming that allows the programmer to execute specific code based on certain conditions. The if statement checks for a particular condition and executes the code within the block if the condition is true. The else statement executes a block of code when the condition in the if statement is false. The elif statement, which is short for "else if", allows the programmer to check for multiple conditions and execute different blocks of code based on the conditions that are true.
Syntax of if-else-elif statement:
if condition1: statement1 elif condition2: statement2 else: statement3
num = float(input("Enter a number: ")) if num > 0: print("The number is positive.") elif num == 0: print("The number is zero.") else: print("The number is negative.")
2. Program to check if a student has passed or failed a subject:
marks = int(input("Enter your marks: ")) if marks >= 40: print("Congratulations! You have passed.") else: print("Sorry, you have failed.")
In this tutorial, we have discussed the 'if-else' statement in Python, along with practical codes, examples, and explanations. The 'if-else' statement is an essential tool for decision-making in programs. By understanding how to use the 'if-else' statement, you can write more efficient and effective programs in Python.
Test your C Programming skills with this comprehensive mock test on C Programming.
Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.
Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.
Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.
Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.
Take Test