PYTHON Tutorial

Operators in Python

Arithmetic Operators

  • Perform basic mathematical calculations
  • Operators: +, -, *, /, //, **, %

Comparison Operators

  • Compare two values to produce a Boolean result
  • Operators: ==, !=, <, >, <=, >=

Logical Operators

  • Combine Boolean expressions
  • Operators: and, or, not

Practical Steps:

  • Assign values to variables (e.g., a = 5, b = 2)
  • Use arithmetic operators to perform calculations (e.g., print(a + b))
  • Use comparison operators to compare values (e.g., print(a > b))
  • Use logical operators to combine conditions (e.g., print(a > 0 and b > 0))

Example:

# Arithmetic operator - addition
print(5 + 2)  # Outputs 7

# Comparison operator - greater than
print(5 > 2)  # Outputs True

# Logical operator - and condition
print((5 > 0) and (2 > 0))  # Outputs True