pwp notes 2
pwp notes 2
STATEMENTS
Q.1.Explain membership and Identity operators in Python - 4mks
Q.2.Explain Bitwise operator in Python with appropriate example - 4mks
Q.3.List identity operators in python - 2 mks
Q.4.Explain membership and assignment operators with example - 4mks
Q.5.Give membership operators in python - 2 mks
8. Order of Python Operator Precedence (Highest to Lowest)
1. Parentheses (()):
○ Highest precedence. Expressions inside parentheses are evaluated first.
○ Example: 3 * (2 + 5) → First evaluate (2 + 5), then multiply by 3.
2. Exponentiation (**):
○ Used to raise a number to a power.
○ Example: 2 ** 3 → Evaluated as 8.
3. Unary Plus/Minus and Bitwise NOT (+, -, ~):
○ Unary Plus (+) and Unary Minus (-) are used to indicate positive or negative
numbers.
○ Bitwise NOT (~) inverts the bits of the operand.
○ Example: -5, +10, ~3.
4. Multiplication, Division, Floor Division, Modulus (*, /, //, %):
○ Multiplication (*), Division (/), Floor Division (//), and Modulus (%) operators
have the same precedence.
○ Example: 2 * 3, 5 // 2, 5 % 2.
5. Addition and Subtraction (+, -):
○ Addition (+) and Subtraction (-) have lower precedence than multiplication,
division, and modulus.
○ Example: 5 + 3, 7 - 2.
6. Bitwise Shift (<<, >>):
○ These operators perform left and right shifts of bits.
○ Example: 5 << 1 (left shift) and 5 >> 1 (right shift).
7. Bitwise AND (&):
○ This operator performs a bitwise AND operation.
○ Example: 5 & 3 (binary 101 & 011 → 001).
8. Bitwise XOR (^):
○ This operator performs a bitwise XOR (exclusive OR) operation.
○ Example: 5 ^ 3 (binary 101 ^ 011 → 110).
9. Bitwise OR (|):
○ This operator performs a bitwise OR operation.
○ Example: 5 | 3 (binary 101 | 011 → 111).
10. Comparison Operators (==, !=, <, >, <=, >=):
○ Used to compare two values.
○ Example: x == y, a > b.
11. Logical NOT (not):
○ NOT is used to invert the boolean value.
○ Example: not True → False.
12. Logical AND (and):
○ AND returns True if both conditions are True.
○ Example: True and False → False.
13. Logical OR (or):
○ OR returns True if at least one condition is True.
○ Example: True or False → True.
14. Identity Operators (is, is not):
○ Used to check if two variables point to the same object in memory.
○ Example: x is y, a is not b.
15. Membership Operators (in, not in):
○ Used to check if a value exists in a sequence (like a list, tuple, or string).
○ Example: x in list, y not in tuple.
16. Assignment Operators (=, +=, -=, *=, /=, etc.):
○ Lowest precedence. These are used to assign values to variables.
○ Example: x = 5, y += 3, z *= 2.
1. if Statement
The if statement is used to test a condition, and if the condition evaluates to True, the code inside
the if block will be executed.
2. if...else Statement
The if...else statement allows you to execute one block of code if the condition is True, and a
different block of code if the condition is False.
3. Nested if Statements
A nested if statement occurs when an if statement appears inside another if or else block. This is
useful when you need to check multiple conditions in a specific order.
2.4 Looping in python (while loop, for loop, nested loops)
Q.1.Print the following pattern using loop:
1010101
10101
101
1
Q.2. Explain use of Pass and Else keyword with for loops in python
Looping allows you to repeat a block of code multiple times. There are two primary types of loops in
Python:
● while loop
● for loop
1. while Loop
A while loop repeatedly executes a block of code as long as a given condition evaluates to True. If the
condition becomes False, the loop stops.
Output:
0
1
2
3
4
2. for Loop
A for loop is typically used for iterating over a sequence (like a list, tuple, dictionary, string, or range) and
executing a block of code for each item in the sequence.The range() function generates a sequence of
numbers, which is often used to control the number of iterations in a for loop.
Output:
0
1
2
Output:
2
3
4
5
6
Output:
0
2
4
6
8
3. Nested Loops
In Python programming language there are two types of loops which are for loop and
while loop. Using these loops we can create nested loops in Python. Nested loops
mean loops inside a loop. For example, while loop inside the for loop, for loop inside
the for loop, etc.
x = [1, 2]
y = [4, 5]
for i in x:
for j in y:
print(i, j)
Output:
14
15
24
25
1. continue Statement
The continue statement skips the current iteration of the loop and moves to the next
iteration, without executing the remaining code inside the loop for the current
iteration.
Output
1
3
4
2. pass Statement
The pass statement is a placeholder that does nothing. It's often used in places
where a statement is syntactically required, but you don’t want to execute any
code.
Output:
0
1
3
4
3. break Statement
The break statement immediately terminates the loop and exits out of it, regardless
of the loop's condition.
Output:
0
1
2
In Python, a for or while loop can have an else block, which will execute if the
loop completes normally
Output:
0
1
2
3
4
Loop finished without break