From the Southern Hemisphere

From the Southern Hemisphere

南半球に移住したプログラマの日々

【Python】What is the None in Python

None in python is a datatype which express "not exist". It is same as Null datatype in other language like Java or C#.

Declaring a None variable

It is way to declare a None variable. Please make sure that Initial letter of "None" is capital.

# declaring a None variable
my_variable = None

Examples

Example 1

Checking if a variable is None using is operator

# declaring a None variable

my_variable = None

# checking if the variable is None 

if my_variable is None:
    print("None")

else:

    print("Not None")
# result

None

Example 2

Checking if a variable is not None using is and not keywords

# declaring a None variable

my_variable = None

# checking if the variable is not None 

if my_variable is not None:
    print("Not None")

else:

    print("None")
# result

None

Example 3

Checking if a variable is None using == operator

# declaring a None variable

my_variable = None

# checking if the variable is not None 

if my_variable == None:
    print("None")

else:

    print("Not None")
# result

None

  

Learning Python: Powerful Object-Oriented Programming (English Edition)

Learning Python: Powerful Object-Oriented Programming (English Edition)

  • 作者:Lutz, Mark
  • 発売日: 2013/06/13
  • メディア: Kindle版