newtum

Our Social Network

CONDITIONAL OPERATORS IN PYTHON WITH EXAMPLE - COMPLETE BEGINNER-FRIENDLY GUIDE

Conditional Operators in Python with Example - Complete Beginner Guide

Python gives us the power to make decisions in code using conditions—just like we make decisions in real life. From checking if a number is greater than another, to comparing values or verifying equality, conditional operators make this possible.

What Are Conditional Operators in Python?

Conditional operators (also known as comparison operators) are used to compare two values. Based on the comparison, Python returns either True or False.

Here’s a list of commonly used conditional operators:

Operator Meaning Example
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
== Equal to a == b
!= Not equal to a != b

These operators are often used in if conditions to control the program flow based on values

Example 1: Check if a Number is Divisible by 4

Let’s write a program that checks if a number is divisible by 4 using conditional operators.

1n = int(input("Enter a number: "))
2if n % 4 == 0:
3print("Number " + str(n) + " is divisible by 4.")
4else:
5print("Number " + str(n) + " is not divisible by 4.")

Code Explanation

  • n % 4 calculates the remainder when n is divided by 4.
  • The == operator checks whether the result is equal to 0.
  • If True, it prints that the number is divisible by 4.
  • Otherwise, it prints that the number is not divisible.

Output

Enter a number: 20
Number 20 is divisible by 4.
Enter a number: 21
Number 21 is not divisible by 4.

Modifying the Program Using != Operator

Let’s now flip the logic and use the not equal to (!=) operator:

n = int(input("Enter a number: "))
if n % 4 != 0:
print("Number", n, "is not divisible by 4.")
else:
print("Number", n, "is divisible by 4.")

This change checks whether the number is not divisible by 4 first, and the else handles the rest.

Example 2: Ticket Fare Based on Train Type

Let’s use the == (equal to) operator to determine the fare based on the train type.

1train_type = input("Enter train type: ")
2if train_type == "Rajdhani":
3ticket = 2500
4else:
5ticket = 1500
6print("Ticket fare from Delhi to Mumbai is Rs." , ticket)

Code Explanation

  • Accepts user input for train_type.
  • Compares it to the string "Rajdhani" using the == operator.
  • If matched, sets ticket to 2500.
  • Else, sets ticket to 1500.
  • Prints the final fare.

Note: When comparing strings, enclose them in double or single quotes ("Rajdhani" or 'Rajdhani').

Output

Enter train type: Rajdhani
Ticket fare from Delhi to Mumbai is Rs. 2500
Enter train type: Express
Ticket fare from Delhi to Mumbai is Rs. 1500

Understanding the Role of Conditional Operators

Conditional operators are essential in every Python program involving decision-making. They are the core building blocks when:

  • Checking user input
  • Creating interactive logic
  • Validating data
  • Implementing rules and conditions

Without these operators, it would be impossible to create responsive or intelligent applications.

Watch Our YouTube Tutorial

Want to see this in action?
Watch the full tutorial on YouTube — where we break this down with real-time code and visuals.

Conclusion

Conditional operators in Python are the backbone of decision-making. Whether it’s checking equality, comparing numbers, or validating strings, they allow your program to think and respond dynamically. Practice writing different condition-based examples to strengthen your programming logic.

Frequently Asked Questions (FAQs)

1. What are conditional operators in Python?
Conditional operators compare two values and return True or False. Examples include ==, !=, >, <, >=, and <=.
2. What is the difference between = and == in Python?
  • = is an assignment operator, used to assign a value to a variable.
  • == is a comparison operator, used to compare two values.
3. Can conditional operators be used with strings?

Yes! You can compare strings using conditional operators like == and !=. Make sure to use quotes when comparing strings.

4. Is != the same as not equal to in Python?

Yes. != is the symbol for "not equal to". For example, a != b means "a is not equal to b".

5. Why does Python return True or False in comparisons?

Python follows Boolean logic. Conditional operators return True or False, which help determine the flow of execution in conditions like if, elif, and else.

One condition, one comparison, and Python knows what to do next. That’s the power of conditional operators.

— Manoj Kolhe

More Articles

Ready to Explore the Future of Technology?

Unlock the tools and insights you need to thrive on social media with Newtum. Join our community for expert tips, trending strategies, and resources that empower you to stand out and succeed.

Newtum

Newtum is an emerging online academy offering a wide range of high-end technical courses for people of all ages. We offer courses based on the latest demands and future of the IT industry.

© Newtum, Inc. All Rights Reserved.