Python operators

An operator in python is a symbol or a keyword that helps to perform calculations, perform comparisons on the variables, relate between results and helps to store the calculation/comparison result within another variable.

For example:

first=7

second=8

result=first*second

Here, first, second, result are the variables, in which first stores value 7, second stores value 8. Then, in the result, we are using * symbol which is basically referred as multiplication arithmetic operator. It multiplies first and second values and stores in the result. The equals (assignment operator) helps to save value to the result variable in this case.

In this tutorial, We will learn about various basic types of python operators. Also, I encourage you to open the python terminal and experiment with the given codes for better understanding.

Operand

If we use variable with any operator, those variables are called as an operand. Take above example of multiplication, in the above example, first and second variable is called as an operand because it is used with multiplication symbol.

Expressions

Combination of operand and an operator is called as an expression. In the above example gave in the operator definition, “first*second” is called as an expression. Because, first and second were the operands and * was the operator.  Don’t confuse between variable and operands! Variable is the feature available in programming to store data, whereas operands are used with the operators either as a direct value or indirect value by referring a variable name.

Keywords

Keywords are provided in all high level programming languages. They are provided only as an English word. We use keywords in programming to validate data, used to check the relations between the operands because some keywords are also operators, Manage execution of the program instructions (Generally program instructions are executed one line after the other. We can change execution way using some keywords).

To retrieve entire keyword list that are available in python, visit https://docs.python.org/2.5/ref/keywords.html

Types of operators

Assignment operator (=)

It is used to assign any value to a variable (save values to variable).

Example:

Day=”Sunday”

This will create a new variable called day, and stores the value called “Sunday” inside it.

Arithmetic operators

Arithmetic type of operator is used to perform basic arithmetic operations on the variables. This operator will calculate and return result in the form of number.

Arithmetic addition (+)

It is used to add the values.

Example:

Result=6+8

# adds 6+8 and stores 14 in the result variable

Arithmetic subtraction (-)

It is used to subtract the values.

Example:

Result=5-5

# subtracts 5-5 and stores 0 in the result variable.

Arithmetic multiplication (*)

It is used to multiply the values.

Example:

result=5*5

# multiplies 5*5 and stores 25 in the result variable.

Arithmetic division (/)

It is used to divide the values and obtain result in the form of float type.

Example:

Result=5/2

# divides 5/2 and stores 2.5 in the result variable.

Arithmetic modulus (%)

It divides the value and gives the reminder as the result.

Example:

Result=5%2

# Divides 5/2, find’s the value 1 as the reminder and store’s the reminder value in result variable.

Arithmetic exponential (**)

It is used to find the powers of given 2 values (Example 2**3 will give the result of 8)

Arithmetic floor (//)

It is also used to divide and obtain the result. The difference is, when you use this operator with integer data, it will provide integer result, otherwise it will provide the result in the form of float.

Comparison operators

This type of operator is used to perform comparision on the variable (Operands) and return the true or false result. Depending on the true or false result, we will take necessary action (write instruction for true and false result). Various types of comparison operators are explained below for your benefit.

!= (Not equal to operator, defined with exclamation mark and equals symbol)

Syntax:

A!=b.

It checks whether a is not equal to be? If a is not equal to b, it will return True value else it will return False value.

== (equal to operator, defined with double equals symbol)

It checks whether first operand’s value is equal to second operand’s value and will return the True/False result respectively.

< (Less than operator)

It checks whether first operand’s value is lesser than the second operand’s value and returns the True/False result respectively.

<= (Less than or it may be equal to operator)

It checks whether the first value is less than or it is equal to second value and returns the True/False respectively.

> (Greater than)

It checks whether first value greater than the second value and returns the True/False result respectively.

>= (Greater than or equal to)

It checks whether the first value is greater or equal to the second value and returns the True/False result respectively.

Logical/Relational operators

It is used to relate 2 or more comparison operator or any Boolean type of result. By combining those results with one of the following logical operator, we may obtain the true or false result. Below are the basic logical/relational types of operators.

And operator (and)

In python, “and” operator is represented by using the keyword “and” without the quotation mark.

Example 1:

True and False

Example 2:

1+5<10 and 2*0>0

When “and” operator relates results of 2 comparison operator or 2 boolean results, the result will be following truth table:

Truth table of and operator
First boolean resultSecond boolean resultFinal result of and operator
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Or operator (or)

In python, “or” operator is represented by using the keyword “or” without the quotation mark.

Example 1:

True or False

Example 2:

Day=”Sunday” # initialized a day variable with value of Sunday.

Day==”Saturday” or day==”Sunday”

When “or” operator relates results of 2 comparison operator or 2 boolean results, the result will be following truth table:

Truth table of or operator
First boolean resultSecond boolean resultFinal result of or operator
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Not operator (not)

In python, “not” operator is represented by using the keyword “not” without the quotation mark.

Example 1:

not False

Example 2:

day=”Sunday” # initialized a day variable with value of Sunday.

not Day==”Sunday”

The “not” operator only evaluates one comparison result or one boolean result. In simple words, We can say that, this operator helps us to reverse any Boolean result’s value. “not” of True value will become false and wise versa. Truth table for not is as follows:

Truth table of not operator
boolean valueResult of Not operator
TrueFalse
FalseTrue

Concatination operator (+)

If we want to join some variable value with a string, we use concatenation operator.

example:

name1=”Ramesh”

name2=”Suresh”

s=name1+” and “+name2+” are good friends!”

This will concat a and b variable to the s variable of string type and the final data of s is “Ramesh and Suresh are good friends!”
In the above instructions, you can observe that, if you use + operator with int type of data, it will result in addition. If you use plus operator with any string, it will result in appending the string data.

Membership operator (in)

In python, membership operator is represented by using the keyword “in” without the quotation mark. The membership operator evaluates whether left hand side data exist as a part of right hand side data. The membership operator takes 2 operands/values and returns a Boolean result.

Example:

sentence=”today is Monday” # stores “today is Monday” inside sentence variable

result=”Tuesday” in sentence

# stores False value in result variable because, Tuesday word not exist in sentence variable’s data.

Note:

Membership operator can only used with the data types of string, collection types such as list, tuples, sets etc which you will learn collection types in future.

Published by Harsha HD

Myself Harsha, I'm a person from Bangalore, Karnataka with 100% visually challenged. I completed my PG in masters of computer application at Bangalore university in the year 2022. With my coding interest along with interest of sharing those knowledge to our visually challenged community, I founded this website, trying to provide accessible coding materials and training specially for visually challenged people with affordable cost. So, that the VI people will able to perceive their education in technical/electronics stream in an accessible way.

Leave a Reply

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