Python Forum
test if variable is Numeric?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
test if variable is Numeric?
#5
the common way is
var = 5
print(isinstance(var, int))
note that you can supply multiple types as second argument

var = 5
var2 = 1.2
var3 = 'some text'
print(isinstance(var, (int, float)))
print(isinstance(var2, (int, float)))
print(isinstance(var3, (int, float)))
read https://docs.quantifiedcode.com/python-a...tance.html or https://stereochro.me/ideas/type-vs-isinstance why isinstance() is preferable than type()
At the end of the link there is also link to other resources why checking for type is against python principles/design patterns. You probably have heard that it's easier to ask for forgiveness than permission and "If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck" (https://docs.python.org/3.6/glossary.html , check EAFP and duck-typing) https://en.wikipedia.org/wiki/Duck_typing
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
test if variable is Numeric? - by ilcaa72 - May-25-2018, 01:08 AM
RE: test if variable is Numeric? - by scidam - May-25-2018, 01:13 AM
RE: test if variable is Numeric? - by ilcaa72 - May-25-2018, 01:18 AM
RE: test if variable is Numeric? - by scidam - May-25-2018, 01:35 AM
RE: test if variable is Numeric? - by buran - May-25-2018, 06:23 AM
RE: test if variable is Numeric? - by scidam - May-25-2018, 07:05 AM
RE: test if variable is Numeric? - by nilamo - Jun-22-2018, 04:25 PM
RE: test if variable is Numeric? - by buran - May-25-2018, 07:07 AM
RE: test if variable is Numeric? - by wavic - May-25-2018, 07:13 AM
RE: test if variable is Numeric? - by buran - May-25-2018, 07:16 AM
RE: test if variable is Numeric? - by wavic - May-25-2018, 07:20 AM
RE: test if variable is Numeric? - by ljmetzger - Jun-22-2018, 06:09 PM
RE: test if variable is Numeric? - by volcano63 - Jun-22-2018, 08:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 977 Mar-29-2024, 06:15 PM
Last Post: idev
Question Numeric Anagrams - Count Occurances monty024 2 1,635 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,116 Nov-06-2021, 03:26 PM
Last Post: snippsat
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,386 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Extract continuous numeric characters from a string in Python Robotguy 2 2,785 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 5,216 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,886 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,265 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,653 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  convert a character to numeric and back Skaperen 2 2,222 Jan-28-2020, 09:32 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020