Holla Tech - Learn

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

 

NOTE!
The spaces around the plus and minus signs here are optional (the code would work without them), but they make it easier to read.

Simple Operations
 

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 

 

NOTE!
The spaces around the plus and minus signs here are optional (the code would work without them), but they make it easier to read.

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 

 

NOTE!
The plus signs can also be put in front of numbers, but this has no effect, and is mostly used to emphasize that a number is positive to increase readability of code.

Simple Operations
 

Dividing by zero in Python produces an error, as no answer can be calculated.

>> 11 / 0
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
ZeroDivisionError: division by zero 

 

This is box title
In Python, the last line of an error message indicates the error’s type. Read error messages carefully, as they often tell you how to fix a program!

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE PYTHON MAIN COURSE PAGE. 

PYTHON MAIN COURSE PAGE

 


©️ License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT