In this week I needed the pressure of this blog series to keep learning. Otherwise I would have had many excuses not to learn anything new. I noticed that I do not know enough to start with pytest and instead have to look more on the fundamentals of Python. Therefore, I explain in this post the first part of the built-in data types.
This post is part of my journey to learn Python. You can find the other parts of this series here.
Declare and use a variable
In Python you do not need to specify the type of a variable. You just assign the value and that will define its type:
1 |
a = 1 |
If you assign an integer as above, your variable is of type int:
1 2 |
>>> type(a) <class 'int'> |
The >>>
comes from the Python Interpreter and you do not need to type them. A line without those >>>
is an output.
If you later assign a different type, your variable will change its type as well:
1 2 3 |
>>> a = "Hello" >>> type(a) <class 'str'> |
This behaviour looks dangerous, but it is what makes working with Python so powerful. It is the same as in Ruby, where it took me a while to get used to it. As soon as you no longer relay on the compiler and use tests instead, you will see that this kind of assignment is indeed a great help.
Integers
As with variables, Python makes it easy to write numbers. Whenever you enter a number without a decimal point, Python turns it into an integer:
1 2 |
>>> type(1) <class 'int'> |
We can use the usual arithmetic methods to work with those numbers:
1 2 3 4 5 6 |
>>> 1 + 2 3 >>> 4 * 5 20 >>> 6 - 4 2 |
However, there is one exception that was introduced with Python 3. If you divide two integers, you get a float (a number with a decimal point):
1 2 3 4 |
>>> 3 / 2 1.5 >>> type(3 / 2) <class 'float'> |
This prevents you from losing precision. If you do not care about that and want to get an integer back, you can use //
instead:
1 2 3 4 |
>>> 3 // 2 1 >>> type(3 // 2) <class 'int'> |
Another speciality of Python is that integers have unlimited precision. Therefore, it is possible to calculate the factorial of 100,000. This will result in a number with 456,574 digits:
Floating-point numbers
Floats are numbers with a decimal point and, as with most other programming languages, the numbers we enter are approximated to the internal representation on the machine.
1 2 |
>>> type(5.6) <class 'float'> |
The approximated part is important when we try to compare values:
1 2 |
>>> 0.1 + 0.1 + 0.1 == 0.3 False |
Instead of directly adding floats and compare them, we need to round:
1 2 |
>>> round(.1 + .1 + .1, 10) == round(.3, 10) True |
The range of valid values for floats is big, but not endless:
1 2 3 4 5 |
>>> import sys >>> sys.float_info.max 1.7976931348623157e+308 >>> sys.float_info.min 2.2250738585072014e-308 |
Booleans
In Python you can test any object if it is true or false (its truth value) and use the result in if() statements and while loops. All those built-in objects are considered false:
- constants defined to be false:
None
andFalse
. - zero of any numeric type:
0, 0.0, 0j, Decimal(0), Fraction(0, 1)
- empty sequences and collections:
'', (), [], {}, set(), range(0)
1 2 3 4 5 6 7 8 9 10 |
>>> bool(False) False >>> bool(None) False >>> bool([]) False >>> bool("") False >>> bool(0) False |
You can use the comparison operators without any surprises:
1 2 3 4 5 6 7 8 9 10 11 12 |
>>> 5 > 4 True >>> 5 < 4 False >>> 5 != 4 True >>> 5 == 4 False >>> 5 <= 4 False >>> 5 >= 4 True |
None
None is a special value and comparable to null
in C#. It is the sole value of the NoneType and not a Boolean. You can use it to represent the absence of a value.
Next
I find it hard to keep sets, lists, dictionaries and tuples separated – especially when I write them on my own. I will therefore write a post to explain the differences to memorize what ()
and []
exactly creates.
2 thoughts on “Python Friday #3: Numbers, Booleans & None”