Operators Review

  • plus +: adds two numbers, and concatenates strings and lists.
    • Examples: 1 + 2, "Hello " + "world!", [1] + [2]
  • subtract -: subtracts two numbers, and does set difference with sets.
    • Examples: 1 - 2, {1, 2, 3} - {2, 3, 4}
  • multiply *: multiplies two numbers, repeats strings, and lists.
    • Examples: 1 * 2, "Hello " * 3, [1] * 2
  • divide /: divides two numbers.
    • Examples: 12 / 4
  • power **: raises a number to a power.
    • Examples: 10 ** 2
  • integer divide //: divides two numbers and rounds down.
    • Examples: 10 // 3
  • modulo %: divides two numbers and returns the remainder.
    • Examples: 10 % 3
  • addition assignment += adds a number to a variable.
    • Examples: i += 1
  • equality ==: checks if two values are equal.
    • Examples: 1 == 2, "Hello" == "Hello"
  • inequality <, >, <=, and >=: checks if two values are less than, greater than, less than or equal to, or greater than or equal to each other; does set subset and superset with sets.
    • Examples: 1 < 2, 1 > 2, 1 <= 2, 1 >= 2, {1, 2} < {1, 2, 3}
  • and and, &: checks if two boolean values are both true, does set intersection with sets.
    • Examples: True and False, {1, 2, 3} & {2, 4, 6}
  • or or, |: checks if at least one of two boolean values are true, does set union with sets.
    • Examples: True or False, {1, 2, 3} | {2, 4, 6}
  • xor xor, ^: checks if exactly one of two boolean values are true, does set symmetric difference with sets.
    • Examples: True ^ False, {1, 2, 3} ^ {2, 4, 6}
  • not not, !=: checks if a boolean value is false, check if two values are not equal.
    • Examples: not True, 1 != 2

Feel free to copy any of the examples into the empty code editor below. You will need to wrap the code in a print function for it to display.