Python

Python Equal

Python equal comparison is done with ==, the equal operator. The result of the operation is a Boolean. The most common use of the equal operator is to decide the flow of the application: Comparing Objects with == Python equal operator compares the value of objects, that’s in contrast to the Python is operator that …

Python Equal Read More »

Python Less Than

Python less than comparison is done with <, the less than operator. The result of the operation is a Boolean. The most common use of the less than operator is to decide the flow of the application: Comparing Strings in Python You can use the less than operator to compare strings. It will use lexicographical …

Python Less Than Read More »

Python Modulo

Python modulo operation is done using %, the modulus operator. The Python modulo operation returns the remainder after whole number division of the arguments. In the example above 20/6 is 3 and 2 left over. The 2 left over is what the modulo operation returns. Dividing by a smaller number: Using the modulus operator on …

Python Modulo Read More »

Python Floor Division

Python floor is done with //, the floor division operator. It divides two numbers and rounds down the result to the nearest integer. Don’t confuse it with integer division, since it doesn’t just round to the nearest integer, for example: You can also use floats for floor division: If any of the numbers in the …

Python Floor Division Read More »

Python Division

Python division is done with /, the division operator. Python 3 does floating point division for both integer and float arguments. That’s a change from Python 2 where it would behave differently based on the type of arguments: As you can see, in Python 2.7 when dividing integers it would floor the value to produce …

Python Division Read More »

Python Multiplication

Python multiplication is done with *, the multiplication operator: Multiplying floats: Note that floating point operations can produce small rounding errors, 8.3 * 2.6 is not necessarily 21.58. If any of the numbers in the operation are floats, Python will return a float. Multiplying negatives: The multiplication operator can also be used to multiply certain …

Python Multiplication Read More »

Python Subtraction

Python subtraction is done with -, the subtraction operator: Subtracting floats: Note that floating point operations can produce tiny rounding errors, 11.5 – 8.3 is not necessarily 3.2. If any of the numbers in the operation are floats, Python will return a float. Subtracting negatives: Just adding a – in front of a constant/variable will …

Python Subtraction Read More »

Python Addition

Python addition is done with +, the addition operator. Adding floats: Note that floating point operations can produce small rounding errors, 8.3 + 2.3 is not necessarily 10.6. When adding numbers, if any of the numbers in the operation are floats, Python will return a float. Adding negatives: Just adding a + in front of …

Python Addition Read More »

Python is Integer

To check if a value is an integer in Python 3 you can use the isinstance() method: For Python 2 you had to do something like this: If you know you are working with a float and what to test if it’s an integer or not, you can use the is_integer() method: