Python language basics 8: assigning nulls

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:

hello = null

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’ 🙂

hello = None

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 ‘==’:

res = hello == None

Instead, write as follows:

res1 = hello is None
res2 = hello is not None
print(res1)
print(res2)

res1 will be True and res2 will accordingly be False.

Read all Python-related posts on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Python language basics 8: assigning nulls

  1. Brian Dead Rift Webb says:

    Reblogged this on Brian By Experience.

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

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: