Operators

What is an Operator?

An operator is a symbol that signifies an operation to be performed on one or more operands.

Types

  • Unary and Binary Operators: Operators can be unary, operating on one operand, or binary, operating on two operands.
  • Examples: Addition (+), subtraction (-), multiplication (*), and division (/) are common binary operators, while negation (-) is a unary operator.

Operand: An operand is a value that an operator acts upon. For example, in the expression 2 + 32 and 3 are operands.


Arithmetic Operators

The common arithmetic operators in Python are + (addition), - (subtraction), * (multiplication), / (division), and ** (exponentiation).

The division operator, /, produces “true division” regardless of its operand types. The truncating division operator, //, produces either an integer or float truncated result based on the type of operands applied to. The modulus operator (%) gives the remainder of the division of its operands

Relational Operators

  • The relational operators in Python perform the comparison operations
  • Relational expressions are a type of Boolean expression, since they evaluate to a Boolean result.
  • These operators not only apply to numeric values, but to any set of values that has an ordering, such as strings.
  • String values are ordered based on their character encoding, which normally follows a lexographical (dictionary) ordering .
  • For example, ‘Alan’ is less than ‘Brenda’ since the Unicode (ASCII) value for ‘A’ is 65, and ‘B’ is 66.
  • However, ‘alan’ is greater than (comes after) ‘Brenda’ since the Unicode encoding of lowercase letters (97, 98, . . .) comes after the encoding of uppercase letters (65, 66, . . .).

Membership Operators
  • These operators can be used to easily determine if a particular value occurs within a specified list of values.
  • The membership operators are in and not in.
    • The in operator is used to determine if a specific value is in a given list, returning True if found, and False otherwise.
    • The not in operator returns the opposite result.

The membership operators can also be used to check if a given string occurs within another string.



Boolean Operators


Boolean algebra contains a set of Boolean (logical) operators, denoted by andor, and not in Python.

  • Logical and is true only when both its operands are true—otherwise, it is false.
  • Logical or is true when either or both of its operands are true, and thus false only when both operands are false.
  • Logical not simply reverses truth values—not False equals True, and not True equals False.