Python Operators

Posted by

Python Operators are the special characters that carry out a certain actions in programming this may be of type arithmetic or logical computations.

They play a key role in programming by solving various problems we face in almost all the programming languages and day to day tasks as well.

Let’s deal with these operators in this blog.

Python operators are symbols that represent operations to be performed on one or more operands. Here are some of the most commonly used operators in Python:

Arithmetic operators :

These operators are used to perform basic arithmetic operations. Examples include + (addition), – (subtraction), * (multiplication), / (division), % (modulus), and ** (exponentiation).

Comparison operators :

These operators are used to compare two values and return a Boolean value (True or False) depending on whether the comparison is true or false. Examples include == (equality), != (inequality), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).

Logical operators :

These operators are used to combine Boolean expressions and return a Boolean value. Examples include and (logical AND), or (logical OR), and not (logical NOT).

Assignment operators :

These operators are used to assign a value to a variable. Examples include = (assignment), += (addition assignment), -= (subtraction assignment), *= (multiplication assignment), /= (division assignment), and %= (modulus assignment).

Bitwise operators :

These operators are used to perform bitwise operations on binary numbers. Examples include & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right shift).

Membership operators :

These operators are used to test whether a value is a member of a sequence (such as a string, list, or tuple). Examples include in (membership test) and not in (negated membership test).

Identity operators :

These operators are used to test whether two objects are the same object in memory. Examples include is (identity test) and is not (negated identity test).

These are just some of the operators available in Python. Understanding how to use them is an important part of programming in Python.

Python operators video tutorial :

Arithmetic Operators :

The basic arithmetic operators are addition, subtraction, multiplication, division and modulus. Let’s try to see few arithmetic operators.

Addition :

x = 1
y = 2

x + y = 3

Subtraction :

x - y = -1

Multiplication :

x * y = 2

Division :

x / y = 0.5

Assignment Operators :

These operators are used to handle assignments now let us try to assign values to variables like

x = 5 
y = 10

Here we have assigned values to values to x and y now try to print x and y

x

5
y

10

Now try to assign the values of y to x

x = y
x 

10

Now try to add x and y because we have assigned y value to x

x + y

20

Now try to add +2 to x

x = x + 2

22

Now try to do the above step in another way i.e., adding

x += 2

24

Now try to subtract in the same way

x -= 10

14

Now try to multiply

x *= 2

28

Relational Operators :

These type of operators are helpful in relating the variables and returning output.We will be seeing 6 types of relational operators with examples.

For example compare x greater than y

x = 1
y = 2
x > y

False

x less than y

x < y

True

x greater than or equal to y

x >= y

False

Now re-assign x value to 2 and compare various scenarios

x = 2
x >= y

True

x <= y

True

x == y

True

Now see x not equal to y

x != y

False

Logical Operators :

Let’s consider a scenarios where you need to pass a exam in subjective (y) and objective (x) tests, so both the conditions to be passed to be TRUE

AND

Both the conditions need to be true to make final result to be true.

x = 10 
y = 20

x > 5 and y > 15

True

Even if one condition fails the final result is FALSE.

x > 10 and y > 15 

now x condition fails

False

OR

Any one of the both conditions needs to be true to make final result to be true.

x > 10 or y > 15

True

When both the conditions fail final result will also be false even in case of or condition.

x > 10 or y > 20

False

NOT :

This will reverse the above scenarios let see example to understand.

not x > 2

False

If you have any query’s in this tutorial on Python Operators do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

Refer the above video tutorial to get clear understanding with examples.

Python Operators

Python Operators
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x