STATEMENT,
EXPRESSION AND
OPERATORS
• Statement: A statement in Python is an instruction that performs an
action.
• Eg:
• x = 5 # Assignment statement
• print(x) # Print statement
• Expression: An expression is a combination of variables, operators,
and values that produces a result or value.
• y = 10 (statement)
• z = y + 5 # The expression y + 5 evaluates to 15
• print(z) # Output: 15
• Operators and Operands
• Operators: Special symbols that perform operations on variables and values.
• Operands:The values or variables on which operators perform operations.
• Types of Operators
• a) Arithmetic Operators
• b) Relational (Comparison) Operators
• c) Assignment Operators
• d) Bitwise Operators
• e) Logical Operators
• f) Membership Operators
a) Arithmetic Operators
These are used to perform mathematical operations like addition,
subtraction, multiplication, and division.
Eg:
a = 10
b = 3
print(a + b) # Output: 13
print(a % b) # Output: ?
print(a ** b) # Output: ?
print(a // b) # Output: ?
b) Relational (Comparison) Operators
These operators compare two values and return a Boolean result (True
or False).
Eg:
a = 5
b = 8
print(a == b) # Output: False
print(a <= b) # Output: True
• c) Assignment Operators
• These are used to assign values to variables.
• a = 10
• a += 5 # a becomes ?
• print(a)
• d) Bitwise Operators
• These operate at the bit level and are useful in manipulating bits.
a = 10 # binary: 1010
b = 4 # binary: 0100
print(a & b) # Output: 0 (binary: 0101000)
print(a | b) # Output: 14 (binary: 1110)
print(a << 1) # Output: 20 (binary: 10100)
e) Logical Operators
Logical operators are used to combine conditional statements.
They evaluate expressions to determine whether they are true or false.
and: Returns True if both conditions are true.
or: Returns True if at least one condition is true.
not: Returns True if the condition is false (negates the condition).
x = 10
y = 5
if x > 5 and y > 3:
print("Both conditions are true") # Output: Both conditions are true
if x > 5 or y > 10:
print("At least one condition is true") # Output: At least one condition is true
if not(x < 5):
print("Condition is false, so 'not' makes it true")
f) Membership Operators
• Membership operators are used to test whether a value or variable exists in a
sequence (such as a string, list, tuple, or dictionary).
• in: Returns True if a value is found in the sequence.
• not in: Returns True if a value is not found in the sequence.
• my_list = [1, 2, 3, 4, 5]
• # IN Operator
• if 3 in my_list: print("3 is in the list") # Output: 3 is in the list
• # NOT IN Operator
• if 6 not in my_list: print("6 is not in the list") # Output: 6 is not in the list

python statement, expressions and operators.pptx

  • 1.
  • 2.
    • Statement: Astatement in Python is an instruction that performs an action. • Eg: • x = 5 # Assignment statement • print(x) # Print statement • Expression: An expression is a combination of variables, operators, and values that produces a result or value. • y = 10 (statement) • z = y + 5 # The expression y + 5 evaluates to 15 • print(z) # Output: 15
  • 3.
    • Operators andOperands • Operators: Special symbols that perform operations on variables and values. • Operands:The values or variables on which operators perform operations. • Types of Operators • a) Arithmetic Operators • b) Relational (Comparison) Operators • c) Assignment Operators • d) Bitwise Operators • e) Logical Operators • f) Membership Operators
  • 4.
    a) Arithmetic Operators Theseare used to perform mathematical operations like addition, subtraction, multiplication, and division. Eg: a = 10 b = 3 print(a + b) # Output: 13 print(a % b) # Output: ? print(a ** b) # Output: ? print(a // b) # Output: ?
  • 5.
    b) Relational (Comparison)Operators These operators compare two values and return a Boolean result (True or False). Eg: a = 5 b = 8 print(a == b) # Output: False print(a <= b) # Output: True
  • 6.
    • c) AssignmentOperators • These are used to assign values to variables. • a = 10 • a += 5 # a becomes ? • print(a)
  • 7.
    • d) BitwiseOperators • These operate at the bit level and are useful in manipulating bits. a = 10 # binary: 1010 b = 4 # binary: 0100 print(a & b) # Output: 0 (binary: 0101000) print(a | b) # Output: 14 (binary: 1110) print(a << 1) # Output: 20 (binary: 10100)
  • 8.
    e) Logical Operators Logicaloperators are used to combine conditional statements. They evaluate expressions to determine whether they are true or false. and: Returns True if both conditions are true. or: Returns True if at least one condition is true. not: Returns True if the condition is false (negates the condition). x = 10 y = 5 if x > 5 and y > 3: print("Both conditions are true") # Output: Both conditions are true if x > 5 or y > 10: print("At least one condition is true") # Output: At least one condition is true if not(x < 5): print("Condition is false, so 'not' makes it true")
  • 9.
    f) Membership Operators •Membership operators are used to test whether a value or variable exists in a sequence (such as a string, list, tuple, or dictionary). • in: Returns True if a value is found in the sequence. • not in: Returns True if a value is not found in the sequence. • my_list = [1, 2, 3, 4, 5] • # IN Operator • if 3 in my_list: print("3 is in the list") # Output: 3 is in the list • # NOT IN Operator • if 6 not in my_list: print("6 is not in the list") # Output: 6 is not in the list

Editor's Notes

  • #2 It can be a single line of code that makes the program do something, such as assigning a value, executing a loop, or printing output.
  • #4 print(a % b) # Output: 1 (remainder of 10 divided by 3) print(a ** b) # Output: 1000 (10 raised to the power of 3) print(a // b) # Output: 3 (10 // 3 divides 10 by 3, which equals 3.33..., but the floor division rounds it down to 3.) For Negative Numbers: -10 // 3 gives -3.33..., but floor division rounds down towards the more negative number, which is -4. 17 % 5 results in 2
  • #6 a += 5 # a becomes 15
  • #7 A<< 1 MEANS LEFT SHIFTING THE BITS BY 1 (GIVEN VALUE) Original binary (a = 10): 1010 After shifting left by 1: 10100