Operators Review
- plus
+
: adds two numbers, and concatenates strings and lists.- Examples:
1 + 2
,"Hello " + "world!"
,[1] + [2]
- Examples:
- subtract
-
: subtracts two numbers, and does set difference with sets.- Examples:
1 - 2
,{1, 2, 3} - {2, 3, 4}
- Examples:
- multiply
*
: multiplies two numbers, repeats strings, and lists.- Examples:
1 * 2
,"Hello " * 3
,[1] * 2
- Examples:
- divide
/
: divides two numbers.- Examples:
12 / 4
- Examples:
- power
**
: raises a number to a power.- Examples:
10 ** 2
- Examples:
- integer divide
//
: divides two numbers and rounds down.- Examples:
10 // 3
- Examples:
- modulo
%
: divides two numbers and returns the remainder.- Examples:
10 % 3
- Examples:
- addition assignment
+=
adds a number to a variable.- Examples:
i += 1
- Examples:
- equality
==
: checks if two values are equal.- Examples:
1 == 2
,"Hello" == "Hello"
- Examples:
- 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}
- Examples:
- 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}
- Examples:
- 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}
- Examples:
- 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}
- Examples:
- not
not
,!=
: checks if a boolean value is false, check if two values are not equal.- Examples:
not True
,1 != 2
- Examples:
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.