0% found this document useful (0 votes)
13 views38 pages

Chapter 2 PWP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views38 pages

Chapter 2 PWP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

By,

Miss. Saniya Shaikh


🞂 Operators are the constructs which can manipulate
the value of operands.

🞂 Consider the expression 4 + 5 = 9. Here, 4 and 5


are called operands and + is called operator.
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Operator Description Example

+ Addition Adds values on either side of the a + b = 30


operator.
- Subtraction Subtracts right hand operand a – b = -10
from left hand operand.

* Multiplication Multiplies values on either side a * b = 200


of the operator

/ Division Divides left hand operand by b/a=2


right hand operand

% Modulus Divides left hand operand by b%a=0


right hand operand and returns
remainder

** Exponent Performs exponential (power) a**b =10 to


calculation on operators the power 20
🞂 These operators compare the values on either
sides of them and decide the relation among
them. They are also called Relational
operators.
== If the values of two operands are equal, then (a == b) is
the condition becomes true. not true.
!= If values of two operands are not equal, then (a != b) is
condition becomes true. true.
<> If values of two operands are not equal, then (a <> b) is
condition becomes true. true. This is
similar to
!= operator.
> If the value of left operand is greater than the (a > b) is
value of right operand, then condition becomes not true.
true.
< If the value of left operand is less than the (a < b) is
value of right operand, then condition becomes true.
true.
>= If the value of left operand is greater than or (a >= b) is
equal to the value of right operand, then not true.
condition becomes true.
<= If the value of left operand is less than or equal (a <= b) is
to the value of right operand, then condition true.
becomes true.
Operator Description Example

= Assigns values from right c = a + b assigns


side operands to left side
value of a + b into c
operand
+= Add AND It adds right operand to the c += a is equivalent to
left operand and assign the c=c+a
result to left operand
-= Subtract It subtracts right operand
AND from the left operand and c -= a is equivalent to
assign the result to left c=c-a
operand
*= Multiply AND It multiplies right operand
with the left operand and c *= a is equivalent to
assign the result to left c=c*a
operand
/= Divide AND It divides left operand with
the right operand and
assign the result to left c /= a is equivalent to
operand c=c/a

%= Modulus AND It takes modulus using two


operands and assign the c %= a is equivalent to
result to left operand c=c%a

**= Exponent AND Performs exponential


(power) calculation on
operators and assign value c **= a is equivalent to
to the left operand c = c ** a

//= Floor Division It performs floor division


on operators and assign c //= a is equivalent to
value to the left operand c = c // a
a = 0011 1100
b = 0000 1101

1. a&b = 0000 1100


2. a|b = 0011 1101
3. a^b = 0011 0001
4. ~a = 1100 0011
Operator Description Example

& Binary AND Operator copies a bit to the result (a & b) (means 0000
if it exists in both operands 1100)

| Binary OR It copies a bit if it exists in either (a | b) = 61 (means


operand.
0011 1101)

^ Binary XOR It copies the bit if it is set in one (a ^ b) = 49 (means


operand but not both. 0011 0001)

~ Binary Ones (~a ) = -61 (means


Complement It is unary and has the effect of 1100 0011 in 2's
complement form
'flipping' bits.
due to a signed
binary number.

<< Binary The left operands value is moved a << 2 = 240


Left Shift left by the number of bits
(means 1111 0000)
specified by the right operand.
>> Binary The left operands value is moved a >> 2 = 15 (means
Right Shift right by the number of bits
0000 1111)
specified by the right operand.
Operator Description Example

and Logical AND If both the operands are (a and b) is true.


true then condition
becomes true.

or Logical OR If any of the two operands (a or b) is true.


are non-zero then
condition becomes true.

not Logical NOT Used to reverse the logical Not(a and b) is false.
state of its operand.
🞂 Python’s membership operators test for
membership in a sequence, such as strings,
lists, or tuples.

🞂 There are two membership operators as


explained below −
Operator Description Example

in Evaluates to true if it finds a


variable in the specified
sequence and false otherwise. x in y, here in results
in a 1 if x is a member
of sequence y.

not in Evaluates to true if it does not x not in y, here not in


finds a variable in the specified results in a 1 if x is not
sequence and false otherwise. a member of sequence
y.
Operator Description Example

is Evaluates to true if the variables


on either side of the operator
point to the same object and x is y, here is results in
false otherwise. 1 if id(x) equals id(y).

is not Evaluates to false if the


variables on either side of the x is not y, here is
operator point to the same not results in 1 if id(x)
object and true otherwise. is not equal to id(y).
Sr.No. Operator & Description

1 **
Exponentiation (raise to the power)

2 ~+-
Complement, unary plus and minus (method names for the
last two are +@ and -@)

3 * / % //
Multiply, divide, modulo and floor division

4 +-
Addition and subtraction

5 >> <<
Right and left bitwise shift
6 &
Bitwise 'AND'

7 ^|
Bitwise exclusive `OR' and regular `OR'

8 <= < > >=


Comparison operators

9 <> == !=
Equality operators

10 = %= /= //= -= += *= **=
Assignment operators

11 is is not
Identity operators

12 in not in
Membership operators

13 not or and
Logical operators
➢ Python if Statement is used for decision-making
operations.

➢ It contains a body of code which runs only when the


condition given in the if statement is true. If the
condition is false, then the optional else statement runs
which contains some code for the else condition.
➢ Syntax:
if expression
:Statement
else
Statement
a=6

if (a==1):
print("One")
elif a==2:
print("Two")
elif a==3:
print("Three")

elif a==4:
print("Four")

else:
print("Wrong input")
➢ The while loop in Python is used to iterate over a block of
code as long as the test expression (condition) is true.

➢Syntax of while Loop in Python


while test_expression:
Body of while
n = 10

sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1

print("The sum is", sum)


counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1

else:
print("Inside else")
🞂 The for loop in Python is used to iterate over
a sequence (list, tuple, string) or other
iterable objects. Iterating over a sequence is
called traversal.

🞂Syntax of for Loop


for val in sequence:
Body of for
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)
🞂 We can generate a sequence of numbers
using range() function. range(10) will generate
numbers from 0 to 9 (10 numbers).
🞂 We can also define the start, stop and step size
as range(start, stop,step_size). step_size defaults
to 1 if not provided.
1. print(range(10))
2. print(list(range(10)))
3. print(list(range(2, 8)))
4. print(list(range(2, 20, 3)))
🞂 A for loop can have an optional else block as well.
The else part is executed if the items in the sequence used in
for loop exhausts.
🞂 The break keyword can be used to stop a for loop. In such
cases, the else part is ignored.
🞂 Hence, a for loop's else part runs if no break occurs.

🞂 Here is an example to illustrate this.


digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]0.
for x in adj:
for y in fruits:
print(x, y)
➢ Output:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
🞂 Continue is also a loop control statement just
like the break statement. continue statement
is opposite to that of break statement,
instead of terminating the loop, it forces to
execute the next iteration of the loop.
for letter in 'Python':
if letter == 'h':
continue
print ('Current Letter :', letter)

Output:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
🞂 The pass statement in Python is used when a
statement is required syntactically but you do
not want any command or code to execute.
🞂 The pass statement is a null operation;
nothing happens when it executes.
The pass is also useful in places where your
code will eventually go, but has not been
written yet
for letter in 'Python':
if letter == 'h':
pass
print ('This is pass block‘)
print ('Current Letter :', letter)
print ("Good bye!“)

Ouyput:
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!
🞂 The break statement in Python terminates the
current loop and resumes execution at the
next statement, just like the traditional break
found in C.
🞂 The most common use for break is when
some external condition is triggered
requiring a hasty exit from a loop.
The break statement can be used in
both while and for loops.
for letter in 'Python':
if letter == 'h':
333break
print ('Current Letter :', letter)

Output:
Current Letter : P
Current Letter : y
Current Letter : t
🞂 Python supports to have an else statement
associated with a loop statements.
1. If the else statement is used with a for loop,
the else statement is executed when the
loop has exhausted iterating the list.
2. If the else statement is used with
a while loop, the else statement is executed
when the condition becomes false.
for num in range(10,20):
for i in range(2,num):
if num%i == 0:
j=num/i
print ('%d equals %d * %d' % (num,i,j))
break
else:
print (num, 'is a prime number')

Output:
10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 equals 3 * 5
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number
Thank You…!!!

You might also like