Home » Python

None keyword with example in Python

Python None keyword: Here, we are going to learn about the None keyword in Python with example.
Submitted by IncludeHelp, on April 07, 2019

Python None keyword

Note: None is not a keyword – we are just considering & writing it as a keyword which is a replacement of null (in C/C++).

"None" (case-sensitive) is an object of its own datatype, the NoneType, which is similar to null, it is used to define a null value to the variable or represents that a variable does not have any value at all.

None is a type of <class 'NoneType'>.

Syntax:

    None

Example:

    Input:
    a = None
    print("a: ", a);

    Output:
    a: None

Python code to demonstrate example of None keyword

# python code to demonstrate example of 
# None keyword

# type of None
print("type of None: ", type(None))

# declaring a variable and initialize it with None
a = None
# printing value
print("a: ", a)

# checkng None in condition
if a == None:
    print("variable \'a\' contains None")
else:
    print("variable \'a\' does not contain None")

# assigning a value and rechecking the condition
a = 100
if a == None:
    print("variable \'a\' contains None")
else:
    print("variable \'a\' does not contain None")

Output

type of None:  <class 'NoneType'>
a:  None
variable 'a' contains None
variable 'a' does not contain None


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.