Simple Operations
Python has the capability of carrying out calculations.
Enter a calculation directly into the Python console, and it will output the answer.>>> 2 + 2
4
>>> 5 + 4 - 3
6
Simple Operations
The minus sign indicates a negative number.
Operations are performed on negative numbers, just as they are on positive ones.>>> -7
-7
>>> (-7 + 2) * (-4)
20
Python also carries out multiplication and division, using an asterisk to indicate multiplication and a forward slash to indicate division.
Use parentheses to determine which operations are performed first.>>> 2 * (3 + 4)
14
>>> 10 / 2
5.0
Using a single slash to divide numbers produces a decimal (or float, as it is called in programming). We'll have more about floats in a later lesson.The spaces around the plus and minus signs here are optional (the code would work without them), but they make it easier to read.
Comments
Post a Comment