DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on Edited on

Python Day 3: Understanding Operators in Python

Operators are symbols or special keywords used to perform operations on values and variables.

For example, we can use operators to add two numbers, compare values, check conditions, and perform many other operations.

Python has six main categories of operators:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Special Operators

1) Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division.

  • Addition
  • Subtraction
  • Multiplication / Division // Floor Division % Modulus Operator ** Exponent Operator Or Power Operator

Example:

no1 = 10
no2 = 2

print(no1 + no2)   # 12
print(no1 - no2)   # 8
print(no1 * no2)   # 20
print(no1 / no2)   # 5.0
print(no1 // no2)  # 5
print(no1 % no2)   # 0
print(no1 ** no2)  # 100
Enter fullscreen mode Exit fullscreen mode

Multiple variable assignment:

We can assign values to multiple variables in a single line.

no1=10, no2=3  // SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
Enter fullscreen mode Exit fullscreen mode
no1, no2 = 10, 3  
print(no1 + no2) // 13
Enter fullscreen mode Exit fullscreen mode
no1=10
no2=3 
print(no1 + no2)  // 13
Enter fullscreen mode Exit fullscreen mode

Operators with strings:

The + operator can join two strings, and the * operator can repeat a string.

name = "Prem "

print(name * 3)  # Prem Prem Prem
print(name + "Kumar")  # Prem Kumar
Enter fullscreen mode Exit fullscreen mode

But we cannot directly add a string and an integer.

name = "Prem"

print(name + 3)  # TypeError
Enter fullscreen mode Exit fullscreen mode

We also cannot use the exponent operator with strings.

print(name ** 3)  # TypeError
Enter fullscreen mode Exit fullscreen mode

Division by zero:

Dividing a number by zero raises a ZeroDivisionError.

no1, no2 = 10, 0  
print(no1/no2)  //ZeroDivisionError:division by zero
print(no1//no2) //ZeroDivisionError:division by zero
Enter fullscreen mode Exit fullscreen mode

Relational Operators

Relational operators are used to compare two values. The result will always be either True or False.

,<, >=, <=,==,!=

Example:

no1, no2 = 10, 20

print(no1 > no2)   # False
print(no1 < no2)   # True
print(no1 >= no2)  # False
print(no1 <= no2)  # True
print(no1 == no2)  # False
print(no1 != no2)  # True
Enter fullscreen mode Exit fullscreen mode

Comparing strings:

Python compares strings using lexicographical order (based on Unicode values).

name1 = 'ajith'
name2 = 'vijay'
print(name1 > name2)  //False
print(name1 < name2)   //True
Enter fullscreen mode Exit fullscreen mode

Python is case-sensitive. Uppercase and lowercase letters have different Unicode values.

For example:

name1 = 'Ajith'
name2 = 'ajith'
print(name1 > name2) //False
Enter fullscreen mode Exit fullscreen mode

The Unicode value of A is 65, while the Unicode value of a is 97.

name1 = 'ajith'
name2 = 'ajithkumar'
print(name1 > name2) //False
Enter fullscreen mode Exit fullscreen mode

Comparing Boolean values:

In Python, True behaves like 1 and False behaves like 0 in numeric comparisons.


print(True > True)  //False
print(False > True)  //False
print(False < True)  //True
Enter fullscreen mode Exit fullscreen mode

True -> Non-zero value it will be 1 or 1.3 or 0.005
False -> Zero

Chained comparisons:

Python allows us to combine multiple comparisons in one expression.

no1, no2, no3 = 100, 200, 300
print(no3 > no2 < no1) //False
print(no3 > no2 > no1) //False
Enter fullscreen mode Exit fullscreen mode

Python evaluates these as two comparisons joined by and.

name1 = 'sanjay'
name2 = 'sanjay'
print(name1 == name2) #Equality Operator //True
print(name1 != name2)  //False
Enter fullscreen mode Exit fullscreen mode

Logical Operators

Logical operators are used to combine conditions or reverse a condition.

Python has three logical operators: and,or,not

AND operator

The and operator evaluates to True only when both conditions are true.
Example:

print(True and True)    # True
print(True and False)   # False
Enter fullscreen mode Exit fullscreen mode

OR Operator

The or operator evaluates to True when at least one condition is true.
Example:


print(True or True)    # True
print(False or True)   # True
print(False or False)  # False
Enter fullscreen mode Exit fullscreen mode

NOT Operator

The not operator reverses the Boolean result.

print(not True)   # False
print(not False)  # True
Enter fullscreen mode Exit fullscreen mode

Important: Logical operators can also work with numbers and strings.

In Python, 0, None, and empty values such as "" are considered falsy. Most other values are truthy.

Also, and and or return one of their operands, not necessarily a Boolean value.

no1,no2=100,200;
print(no1 and no2)  //200
print(100 and 200)  //200
print(True and True)  //True

Enter fullscreen mode Exit fullscreen mode
no1,no2=0,200;
print(no1 and no2)  //0
print(0 and 200)    //0
print(False and True)  //False
Enter fullscreen mode Exit fullscreen mode
no1,no2=100,200;
print(no1 or no2)  //100
print(100 or 200)  //100
print(True or True)  //True
Enter fullscreen mode Exit fullscreen mode

no1,no2=0,200;   
print(no1 or no2)  //200
print(0 or 200)   // 200
print(False or True)  //True
Enter fullscreen mode Exit fullscreen mode
print(not 5) // False
print(not 0)  //True
Enter fullscreen mode Exit fullscreen mode
print(not 'Vishnu')  //False
print(not '')      //True
Enter fullscreen mode Exit fullscreen mode

Bitwise Operators

Bitwise operators perform operations on the binary (bit) representation of integers.
& | ~ >> <<

Bitwise AND (&)

The result is 1 only when both corresponding bits are 1. Otherwise, the result is 0.

print(4 & 5) # 4

Binary calculation:

4 = 100
5 = 101
-----------
    100 = 4
Enter fullscreen mode Exit fullscreen mode

Bitwise OR (|)

The result is 1 when at least one of the corresponding bits is 1.

print(4 | 5) # 5

Binary calculation:

4 = 100
5 = 101
-----------
    101 = 5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)