Home     My Faqts     Contributors     About     Help    

faqts : Computers : Programming : Languages : Python

FAQTs repaired & updated!
Thanks for your patience...
Entry Add Entry Alert - Edit this Entry

Did You Find This Entry Useful?

3 of 3 people (100%) answered Yes
Recently 3 of 3 people (100%) answered Yes

How do I check if a variable holds a numeric value ?

May 28th, 2002 20:09

Frank Timmermann, Curtis Yanko, Joćo Prado Maia

The type() function is what you need for this. In the interpreter it 
works like this:
<type 'string'>
...so, you might do something like this:
if type(myVar) = type(1)
    print "The variable is an Int"
if type(myVar) = type(1.0)
    print "The variable is a Float"
From here you should be able to write a isnumeric function easily.
isnumeric could look like this
def isnumeric(myVar):
    '''checks via type() if myVar is numeric or not'''
    if type(myvar)==type(1) or type(myvar)==type(1L) or \
       type(myvar)==type(1.1) or type(myvar)==type(1+1j) :
        return 1
        return 0

© 1999-2004 Synop Pty Ltd