This time we'll look at data types for numbers, the ones you'll deal with constantly once figures are involved.
Python has several numeric data types, such as int, float, and complex. Let's go further.
The Kinds of Numeric Data Types in Python 3
As the name says, a numeric data type holds numbers. Like numbers in general, it accepts calculation or arithmetic operations (add, subtract, multiply, divide, etc.)
In math we know several kinds of numbers: whole, fractional, real, imaginary, all the way to complex. Python knows 3 number types too, the numeric data types int, float, and complex.
The int Data Type (Whole Numbers)
First there's int, the integer data type. It holds whole numbers, positive or negative.
What is a whole number?
A whole number is a number with no decimal part, in other words not a fraction — surely you didn't forget that 🙄.
>>> a = 3
>>> type(a)
<class 'int'>
>>> b = -3
>>> type(b)
<class 'int'>
The float Data Type (Fractions)
Next there's the float data type, or floating point. It holds fractional numbers, whether written directly as a decimal or produced by a division.
Decimal numbers in Python are separated with a dot (.) rather than a comma (,)
>>> a = 2.5
>>> type(a)
<class 'float'>
>>> b = 22/7
>>> type(b)
<class 'float'>
The complex Data Type (Complex Numbers)
Complex numbers can feel strange to us. A complex number is a combination of a real number and an imaginary number.
For example:
That sum is what's called a complex number. The number 1 is a real number, while 2i there is an imaginary number, with i being the root of -1.
If you want to know more about complex numbers, take a look here.
In Python the imaginary unit isn't written as i, but with the letter j.
>>> a = 1+2j
>>> type(a)
<class 'complex'>
You can also use just the imaginary part to make a complex number.
>>> b = 1j
>>> type(b)
<class 'complex'>
Tangled, right 😥 But don't worry, complex numbers won't get much use in basic Python programming. So for now we won't go further into them. Just knowing they exist is enough.
Imaginary and complex numbers see a lot of use in finding the roots of quadratic equations, and in real work in electronics, especially electromagnetics. Python's complex type is itself widely used as a tool to process and visualize data tied to that kind of work.
Operations on Numeric Data Types
For a quick picture of the operations on numeric data types, see the table below.
| Operation | Operand | Description |
|---|---|---|
| Addition | + |
Adds numbers |
| Subtraction | - |
Subtracts numbers |
| Multiplication | * |
Multiplies numbers |
| Division | / |
Divides numbers |
| Floor division | // |
Divides numbers and rounds the result |
| Remainder (modulus) | % |
Takes the remainder of a division |
| Exponent | ** |
Raises a number to a power |
Now let's try each operation above in the Python interpreter. Go ahead, try them with whatever numbers you like.
>>> 1+2 # ordinary addition
3
>>> 1+-2 # same as 1+(-2) or 1-2
-1
>>> 2-1 # ordinary subtraction
1
>>> 2--1 # same as 2-(-1) or 2+1
3
>>> 2*4 # ordinary multiplication
8
>>> 5/2 # ordinary division
2.5
>>> 5//2 # floor division, the quotient gets rounded down
2
>>> 5%2 # takes the remainder
1
>>> 2**3 # exponentiation, meaning 2 to the power of 3
8
>>> 4**0.5 # exponentiation with a decimal power
2
Next are combined arithmetic operations. The order of operations in Python follows the arithmetic rules from math. Try to recall the lesson on order of operations (multiply, divide, add, subtract). If it's still gone, take a look for yourself on wikipedia hehe.
>>> 2*3 - 1
5
>>> 12/2 - 2**3/2 + 5
7.0
>>> 12/(2 - 2**3)/2 + 5
4.0
>>> 1 + 5%2
2
You can watch the order of operations on numbers in Python 3. Not far off from math in general, hehe...
The modulus (
%) and floor division (//) operations rank the same as multiplication and division. So in a combined operation they run left to right.
Wrapping Up
That's it for this one, hope it helps. Next we'll play with words, characters, and text. More precisely, we'll cover string data types in Python 3.
See ya.. ;)