0% found this document useful (0 votes)
4 views

UNIT1 Python Operators

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

UNIT1 Python Operators

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

ARTIFICIAL INTELLIGENCE USING PYTHON

VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR


WOMEN(AUTONOMOUS)
PG AND RESEARCH DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS

PYTHON OPERATORS

COURSE MATERIAL TAKEN FROM NPTEL LECTURE NOTES


In this
lecture
⚫Operators and operands
⚫Different types of
operators
◦ Arithmetic
◦ Assignment
◦ Relational or comparison
◦ Logical
◦ Bitwise
⚫Precedence of operators
Python for Data Science 2
Operators and
operands
⚫Operators are special symbols
that help in carrying out an
assignment operation or
arithmetic or logical computation
⚫Value that the operator operates
on is called operand

Python for Data Science 3


Arithmetic
operators
⚫Used to perform mathematical operations between
two operands
⚫Create two variable a and b with values 10 and 5
respectively
Symbol Operation Example

+ Addition

Python for Data Science 4


Arithmetic
operators
⚫Used to perform mathematical operations between
two operands
⚫Create two variable a and b with values 10 and 5
respectively
Symbol Operation Example

+ Addition

- Subtraction

Python for Data Science 5


Arithmetic
operators
Symbol Operation Example

* Multiplication

Python for Data Science 6


Arithmetic
operators
Symbol Operation Example

* Multiplication

/ Division

Python for Data Science 7


Arithmetic
operators
Symbol Operation Example

* Multiplication

/ Division

% Remainder

Python for Data Science 8


Arithmetic
operators
Symbol Operation Example

* Multiplication

/ Division

% Remainder

** Exponent

Python for Data Science 9


Hierarchy of arithmetic
operators
Decreasing order of
Operation A = 7 – 2 x 𝟐𝟕 + 𝟒
precedence 𝟑𝟐

Parentheses ()
Exponent **
Division /
Multiplication *

Addition and subtraction +,-

Python for Data Science 10


Assignment
operators
⚫Used to assign values to
variables
Symbol Operation Example

Assign values from right side operands to


=
left side operand

Python for Data Science 11


Assignment
operators
⚫Used to assign values to
variables
Symbol Operation Example

Assign values from right side operands to


=
left side operand

Adds right operand to left operand and


+=
stores result on left side operand
(a=a+b)

Python for Data Science 12


Assignment
operators
⚫Used to assign values to
variables
Symbol Operation Example

Assign values from right side operands to


=
left side operand

Adds right operand to left operand and


+=
stores result on left side operand
(a=a+b)
Subtracts right operand from left
-=
operand and stores result on left side
operand (a=a-b)

Python for Data Science 13


Assignment
operators
Symbol Operation Example

Multiplies right operand from left


*=
operand and stores result on left side
operand (a=a*b)

Python for Data Science 14


Assignment
operators
Symbol Operation Example

Multiplies right operand from left


*=
operand and stores result on left side
operand (a=a*b)
Divides right operand from left operand and
/=
stores result on left side operand
(a=a/b)

Python for Data Science 15


Relational or comparison
operators
⚫ Tests numerical equalities and inequalities between two operands and returns
a boolean value
⚫ All operators have same precedence

⚫ Create two variables x and y with values 5 and 7 respectively

Symbol Operation Example

< Strictly less than

Python for Data Science 16


Relational or comparison
operators
⚫ Tests numerical equalities and inequalities between two operands and returns
a boolean value
⚫ All operators have same precedence

⚫ Create two variables x and y with values 5 and 7 respectively

Symbol Operation Example

< Strictly less than

<= Less than equal to

Python for Data Science 17


Relational or comparison
operators
Symbol Operation Example

> Strictly greater than

>= Greater than equal to

Python for Data Science 18


Relational or comparison
operators
Symbol Operation Example

> Strictly greater than

>= Greater than equal to

== Equal to equal to

Python for Data Science 19


Relational or comparison
operators
Symbol Operation Example

> Strictly greater than

>= Greater than equal to

== Equal to equal to

!= Not equal to

Python for Data Science 20


Logical
operators
⚫ Used when operands are conditional statements and returns boolean
value
⚫ In python, logical operators are designed to work with scalars or
boolean
Symbol values Operation Example

or Logical OR

Python for Data Science 21


Logical
operators
⚫ Used when operands are conditional statements and returns boolean
value
⚫ In python, logical operators are designed to work with scalars or
boolean
Symbol values Operation Example

or Logical OR

and Logical AND

Python for Data Science 22


Logical
operators
⚫ Used when operands are conditional statements and returns boolean
value
⚫ In python, logical operators are designed to work with scalars or
boolean
Symbol values Operation Example

or Logical OR

and Logical AND

not Logical NOT

Python for Data Science 23


Bitwise
operators
⚫Used when operands are
integers
⚫Integers are treated as a string
of binary digits
⚫Operates bit by bit
⚫Can also operate on conditional
statements which compare
scalar values or arrays
⚫Bitwise OR (|), A N D(&)

Python for Data Science 24


Bitwise
operators
Create two variables x and y with values 5 and 7 respectively

⚫Binary code for 5 is 0000 0101 and for 7 is 0000 0111


⚫0 corresponds to False and 1 corresponds to True
⚫In bitwise OR ( | ), operator copies a bit to the result if it
exists in either operand
⚫In bitwise A N D (& ), operator copies a bit to the result if it
exists in both operands

Python for Data Science 25


Bitwise OR on
integers
Code and output in
console

Binary code for 5 Binary code for 7

0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1

⚫0 present in corresponding positions, therefore resultant


cell is also 0

Python for Data Science 26


Bitwise OR on
integersBinary code for 5 Binary code for 7

0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1

0 0 0 0 0 1

⚫0 present in positions 2-5, therefore resultant cell


will also contain 0
⚫In the 6th position, 1 is present in both operands
and hence resultant will also contain 1

Python for Data Science 27


Bitwise OR on
integers
⚫The 7th position has 0 in the first operand and 1 in the
second Binary code for 5 Binary code for 7

0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1

⚫Since this is an OR operator, only the True condition is


considered
0 0 0 0 0 1 1

Binary code for 5 Binary code for 7

0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1

0 0 0 0 0 1 1 1
Python for Data Science 28
Bitwise
operators
⚫Bitwise operators can also operate on
conditional statements
Symbol Operation Example

| Bitwise OR

Python for Data Science 29


Bitwise
operators
⚫Bitwise operators can also operate on
conditional statements
Symbol Operation Example

| Bitwise OR

& Bitwise AND

Python for Data Science 30


Precedence of
operators
Decreasing order
Operation
of precedence
Parentheses ()
Exponent **
Division /
Multiplication *
Addition
+,-
and
subtraction
Bitwise AND &

Python for Data Science 31


Precedence of
operators
Decreasing order
Operation
of precedence
Bitwise OR |
Relational/
==, !=, >, >=, <,
compariso
<=
n
operators
Logical NOT not
Logical AND and
Logical OR or

Python for Data Science 32


Summar
y ⚫
Important
operators
◦ Arithmetic
◦ Assignment
◦ Relational
◦ Logical
◦ Bitwise

Python for Data Science 33


THANK YOU

You might also like