• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

What is NoneType in Python and Null Equivalent?

Aniruddha Chaudhari/64368/6
CodePython

NoneType in Python is the data type of the object when the object does not have any value. You can initiate the NoneType object using keyword None as follows.

obj = None

Let’s check the type of object variable ‘obj’.

obj = None
type(obj)

Output:

 <type 'NoneType'>

NoneType object indicates no value.

If you try to print the value of the NoneType object, it does not print anything on the Python interpreter console.

When is NoneType used?

It is useful in many places.

It is the default return type of the function when the function does not return any value.

Use cases (Examples):

  • If the Python regular expression in re.search does not match, it returns NoneType object.
  • When you search key in the Python dictionary and the key is not present, it returns NoneType object.
  • The None keyword is also used for matching or identifying if a certain function returns any value or not.

What is Equivalent to NULL in Python?

You might have seen the NULL value in many of the programming languages but Python. In, PHP, the null value is represented as NULL. In Java programming, it is represented with the keyword Null.

In Python, there is no keyword NULL. Here None keyword is used as equivalent to the NULL.

If you are moving from other programming languages to Python, don’t get confused with None keyword or NoneType object in Python.

How to filter out None values from List and Dictionary?

Remove None Elements from List:

If you want to remove all the None type elements from the Python list, use the filter and lambda function in Python.

list_in = [1, 3, 'cse', None]

lambda_obj = lambda x: (x is not None)

list_out = list(filter(lambda_obj, list_in))

print(list_out)

Output:

[1, 3, 'cse']

Remove Dictionary Entries having Value None:

If you want to remove entries from the dictionary that has values None, refer this tutorial, Or, here is the simple code.

dic_in = {'a': 2, 'b': 0, 'c': 0, 'd': 4, 'e': None}

dic_out = {x:y for x,y in dic_in.items() if y is not None}

print(dic_out)

Output:

{'a': 2, 'b': 0, 'c': 0, 'd': 4}

Hope this helps you to understand lot of concepts related to the None type and equivalent Null type in Python.

For more about Python, do check the complete Python tutorial.

Python Interview Questions eBook

PythonPython None
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Abhimanyu Barnwal
    April 11, 2020 at 6:58 pm

    I am using RiverGIS under QGIS and it works on python code: –

    while executing Creating RAS GIS Import file from HEC-RAS model geometry…
    I am getting this error code: –

    An error has occurred while executing Python code: 
    
    AttributeError: 'NoneType' object has no attribute 'startswith' 
    Traceback (most recent call last):
    
    • Reply
      Aniruddha Chaudhari
      April 11, 2020 at 7:39 pm

      Abhimanyu, you are using startswith() method with the NoneType object.

      For example

      str.startswith()

      Here, str is a NoneType object. It should be a string.

  • Reply
    Pradhan
    April 22, 2020 at 12:43 pm
    elevation = soup.find('div', {'id':'elevation'})
        altitude = elevation.find('span', {'class': "value"})
    

    I am using altitude data by requesting the website through selenium web driver in anaconda3. For the line, altitue=elevation.find, it says ‘NoneType’ object has no attribute ‘find’.
    Can you help me out?

    • Reply
      Aniruddha Chaudhari
      April 22, 2020 at 4:03 pm

      Hi Pradhan,

      From code what I can see is that soup.find() is returning none. One of the reasons behind could be ‘soup’ does not have ‘div’ having ‘id’ as ‘elevation’.

      You can print the ‘soup’ and check the text if you have that ‘div’.

  • Reply
    John
    April 5, 2022 at 6:14 pm

    Thank you so much for this; I was stuck for about an hour before I found this.

    • Reply
      Aniruddha Chaudhari
      April 26, 2022 at 3:21 pm

      You’re welcome. And I’m glad you find this guide useful.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard