Python Conditionals and Operators

Advertisements
Advertisements

So far you have learned about different structures of conditional. But the conditionals such as if-then-else also employ different types of logical operators. In this article, you will learn about the conditionals and operators that we can use with conditional structures. There are categories of logical operators that the python programming language uses and they are :

  1. Relational Operators
  2. Set Membership operators
  3. Boolean Functions
  4. Boolean Operators

Now we see examples of each one them and understand how it works.

Relational Operators

The relational operators are those operators that return true or false after comparing numeric or non-numeric values. To learn more about relational or logical operators visit the following link:

Python Logical Operators

Advertisements

Example Program:

# Check the mark of a student and display the result
marks_average = 60
if marks_average >= 50:
    print("You have passed the Test")
else:
    print("You failed! Try harder next time")

The program uses “greater than or equal to” (>=) operator to evaluate the marks_average. The output of the program is given below.

You have passed the Test

Set Membership Operator

The set membership operator checks if a particular value belongs to a set or not. The set could be anything that represent a list, set, dictionary, or string. There are two set membership operators

  1. in
  2. not in

In this example, we are going to use "in" operator to check if a value belongs to a list or not.

my_fruits = ["Oranges","Apples","Grapes","Banana","Mango"]
# now check if "Apples" in the fruit list or not
if "Apples" in my_fruits:
   print("Apples in the list")
else:
   print("Not in the list")

The set operator will check each item with "Apples" and if not found returns false. If found returns a true. You will print the result based on the output you get.

Boolean Functions

Python has special function associated with different data types or for testing certain conditions. They are called Boolean \hspace{3px} functions because the return either true or false. Consider the following code which checks if a string is digit or not.

str = "23444"
if str.isdigit():
   print("This string is digits")
else:
   print("This is string")

The output of the program is given below.

============== RESTART: C:/Python_projects/Boolean_Functions.py ==============
This string is digits

Boolean Operators

Advertisements

The Boolean operators can check multiple conditions at a time and return either true or false. There are three Boolean \hspace{3px} operators which are given below.

  • and
  • or
  • not
# In the example, we check the price of cake which must be less than $100 
# and must contain chocolate, if true you can buy them, else not buy them
# ===================================================================
cake_price = 90
contains_chocolate = True
# ===================================================================
# check the conditions using Boolean operator AND
# ===================================================================
if cake_price < 100 and contains_chocolate == True:
    print("Go ahead, buy the cake")
else:
    print("Don't buy the cake")

Output of the program is given below.

======== RESTART: C:/Python_projects/Boolean_Operator_Cake_Example.py ========
Go ahead, buy the cake

Bestseller

Python Programming: Using Problem Solving Approach

Python Programming is designed as a textbook to fulfil the requirements of the first-level course in Python programming. It is suited for undergraduate degree students of computer science engineering, information technology as well as computer applications. This book will enable students to apply the Python programming concepts in solving real-world problems. The book begins with an introduction to computers, problem solving approaches, programming languages, object oriented programming and Python programming. Separate chapters dealing with the important constructs of Python language such as control statements, functions, strings, files, data structures, classes and objects, inheritance, operator overloading and exceptions are provided in the book. Each chapter ends with objective-type questions, review questions, programming and debugging exercises to facilitate revision and practice of concepts learnt.

Advertisements
Advertisements
Advertisements