Python Programming: Operators And Decision Making Statements

In today’s IT market, Python has become one of the most important programming languages that has become the first choice for many startups as well as tech giants to develop their software projects.

Some of the features of Python such as being an open-source, simplicity and availability different frameworks have become the reasons why many developers prefer using this programming language over the others.

Looking at the vast job opportunities provided by Python, the number of IT professionals willing to learn this language is increasing day to day. We found that many of them are searching for the right online platform that helps them to easily learn and build their career as a Python developer.

So, with an aim to help such aspiring people to learn Python programming, we are presenting this blog here that discusses some of the key fundamental concepts of Python programming language.

Before proceeding further, we would like to suggest you read this blog that discusses some of the important concepts of Python such as How to install Python, Basic syntax of Python and many other such valuable concepts.

But if you are a completely new beginner and have no introduction to this programming language, then, we suggest that you first read this blog that provides you a good introduction to Python programming language. You can also watch this YouTube video that discusses about the same concept.

Now, let us start the discussion regarding the important fundamental programming concepts of Python. This blog takes you along a discussion of the two important concepts of Python programming language that are listed below:

  • Python Operators
  • Python Decision making statements.

At first, we will discuss what Python operators are.

Python Operators:

A Python operator is defined as a symbol that is responsible for a particular operation between two operands. An operand is a variable or a value on which we perform the operation.

For example,

10 + 15
25

Here,

  • + symbol is the operator that performs the addition
  • 10 and 25 are the operands
  • 25 is the output.

Python programming language provides a variety of operators such as:

  • Arithmetic operators
  • Comparison operators
  • Assignment operators
  • Logical operators
  • Bitwise operators
  • Membership operators
  • Identity operators.

Now let us discuss each of the operators in detail.

Arithmetic operators:

Arithmetic operators are used to perform mathematical operations between two operands. They perform some of the mathematical operations like addition, subtraction, multiplication, etc.

The below table gives the list of different arithmetic operators and their description.  

Operator Syntax Description
Addition (+) a+b It is used to add two operands.
Subtraction (-) a-b It is used to subtract second operand from the first operand.
Divide (/) a/b It returns the quotient after dividing the first operand by the second operand.
Multiplication (*) a*b It is used to multiple one operand with the other.
Floor division (//) a//b It gives the floor value of the quotient produced by dividing the two operands.
Remainder (%) a%b It returns the reminder after dividing the first operand by the second operand.
Exponent (**) a**b It calculates the first operand power to the second operand.

 The below program will explain how arithmetic operators will work:

a= 20;
b= 10;
print (‘a+b=’, a+b)  
print (‘a-b=’, a-b)
print (‘a*b =’, a*b)
print (‘a/b =’, a/b)
print (‘a//b =’, a//b)
print (‘a**b =’, a**b)

The output of the following program will be as follows:

a+b = 30
a-b = 10
a*b = 200
a/b = 2.0
a//b = 2
a**b = 10240000000000

Comparison operator:

The comparison operators do the task of comparison between two operands. These operators are used to compare the values of two operands and return the Boolean value i.e., true or false, accordingly.

The below table shows the different comparison operators of Python.

Operator Syntax Description
Equal to (==) a==b If the value of two operands are equal, then the condition becomes true.
Not equal to (!=) a!=b If the value of two operands are not equal, then the condition becomes true.
Greater than (>) a>b If the value of left operand is greater than the value of right operand, then the condition becomes true.
Less than (<) a If the value of right operand is left operand is less than the right operand, then the condition becomes true.
Greater than or equal to (>=) a>=b If the value of left operand is greater than the value of right operand, then the condition becomes true.
Less than or equal to (<=) a<=b If the value of the left operand is less than or equal to the value of right operand, then the condition becomes true.

The below program will explain how comparison operators work in Python.

a=20
b=22
print (‘a==b is’, a==b)
print (‘a !-b is’, a!=b)
print (‘a>b is’, a>b)
print (‘a<b is’, a<b)
print (‘a>=b is’, a>=b)
print (‘a<=b is’, a<=b)

The output of the following program will be as follows:

a==b is False
a!=b is True
a>b is False
a<b is True
a>=b is False
a<=b is True

Assignment operators:

An assignment operator is used to assign a value to a variable. It assigns the value of right expression to the left operand. Here, sometimes, assignments are done directly, and sometimes, the operator initially performs some mathematical operations and then assigns the value to the operand.

The below table shows the different assignment operator.

Operator Description
Assign (=) It assigns the value of the right expression to the left operand.
Add and Assign (+=) It increases the value of left operand by the value of the right operand and assigns the modified value back to the left operand.
Subtract and Assign (-=) It decreases the value of the left operand by the value of the right operand and assigns the modified value back to the left operand.
Multiply and Assign (*=) It multiples the value of the left operand by the value of the right operand and assigns the modified value back to the left operand.
Divide and Assign (/=) It divides the value of the left operand with the right operand and assigns the result to the left operand.
Modulus and Assign (%=) It divides the value of the left operand by the value of the right operand and then assigns the value back to the left operand.
Exponent and Assign (**=) It performs exponential (power) calculation on operators and assigns the value to the left operand.
Floor-Divide and Assign (//=) It performs the floor division on the values on either side. It then assigns it to the expression on the left.

The following program will explain how the Assignment operators work in Python.

x = 11
y = 20
z = x+y
print (“Line 1 – Value of z ”, z)
z += x
print (“Line 2 – Value of z ”, z)
z -=x
print (“Line 3 – Value of z ”, z)
z *=x
print (“Line 4 – Value of z ”, z)
z/=x
print (“Line 5 – Value of z ”, z)
z %=x
print (“Line 6 – Value of z ”, z)
z **=x
print (“Line 7 – Value of z ”, z)
z//=x
print (“Line 8 – Value of z ”, z)

The output of the above program is as follows:

Line 1 – Value of z 31
Line 2 – Value of z 42
Line 3 – Value of  z 31
Line 4 – Value of z 341
Line 5 – Value of z 31.0
Line 6 – Value of z 9.0
Line 7 – Value of z 31381059609.0
Line 8 – Value of z 2852823600.0

Logical operators

Logical operators are used primarily in the expression evaluation to make a decision. Python logical operators are and, or, not

The below table shows the relevant logical operators used in the Python.

Operator Syntax Description
and a and b If the conditions on both sides of operator are true, then the expression as a whole is true.
or a or b The expression is false only if both the statements around the operator are false. Otherwise it is true.
not not a This inverts the Boolean value of an expression. It converts True to False, and False to True.

The following is the truth table of logical operators.

1. Truth table of and operator is given below. This operator will result into True only if both the operands are found to be True. Otherwise, it will result into False.

A B A or B
True True True
True False False
False True False
False False False

2. Truth table of or operator is given below. This operator will result into True if any one of the operands is True.  Otherwise, it will result into False.

A B A or B
True True True
True False True
False True True
False False False

3. Truth table of not operator is given below.

A not A
True False
False True

Bitwise operators

Bitwise operators works on bits and performs bit by bit operations. Python provides bitwise operators such as Binary AND, Binary OR, Binary XOR, etc.

The below table shows the list of Python Bitwise operators.

Operator Syntax Description
Binary AND (&) a&b It performs bit by bit operation on the two values. If both the bits are 1, then output will be 1; otherwise 0.
Binary OR (!) a|b If any one of bits is 1, then output is 1; otherwise 0.
Binary XOR (^) a^b If both the bits are the same, then 0; otherwise 1.
Binary Complement (~) ~a If the bit is 1, then output will be 0; if the bit is 0, then output will be 1.
Binary Left shift (<<) a< The left operand is moved left by the number of bits specified by the right operand.
Binary Right Shift (>>) a>>b The left operand is moved right by the number of bits specified by the right operand.

The below program shows how the comparison operators are used in Python.

a = 75
b = 24
c = 0
c  = a &b;
print (“Line 1 – Value of c is ”, c)
print (“Line 2 – Value of c is ”, c)
print (“Line 3 – Value of c is ”, c)
print (“Line 4 – Value of c is ”, c)
print (“Line 5 – Value of c is ”, c)
print (“Line 6 – Value of c is ”, c)

The output of the above program will be as follows:

Line 1– Value of c is 8
Line 2 – Value of c is 91
Line 3 – Value of c is 83
Line 4 – Value of  c is -76
Line 5 – Value of c is 300
Line 6 – Value of c is 18

Membership operators

Python membership operators are used to check whether a value is a member of a   sequence. Here the sequence may be a list, a string or a tuple.

There are two membership operators – ‘in’ and ‘not in’.

Operator Description
in It evaluates to true if it finds a variable in the specified sequence. Otherwise, to false.
not in It evaluates to true if it does not find a variable in the specified sequence. Otherwise, to false.

The following program will explain how Membership operators work in Python.

a = “online education”
b = {1:’x’, 2:’y’}
print (“o” in a)
print (“online” not in a)
print (1 in b)
print (‘y’ in b)

Once we execute the above program we will get the following output:

True
False
True
False

Identity operators

Python provides two types of identity operators. These operators are used to compare the memory locations of two objects.

The below table shows the identity operators in Python.

Operator Description
is It is evaluated to be true if the operands are identical
is not It is evaluated to be true if the operands are not identical.

The below program shows how the identity operators work in Python.

x1= 8
y1= 8
x2 = “Developer”
y2 = “Developer”
x3 = [4,5,6]
y3 = [4,5,6]
print(x1 is not y1)
print(x2 is y2)
print(x3 is y3)

 The above program will provide the following output:

False
True
False

Operator precedence

The precedence of operators is very important as it decides which operators need to be evaluated first. The below table shows the precedence of operators in Python:

Sr.No Operator Description
1 ** Exponentiation (raise to the power)
2 ~+- The negation, unary plus and minus
3 * / % // Multiplication, Divide, Modulus and Floor division
4 + – Addition and Subtraction
5 >> << Left shift and Right shift
6 & Binary and
7 ^ | Binary xor and or
8 <= <> >= Comparison operators
9 <> == != Equality operators
10 = %= /= //= -= += *= **== Assignment operators
11 is is not Identity operators

Now let us discuss about the next topic of this blog i.e. Python Decision Making.

Python Decision Making:

Decision making is one of the important aspects of any programming language. These are required whenever there is a need to execute a code only if a certain condition is satisfied. The flow of execution of a program can be decided with the help of these statements.

Python provides a few decision making statements and they are as follows.

Sr.No Statement Description
1 if Statement Here, the program will evaluate the test expression and will execute the statements only if the text expression is True. Otherwise, if the text expression is False, the statement is not executed.
2 if – else statement Here, an if statement is followed by an optional else statement. This statement executes when the Boolean expression is False.
3 if elif else statement This statement allows the programmers to check the multiple statements.

1) if statement: Sometimes, in a program, there will be a need to take a decision based on a condition. Here the expression’s value can either be True or False. For example, assume two variables x and y. If x is greater, then we want the output to be as “x is greater” or else the output to be as “y is greater”.

To get these results we use an if-statement.

The flow chart of the if statement is as shown below:

The syntax of if statement is given below:

if expression;
statement

The following program shows the working of if statement in Python.

# This program checks which number is greater
# And displays appropriate message

x = 30
y = 40

if x < y;

print (“y is greater”)

The output of this program will be as follows:

y is greater

In the above program, we can understand how the if statement works. When the if statement comes, the program checks whether the value of y is greater or not. If y is greater, it gives the output as y is greater.

Now if the condition becomes false, it exits the if statements and then executes the next statements. Here to print the next statement, an else statement is used.

2) if-else statement

Now in the program, if the given condition becomes false, the program exits the if   statements and then executes the next statements. Here, to print the next statements, an else statement is used.

In if-else statement, an else block will be present in addition to if statement. Whenever the if condition becomes false this block gets executed. If the condition is true if-block is executed, otherwise else-block will be executed.

Flow chart of if-else statement is as shown below

The syntax of if-else statement is given below.

if expression;   
    Body of if
else:
    Body of else

The following program shows the working of if-else statement

# This program is to check which number is greater
# And displays appropriate message

x= 50
y= 40

if x < y;
print(“y is greater number”);
else:
print(“x is greater number”)

The output of this program will be as follows:

y is greater number

In the above program, we can understand how the if-else statements work. When the if statement comes, the program checks whether the value of y is great or not. If y is the greater number, it gives the output as y greater number.

Now suppose the value of y is less than the value of x, program skips the if statements and checks for else statement, and prints x as greater number.

3) If elif else statement

Here, elif is short form of else if statement. This conditional statement allows the programmers to check multiple statements.

Suppose the condition of if statement is false, then the program will go into elif block (else if) i.e. basically another if statement. Again, suppose the condition found to be true, the program will execute the body of the elif statement, and if the condition is found to be false, the program will move to the next else statement and execute the body of the else block.

Here only one block among the many if….elif….else block will be executed according to the condition.

Flow chart of if….elif….else statement is as shown below

The syntax of if….elif….else statements is given below.

if text expression:
Body of if
elif test expression:
Body of elif
else:
Body of else

The following program will show the working of if…elif…else statement.

x=100
if (x == 10);
print (“value of variable x is 10”)
elif (x  == 20);
print (“value of variable x is 20”)
elif (x == 30);
print (“value of variable x is 30”)
else:
print (“value of variable x is greater than 30”)

The output of the following program will be as follows:

value of variable x is greater than 30

Here, in the above example, the program initially checks the if statement. Suppose it finds the condition to be true, it executes the if statement, but as in the above example, if the value of if statement is false, the program skips if statement and moves into checking elif statement. Since the elif statement turns out to be false, now the programs moves into check the next elif statement. As second elif statement turns out to be false, the program skips the second elif statement as well, and moves into the last statement i.e., else statement and executes it.

So, in this program, the output will be “value of variable x is greater than 30”.

This is about the Python Decision making statements. These Decision making    statements allow the programmers to write the code efficiently.

Conclusion:

This has been a guide to discuss some of the important fundamental concepts of Python programming i.e., Operators and Decision Making Statements. We hope that our readers have found some valuable information reading this blog.

If you have any questions with regards to the above discussed topics, please feel free to share them in the comment section below. And if you think some more points need to be supplemented to this blog, please let us know them through the comment section.

In the next blog we will be discussing some more concepts of Python programming.

In addition to all the above information, we here at this point like suggest you to visit these online courses of Python provided by Simpliv that contains all the valuable courses to master this programming language.

If you think this blog has been successful in helping you to find some relevant information about Python programming, then we request you to please share it with you circles, so that it can reach to someone who is looking for similar kind of information.

5 Key Takeaways

  1. An arithmetic operator is used to perform mathematical operation between two operands.
  2. Python Membership Operators are used to check whether a value is a member of a sequence. Here the sequence may be a list, string or a tuple.
  3. Python decision making statements helps to decide the flow of execution of the program.
  4. elif statement is the short form of else if statement. This conditional is used to check multiple statements.
  5. The Logical and operator returns True only if the both the operands are found to be true otherwise it returns False.

Recommended blogs for you

Add a Comment

Please Rate This Post*

Your email address will not be published. Required fields are marked *