Chapter 2 PWP
Chapter 2 PWP
& Binary AND Operator copies a bit to the result (a & b) (means 0000
if it exists in both operands 1100)
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.
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'
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.
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.
sum = 0
i=1
while i <= n:
sum = sum + i
i = i+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.
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…!!!