<jupyter_start><jupyter_text>Arithmetic Operators Numeric types support many arithmetic operators, such as `+`, `-`, `*` and `\`. But these operators are sometimes also supported with non-numeric types, but are overloaded to provided slightly different behavior. For example, sequence types (usually) support some of these operators. For lists/tuples, `+` forms a new list/tuple concatenating elements from both:<jupyter_code>a = (1, 2, 3)
b = (2, 3, 4)
print(id(a), id(b))
result = a + b
print(id(result), type(result), result)<jupyter_output>4447629672 4447632624
4446061288 <class 'tuple'> (1, 2, 3, 2, 3, 4)<jupyter_text>As you can see a (new) tuple is returned that is a concatenation of the two original tuples. The same holds for lists.But what happens if we concatenate a list to a tuple?<jupyter_code>a = [1, 2, 3]
b = (3, 4, 5)
result = a + b<jupyter_output><empty_output><jupyter_text>So as you can see, this concatenation will only work for like types. One interesting things, is that `*` between a list/tuple types and integers **are** supported:<jupyter_code>a = [1, 'x']
a * 3
3 * a<jupyter_output><empty_output><jupyter_text>Div and Mod Two other really important arithmetic operators are **integer floor division** (`//`) and **mod** (`%`).When dealikng with positive integers, we can think of `a // b` as the integer portion of `a / b`:<jupyter_code>10 / 3
10 // 3<jupyter_output><empty_output><jupyter_text>and `%` can be though of as the remainder of an integer division:<jupyter_code>10 // 3<jupyter_output><empty_output><jupyter_text>with remainder:<jupyter_code>10 % 3<jupyter_output><empty_output><jupyter_text>But when we deal with negative numbers, things are no longer that straightforward.You really need to think of `\\` as the **floor** of the regular division `\`. What does the floor of a number mean?  The **floor** of `a/b` is the **largest** **integer** **less than** `a/b`.For example:```floor(10/3)``` --> ```floor(3.333...)``` --> `3`Think of it using a number line:`````` But when dealing with negative numbers:```floor(-10/3)``` --> ```floor(-3.333...)``` --> `-4`Again thinking of it using a number line:`````` So where does that leave us with this so-called 'remainder'.Instead of thinking of `%` as a remainder, it should be considered as a number such that:``a = b * (a // b) + a % b`` Here's a simple example, using positive numbers:<jupyter_code>a = 10
b = 3
a // b, a % b
3 * 3 + 1<jupyter_output><empty_output><jupyter_text>But of course this now changes when dealing with negative numbers:<jupyter_code>a = -10
b = 3<jupyter_output><empty_output><jupyter_text>Rewriting the above formula we get: ```a % b = a - b * (a // b)``` So in this case:<jupyter_code>-10 - 3 * (-10 // 3)<jupyter_output><empty_output><jupyter_text>Which happens to be `a % b`:<jupyter_code>-10 % 3<jupyter_output><empty_output><jupyter_text>But watch what happens when we switch the signs around:<jupyter_code>a = 10
b = -3
a // b<jupyter_output><empty_output><jupyter_text>That has not changed, but...<jupyter_code>10 - -3 * (10 // -3)
10 % -3<jupyter_output><empty_output><jupyter_text>As you can see the sign of the mod operation changed!! So be careful with div and mod - for positive numbers, the analogy to integer portion of the division and the remainder holds - but you cannot use that analogy easily when dealing with negative numbers.  Set Operations In mathematics, sets support operations such as unions and intersections.Python implements this functionality too, using `&` for intersections, and `|` for unions:<jupyter_code>a = {1, 2, 3}
b = {3, 4, 5}
a | b
a & b<jupyter_output><empty_output><jupyter_text>An easy way to remember which operator means which is to remember what `&&` and `||` mean in Java.- `&&` is an `and` operation - so here, for sets `a & b`, we are looking for elements that are in `a` **and** in `b`- `||` is an `or` operation - so here, for sets `a | b`, we are looking for elements that are in `a` **or** in `b` Although sets do **not** support the `+` operator, they do support the `-` operator - it returns the **difference** of two sets:<jupyter_code>a = {1, 2, 3, 4}
b = {2, 3}
a - b<jupyter_output><empty_output><jupyter_text>Note that the difference operation is **not** commutative!<jupyter_code>a - b, b - a<jupyter_output><empty_output><jupyter_text>There is another set operation, called the **symmetric difference** of two sets - basically an operation that returns the elements of the union of the two sets minus the elements in the intersection of the two sets. So, all the elements of both sets that are not common to both. Python uses the `^` operator for this (if you familiar with the `xor` boolean operator, you'll probably understand why Python chose that particular operator.Using set notation, ```a ^ b = (a | b) - (a & b)```<jupyter_code>a = {1, 2, 3}
b = {2, 3, 4}
a ^ b<jupyter_output><empty_output><jupyter_text>Or, equivalently:<jupyter_code>(a | b) - (a & b)<jupyter_output><empty_output><jupyter_text>We also have to be quite careful with order of precedence with these operators, listed here in order of lowest to highest precedence:- `|`- `^`- `&`- `-` For example, as we just saw:<jupyter_code>(a | b) - (a & b)<jupyter_output><empty_output><jupyter_text>But:<jupyter_code>a | b - a & b<jupyter_output><empty_output><jupyter_text>Which was actually calculated this way:<jupyter_code>a | ((b - a) & b)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup