Python language basics 8: assigning nulls

Exercises in .NET with Andras Nemes

Introduction

In the previous post we finished looking into how boolean types are represented in Python. This short post will show how nulls are represented. We use nulls to indicate the absence of any value in a variable.

None

If you have a C# or Java background then you’ll first guess will be that nulls are assigned by the keyword ‘null’, right? Let’s see:

The compiler won’t like that. Instead, the keyword ‘None’ – strictly with a capital N – is reserved for this purpose. I still like VB.NET’s ‘Nothing’ most but ‘None’ is also more imaginative than a simple ‘null’ :-)

Checking whether a variable is null is performed with the ‘is’ and ‘is not’ operators. You’ll get a warning in the PyCharm IDE if you try to test for None using the equality operator ‘==’:

Instead, write as follows:

res1 will be True and res2 will accordingly be…

View original post 11 more words

Advertisements
This entry was posted in Brian By Experience. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s