2. Basic Types: Numbers
Integers
Now that we have shown that #\mathtt{\text{True}}# and #\mathtt{\text{False}}# are equal to #\mathtt{\text{1}}# and #\mathtt{\text{0}}# in Python, it might come as no surprise that the #\color{#4271ae} {\mathtt{\text{bool}}}# type is actually a subtype of the type that includes all the integers; #\color{#4271ae} {\mathtt{\text{int}}}#. Similar to #\color{#4271ae} {\mathtt{\text{bool}}}# being a shorthand for boolean, #\color{#4271ae} {\mathtt{\text{int}}}# is a shorthand for integer.
Integer defintionAn integer is defined as a number that can be written without a fractional component. In computer science, integer refers to a data type that describes some range of mathematical integers.
Like in mathematics, integers in Python support a variety of mathematical operations. Their use is very similar to how you would use them on a calculator, barring some exceptions.
The most common mathematical operators supported by #\color{#4271ae} {\mathtt{\text{int}}}#:
Operator | Name | Usage | Returns |
#\mathtt{\text{+}}# | Addition | #\mathtt{\text{x}}# #\mathtt{\text{+}}# #\mathtt{\text{y}}# | The sum of #\mathtt{\text{x}}# and #\mathtt{\text{y}}#. |
#\mathtt{\text{-}}# | Subtraction | #\mathtt{\text{x}}# #\mathtt{\text{-}}# #\mathtt{\text{y}}# | #\mathtt{\text{x}}# subtracted by #\mathtt{\text{y}}#. |
#\mathtt{\text{*}}# | Multiplication | #\mathtt{\text{x}}# #\mathtt{\text{*}}# #\mathtt{\text{y}}# | #\mathtt{\text{x}}# multiplied by #\mathtt{\text{y}}#. |
#\mathtt{\text{/}}# | Division | #\mathtt{\text{x}}# #\mathtt{\text{/}}# #\mathtt{\text{y}}# | #\mathtt{\text{x}}# divided by #\mathtt{\text{y}}#. |
#\mathtt{\text{**}}# | Exponentiation | #\mathtt{\text{x}}# #\mathtt{\text{**}}# #\mathtt{\text{y}}# | #\mathtt{\text{x}}# to the power of #\mathtt{\text{y}}# |
Less common, but still useful operators supported by #\color{#4271ae} {\mathtt{\text{int}}}#:
Operator | Name | Usage | Returns |
#\mathtt{\text{%}}# | Modulo or Modulus | #\mathtt{\text{x}}# #\mathtt{\text{%}}# #\mathtt{\text{y}}# | The remainder of #\mathtt{\text{x}}# divided by #\mathtt{\text{y}}#. |
#\mathtt{\text{//}}# | Floor Division or Integer Division | #\mathtt{\text{x}}# #\mathtt{\text{//}}# #\mathtt{\text{y}}# | The result of #\mathtt{\text{x}}# divided by #\mathtt{\text{y}}# rounded down (floored) to #\mathtt{\text{int}}#. |
Let's take a look at the operators in action, most of them should be familiar.
Operator examplesSimple integer addition:
>>> 10 + 10
|
20
|
>>> 10 + 10 + 10
|
30
|
Just like we used parentheses in boolean expressions we can use them here to modify the order of evaluation.
Parentheses examplesWe can use parentheses to subvert operator precedence:
>>> 4 + 4 * 4
|
20
|
>>> (4 + 4) * 4
|
32
|
Even when parentheses are not necessary it might be useful to add them anyway to make your code more explicit.
>>> 36 - (-12)
|
32
|
Booleans in Python inherit all functionality from integers, so it's possible to perform calculations with #\color{#F5871F} {\mathtt{\text{True}}}# and #\color{#F5871F} {\mathtt{\text{False}}}#. They will simply evaluate to #\color{#F5871F} {\mathtt{\text{1}}}# and #\color{#F5871F} {\mathtt{\text{0}}}# within mathematical expressions.
Boolean examplesAs a subtype of #\color{#4271ae} {\mathtt{\text{int}}}#, #\color{#4271ae} {\mathtt{\text{bool}}}# inherits the same functionality:
>>> True + False
|
1
|
>>> True * 3 / True
|
3.0
|
Or visit omptest.org if jou are taking an OMPT exam.