NumPy Isdecimal Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the isdecimal() function of the char module in Numpy library. The isdecimal() function checks whether an element contains only decimal characters or not.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/lists -.-> lab-86458{{"`NumPy Isdecimal Function`"}} python/tuples -.-> lab-86458{{"`NumPy Isdecimal Function`"}} python/importing_modules -.-> lab-86458{{"`NumPy Isdecimal Function`"}} python/standard_libraries -.-> lab-86458{{"`NumPy Isdecimal Function`"}} python/numerical_computing -.-> lab-86458{{"`NumPy Isdecimal Function`"}} python/build_in_functions -.-> lab-86458{{"`NumPy Isdecimal Function`"}} numpy/data_array -.-> lab-86458{{"`NumPy Isdecimal Function`"}} numpy/bool_idx -.-> lab-86458{{"`NumPy Isdecimal Function`"}} numpy/fancy_idx -.-> lab-86458{{"`NumPy Isdecimal Function`"}} end

Import the necessary packages

First, we need to import the necessary packages which in our case is only NumPy.

import numpy as np

Use isdecimal() with a string

In this step, we will use the isdecimal() function with a simple string.

string1 = "12342"
print("The Input string is:")
print(string1)

x = np.char.isdecimal(string1)
print("The Output is:")
print(x)

Output:

The Input string is:
12342
The Output is:
True

Use isdecimal() with an array of strings

In this step, we will use the isdecimal() function on an array of strings.

inp_ar = np.array([ '20002 2', 'a10009', '12345 ab','01'] )
print("The Input string is:")
print(inp_ar)

x = np.char.isdecimal(inp_ar)
print("The Output is:")
print(x)

Output:

The Input string is:
['20002 2' 'a10009' '12345 ab' '01']
The Output is:
[False False False True]

Summary

In this lab, we learned about the isdecimal() function in the Numpy library. We saw how it is used with its syntax and values returned by this function along with multiple code examples.

Summary

The isdecimal() function in the NumPy library checks whether an element contains only decimal characters or not. This function returns a boolean value where True is returned if the element contains only decimal characters and False is returned if not. It could be used to check if a number is decimal or not using the appropriate characters.

Other Python Tutorials you may like