You are on page 1of 14

PYTHON Programming (CST310)

Lecture 6: Operators in Python

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
August 25, 2022
Operators

• Operators are used to perform operations on variables and values.


Operators in Python
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
Operators in Python
Arithmetic operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus (Remainder) x%y
** Exponent x**y
Operators in Python
Python Assignment operators
Assignment operators are used to assign values to variables:
= x=10
+= x+=5 x=x+5
-= x-=5 x=x-5
*= x*=5 x=x*5
/= x/=5 x=x/5
**= x**5 x=x**5
Operators in Python
Python Comparison operators
Comparison operators are used to compare two values.
Output of these expressions is either True or False

== Equal to x==10
!= Not Equal to x!=5
> Greater than x>5
< Less than x<5
>= Greater or Equal to x>=5
<= Less than or equal to x<=5
Operators in Python
Python Logical operators
Logical operators are used to combine conditional statements:

and age>18 and age<60 //returns True if both conditions are true.
or age>18 or age<60
not not(age>18 and age<60)
Boolean Expressions in Python
A boolean expression is an expression that is either true or false.

>>> 5 == 5
In the above example, == is used which compares the value of given
two operands and returns true if they are equal, else False.
Operators in Python
In Python, True and False are equivalent to 1 and 0, respectively.

>>> 0 == False
>>> 1 == True
>>> 2 == True

Use the int() method on a boolean to get its int values.


>>>X= True
>>>Y=False
>>>print(int(X))
>>>print(int(Y))
Numbers as Boolean Values

>>> bool(0) // it will cast the integer to its equivalent Boolean value: False

>>> bool(1) // it will cast the integer to its equivalent Boolean value: True
>>> bool(2) // it will cast the integer to its Boolean value: True
>>>bool(-2) // it will cast the integer to its Boolean value: True
Operators in Python
>>> True + (False / True) // Output?

Bool class is considered as a subclass of int type in Python.


So, you can apply arithmetic operations to Booleans, and you can also compare them to
numbers.

In the above example, True + (False / True) can be expressed as 1 + (0/1) whose output is 1.0
Operators in Python
>>> 17 and True // Output?

the operands of the logical operators should be boolean expressions, but Python is not very strict. Any
nonzero number is interpreted as “true.”

>>> -1 and True // Output?


>>> 0 and True // Output ?
Operators in Python
The not Boolean Operator:
• The only Boolean operator with one argument is not.
• It takes one argument and returns the opposite result:
False for True and True for False.

>>> not True // outputs False


>>> not False // outputs True
>>> not 0 // Outputs True
>>> not 1 // outputs False
>>> not 2 // outputs False
What will be the Output?
>>> 1 == 1
>>> 1 == 2
>>> 1 == 1.2
>>> 1 == 1.0

You might also like