Convention 2.7.1. Spaces and Expressions.
For readability, you should always put a space on both sides of operators. This gives your code some “breathing room”.
result=(a*b)/(c+d) # difficult to read
result = (a * b) / (c + d) # more readable
20 + 32 hour - 1 hour * 60 + minute minute / 60 5 ** 2 (5 + 9) * (15 - 7)
+, -, and *, and the use of parentheses for grouping, mean in Python what they mean in mathematics. The asterisk (*) is the token for multiplication, and ** is the token for exponentiation. Addition, subtraction, multiplication, and exponentiation all do what you expect./ which always evaluates to a floating point result.//. It always truncates its result down to the next smallest integer (to the left on the number line).1.75 but the result of the integer division is simply 1. Take care that you choose the correct flavor of the division operator. If you’re working with expressions where you need floating point values, use the division operator /. If you want an integer result, use //.%). The syntax is the same as for other operators.x % y is zero, then x is divisible by y. Also, you can extract the right-most digit or digits from a number. The expression x % 10 yields the right-most digit of x. Similarly, x % 100 yields the last two digits.result=(a*b)/(c+d) # difficult to read
result = (a * b) / (c + d) # more readable
print(18 / 4)
/ operator does exact division and returns a floating point result./ operator does exact division and returns a floating point result./ operator does exact division and returns a floating point result./ operator does exact division and returns a floating point result.print(18 // 4)
// operator does integer division and returns an integer result// operator does integer division and returns an integer result, but it truncates the result of the division. It does not round.// operator does integer division and returns the truncated integer result.// operator does integer division and returns the result of the division on an integer (not the remainder).print(18 % 4)
% operator returns the remainder after division.% operator returns the remainder after division.% operator returns the remainder after division.% operator returns the remainder after division.