Python Friday #3: Numbers, Booleans & None

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:

If you assign an integer as above, your variable is of type 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:

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:

We can use the usual arithmetic methods to work with those numbers:

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):

This prevents you from losing precision. If you do not care about that and want to get an integer back, you can use // instead:

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:

100000!

 

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.

The approximated part is important when we try to compare values:

Instead of directly adding floats and compare them, we need to round:

The range of valid values for floats is big, but not endless:

 

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 and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

You can use the comparison operators without any surprises:

 

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”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.