Python Forum
test if variable is Numeric?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
test if variable is Numeric?
#1
hi, in Visual Basic i can use IsNumeric() function, returns T/F. How do I test if a variable is an integer in python... using standard library

tahnks
Reply
#2
var = '12313210'
if var.isnumeric() and '.' not in var:
    print("The value is integer")
Reply
#3
var = 5
print(var.isnumeric())

returns an error, i was expecting TRUE
Reply
#4
.isnumeric is string method.So, var should be a string.
If you already know that the variable is of numeric type, you can test it using type-function:

if type(var) == int:
    print("var is integer")
Reply
#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
#6
I ask for forgiveness...! In fact, I never use type for type checking in my code, and already knew why it isn't good; In any case, answers should be not only correct, but also clever.
Reply
#7
My explanation was mainly for benefit of the OP :-)
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
#8
If I code for someone else I check the type of the variables or the cli arguments. But if I am doing it for myself... well I don't need it. I know what my script demands.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(May-25-2018, 07:13 AM)wavic Wrote: If I code for someone else I check the type of the variables or the cli arguments.
in this case you should catch the exceptions (in case of unexpected input), not check type... The type of the cli input will always be str anyways :-)
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
#10
I didn't write something for someone for a long time. Honestly, I don't remember what I did the last time. Big Grin
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 510 Mar-29-2024, 06:15 PM
Last Post: idev
Question Numeric Anagrams - Count Occurances monty024 2 1,506 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,004 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,123 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Extract continuous numeric characters from a string in Python Robotguy 2 2,635 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 4,947 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,784 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,115 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,557 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  convert a character to numeric and back Skaperen 2 2,107 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