# Python Programming/Operators

## Contents

## Basics[edit]

Python math works like you would expect.

```
>>> x = 2
>>> y = 3
>>> z = 5
>>> x * y
6
>>> x + y
5
>>> x * y + z
11
>>> (x + y) * z
25
```

Note that Python adheres to the PEMDAS order of operations.

## Powers[edit]

There is a built in exponentiation operator ****, which can take either integers, floating point or complex numbers. This occupies its proper place in the order of operations.

```
>>> 2**8
256
```

## Division and Type Conversion[edit]

For Python 2.x, dividing two integers or longs uses integer division, also known as "floor division" (applying the floor function after division. So, for example, 5 / 2 is 2. Using "/" to do division this way is deprecated; if you want floor division, use "//" (available in Python 2.2 and later).

"/" does "true division" for floats and complex numbers; for example, 5.0/2.0 is 2.5.

For Python 3.x, "/" does "true division" for all types.^{[1]}^{[2]}

Dividing by or into a floating point number (there are no fractional types in Python) will cause Python to use true division. To coerce an integer to become a float, 'float()' with the integer as a parameter

```
>>> x = 5
>>> float(x)
5.0
```

This can be generalized for other numeric types: int(), complex(), long().

Beware that due to the limitations of floating point arithmetic, rounding errors can cause unexpected results. For example:

>>> print 0.6/0.2 3.0 >>> print 0.6//0.2 2.0

## Modulus[edit]

The modulus (remainder of the division of the two operands, rather than the quotient) can be found using the *%* operator, or by the *divmod* builtin function. The *divmod* function returns a *tuple* containing the quotient and remainder.

```
>>> 10%7
3
>>> -10%7
4
```

print (" 10%7 ")

## Negation[edit]

Unlike some other languages, variables can be negated directly:

```
>>> x = 5
>>> -x
-5
```

## Comparison[edit]

Operation | Means |
---|---|

< | Less than |

> | Greater than |

<= | Less than or equal to |

>= | Greater than or equal to |

== | Equal to |

!= | Not equal to |

Numbers, strings and other types can be compared for equality/inequality and ordering:

```
>>> 2 == 3
False
>>> 3 == 3
True
>>> 3 == '3'
False
>>> 2 < 3
True
>>> "a" < "aa"
True
```

## Identity[edit]

The operators `is`

and `is not`

test for object identity and stand in contrast to == (equals): `x is y`

is true if and only if x and y are references to the same object in memory. `x is not y`

yields the inverse truth value.
Note that an identity test is more stringent than an equality test since two distinct objects may have the same value.

```
>>> [1,2,3] == [1,2,3]
True
>>> [1,2,3] is [1,2,3]
False
```

For the built-in immutable data types (like int, str and tuple) Python uses caching mechanisms to improve performance, i.e., the interpreter may decide to reuse an existing immutable object instead of generating a new one with the same value. The details of object caching are subject to changes between different Python versions and are not guaranteed to be system-independent, so identity checks on immutable objects like `'hello' is 'hello'`

, `(1,2,3) is (1,2,3)`

, `4 is 2**2`

may give different results on different machines.

In some Python implementations, the following results are applicable:

```
print 8 is 8 # True
print "str" is "str" # True
print (1, 2) is (1, 2) # False - whyever, it is immutable
print [1, 2] is [1, 2] # False
print id(8) == id(8) # True
int1 = 8
print int1 is 8 # True
oldid = id(int1)
int1 += 2
print id(int1) == oldid # False
```

Links:

- 3. Data model, python.org
- 2. Built-in Functions # id, python.org
- 5. Expressions # is, python.org

## Augmented Assignment[edit]

There is shorthand for assigning the output of an operation to one of the inputs:

```
>>> x = 2
>>> x # 2
2
>>> x *= 3
>>> x # 2 * 3
6
>>> x += 4
>>> x # 2 * 3 + 4
10
>>> x /= 5
>>> x # (2 * 3 + 4) / 5
2
>>> x **= 2
>>> x # ((2 * 3 + 4) / 5) ** 2
4
>>> x %= 3
>>> x # ((2 * 3 + 4) / 5) ** 2 % 3
1
>>> x = 'repeat this '
>>> x # repeat this
repeat this
>>> x *= 3 # fill with x repeated three times
>>> x
repeat this repeat this repeat this
```

## Boolean[edit]

### or[edit]

```
if a or b:
do_this
else:
do_this
```

### and[edit]

```
if a and b:
do_this
else:
do_this
```

### not[edit]

```
if not a:
do_this
else:
do_this
```

The order of operations here is: *not* first, *and* second, *or* third. In particular, "True or True and False or False" becomes "True or False or False" which is True.

Caution, Boolean operators are valid on things other than Booleans; for instance "1 and 6" will return 6. Specifically, "and" returns either the first value considered to be false, or the last value if all are considered true. "or" returns the first true value, or the last value if all are considered false.

## Exercises[edit]

- Use Python to calculate .
- Use Python to calculate .
- Use Python to calculate 11111111111111111111+22222222222222222222, but in one line of code with at most 15 characters. (Hint: each of those numbers is 20 digits long, so you have to find some other way to input those numbers)
- Exactly one of the following expressions evaluates to "cat"; the other evaluates to "dog". Trace the logic to determine which one is which, then check your answer using Python.

```
1 and "cat" or "dog"
0 and "cat" or "dog"
```