0% found this document useful (0 votes)
4 views18 pages

04 Lecture operators in python 2023-2

Uploaded by

ajm8982
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views18 pages

04 Lecture operators in python 2023-2

Uploaded by

ajm8982
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

PYTHON Programming (CST310)

Lecture 4: Operators in Python

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
August 29, 2024
Operators

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


Operators in Python
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise 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

While casting to Boolean, All other values, including non-zero numbers, non-empty
strings (like "hello"), non-empty lists, and other non-empty collections, are considered
True.
Operators in Python
>>> True + (False / True) // Output?
Operators in Python
>>> True + (False / True) // Output?

Since, arithmetic operator is used, so each bool value is converted implicitly to their Integer
equivalent before performing the operation.

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
Case 1: and operator and First Expression is Truthy Value (True, 1)

Then, output will be the second expression as it is.

Case 2: and operator and First Expression is Falsely Value (False, 0)

Then output will be the first expression as it is

Case 3: or operator and First Expression is Truethy Value

Then output will be the first expression as it is

Case 4: or operator and First Expression is False

Then output will be the second expression as it is


Operators in Python
>>> 17 and True
>>> True and 17
>>> -1 and True
>>> True and -1
>>> 0 and True
>>> 1 or False
>>> False or 1
>>> 1 or False or 18
>>> 1 and False and 18
>>> False or 0 or "“
>>> ‘' or False or 0
>>> True and 10 and "Hello"
>>>False and 20 and "World“
>>>True and False and 1
>>>0 or 100 or "AI"
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
Operators in Python
bitwise operators are used to perform bit-level operations
Python Bitwise operators:
on integers. They manipulate individual bits of data and can be very efficient for
certain types of calculations
Bitwise AND (&): Sets each bit to 1 if both bits are 1.
a=5 # 0b0101
b=3 # 0b0011
result = a & b # 0b0001 -> 1

Bitwise OR (|): Sets each bit to 1 if at least one of the bits is 1.


a=5 # 0b0101
b=3 # 0b0011
result = a | b # 0b0111 -> 7

Bitwise NOT (~): Inverts all the bits (flips 0 to 1 and 1 to 0).
~5 returns output 2

Bitwise XOR (^): Sets each bit to 1 if only one of the bits is 1.

You might also like