10 examples of 'python check if variable is number' in Python

Every line of 'python check if variable is number' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
54def is_number(value):
55 return type(value) in _numeric_types
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1022def is_number(value):
1023 """Checks if `value` is a number.
1024
1025 Args:
1026 value (mixed): Value to check.
1027
1028 Returns:
1029 bool: Whether `value` is a number.
1030
1031 Note:
1032 Returns ``True`` for ``int``, ``long`` (PY2), ``float``, and
1033 ``decimal.Decimal``.
1034
1035 Example:
1036
1037 >>> is_number(1)
1038 True
1039 >>> is_number(1.0)
1040 True
1041 >>> is_number('a')
1042 False
1043
1044 .. versionadded:: 1.0.0
1045
1046 .. versionchanged:: 3.0.0
1047 Added ``is_num`` as alias.
1048
1049 .. versionchanged:: 4.0.0
1050 Removed alias ``is_num``.
1051 """
1052 return not is_boolean(value) and isinstance(value, number_types)
266def is_number(val):
267 return isinstance(val, (int, float))
39def is_number(*p):
40 ''' Returns True if ALL of the arguments are AST nodes
41 containing NUMBER constants
42 '''
43 try:
44 for i in p:
45 if i.token != 'NUMBER' and (i.token != 'ID' or
46 i.class_ != 'const'):
47 return False
48
49 return True
50 except:
51 pass
52
53 return False
150def is_number(value):
151 return _get_safe_value_or_none(value, (int, float)) is not None
47def is_number(value):
48 return (not isinstance(value, str)) and np.isscalar(value)
51def is_number(x):
52 if isinstance(x, tf.Tensor):
53 return True
54 try:
55 float(x)
56 return True
57 except:
58 return False
22def is_number(self):
23 return self._operation_type == Operations._number
6def isnum(val):
7 """Check if a value is a number"""
8 # Todo: is there a better way to check this?
9 return isinstance(val, int) or isinstance(val, float) or isinstance(val, long)
359def isnum(o):
360 return isinstance(o, (int, float))

Related snippets