In this episode:
- Arithmetic operators
- Increment and decrement
- Comparison operators
- Logical operators
The complete series
Missed an episode? Here we go:
- Membership And Instance Operators in Python
- Python Data Types
- Code Documentation And Static Typing
- Handling Exceptions and Errors in Python
Floor division returns the integer quotient of the division between two numbers. Consider the following example in Python:
>>> 89 / 4 # Output: 22.25
>>> 89 // 4 # Output: 22
Math.floor(89 / 4) // Output: 22
Python’s arithmetic operators do not operate exclusively on numbers. For example you can use multiplication on strings to repeat a pattern:
>>> "a" * 9 # Output: 'aaaaaaaaa'
Or addition as well to concatenate simple strings:
>>> "a" + "aa" # Output: 'aaa'
For everything else Python raises a TypeError. That means you cannot sum a number and a string together:
>>> "a" + 9 # TypeError: can only concatenate str (not "int") to str
Nor divide them (besides making no sense):
>>> "a" / 9 # TypeError: unsupported operand type(s) for /: 'str' and 'int'
"a" + 9 // Output: "a9"
They do not raise any error in case of invalid arithmetic operations:
"a" / 9 // Output: NaN
var a = 34; ++a
or postfix (after the number):
var a = 34; a++
In the first case (prefix), the operator increments the number and returns the new number:
var a = 34; ++a // returns 35
In the second case (postfix), the operator increments the number, but returns the original number:
var a = 34; a++ // returns 34 // increments to 35
The same rules are true for the decrement operator.
In Python instead there is no such thing as a decrement/increment operator. Rather you have to use an assignment operator. Here’s an increment assignment in Python:
>>> a = 34 >>> a += 1 # a is now 35
And here’s a decrement assignment:
>>> a = 35 >>> a -= 2 # a is now 33
|greater or equal than||>=||>=|
|less or equal than||<=||<=|
|strict (triple) equal||n/a||===|
To be precise Python has also the is operator (not discussed here) which for me falls more in the identity operators family.
What matters here is that Python is predictable when comparing values:
>>> 9 == "9" # Output: False
9 == "9" // Output: true
Here the first operator 9 is converted to a string right before the comparison. To avoid the conversion you must use the “strict comparison operator“, also called triple-equal:
9 === "9" // Output: false
Now that we talked about arithmetic and comparison operators let’s see their companions: logical operators. Oh, my friend, I can still remember boolean algebra in high school. Do you?
Logical operators are useful when you want to do (or not to do) something in your code depending on the outcome of an expression. Suppose we want to print “I am cool” only if 2019 is greater than 1900. Here’s how you’ll do it with the logical operator and in Python:
>>> 2019 > 1900 and print("I am cool") # Output: "I am cool"
2019 > 1900 && console.log("I am cool")
The or logical operator works the other way around. or means: do the stuff on the right only if the stuff on the left is false. Here’s an example in Python:
>>> 2019 > 1900 or print("I am cool") # Output: True
2019 > 1900 || console.log("I am cool")
Last but not least there is logical negation, useful when you want to flip the result of an expression. For example we can print “I am cool” even if a given expression on the left is false. How? With the not operator in Python:
>>> not 2019 > 151900 and print("I am cool") # Output: "I am cool"
2019 is clearly smaller than 151900, but logical not flips the result, so the right expression is still evaluated. The following example should be more simple to grasp:
>>> not False and print("I am cool") # Output: "I am cool"
!false && console.log("I am cool")
Thanks for reading and stay tuned for more!