Checking var is a number?

Discussion in 'Python' started by dav.phillips@ntlworld.com, Jun 6, 2006.

  1. Guest

    Hi,
    I am very new to all this and need to know how to check
    a variable to see if it is a number or not. Also can anyone
    recommend a reference book apart from dive into python
    preferably a reference with good examples of how to impliment
    code.

    The project i have been given to work in is all CGI written
    in Python.

    Many Thanks
    David Phillips
     
    , Jun 6, 2006
    #1
    1. Advertising

  2. Laszlo Nagy Guest

    írta:
    > Hi,
    > I am very new to all this and need to know how to check
    > a variable to see if it is a number or not. Also can anyone
    > recommend a reference book apart from dive into python
    > preferably a reference with good examples of how to impliment
    > code.
    >

    There are different types of number in Python.

    Integers and Long integers (type: 'int', 'long')
    Decimal numbers (class 'decimal.Decimal')
    Floating point numbers (type 'float')

    You can check this way:

    import decimal

    def print_type(var):
    if isinstance(var,int):
    print "this is an integer"
    elif isinstance(var,long):
    print "this is a long integer"
    elif isinstance(var,decimal.Decimal):
    print "this is a decimal"
    elif isinstance(var,float):
    print "this is a float"
    else:
    print "this is something else..."

    Test this:

    ....
    >>> print_type(12)

    this is an integer
    >>> print_type(12L)

    this is a long integer
    >>> print_type(3.5)

    this is a float
    >>> print_type('hello world')

    this is something else...
    >>> print_type('44')

    this is something else...
    >>> d = Decimal('123')
    >>> print_type(d)

    this is a decimal
    >>>


    > The project i have been given to work in is all CGI written
    > in Python.
    >

    Probaby you wanted to convert a string into a number? For example:

    >>> int(s)

    1234
    >>> s = 'Foo'
    >>> int(s)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: invalid literal for int(): Foo
    >>>


    Or you can catch the exception:

    >>> s = 'Foo2'
    >>> try:

    .... intval = int(s)
    .... except ValueError:
    .... print "This cannot be converted to an int."
    ....
    This cannot be converted to an int.
    >>>


    Good luck!

    Laszlo
     
    Laszlo Nagy, Jun 6, 2006
    #2
    1. Advertising

  3. wrote:

    > I am very new to all this and need to know how to check
    > a variable to see if it is a number or not.


    assuming that "variable" means "string object" and "number" means
    "integer", you can use the isdigit predicate:

    if var.isdigit():
    print "all characters in", var, "are digits"

    if you want to check for anything that can be converted to a float, the
    best way is to do the conversion and trap any ValueError that may occur:

    try:
    value = float(var)
    except ValueError:
    print "not a valid float"

    if you want an integer instead, replace "float" with "int".

    if you had something else in mind, let us know.

    > Also can anyone recommend a reference book apart from dive into python
    > preferably a reference with good examples of how to impliment code.


    you can find an extensive list of available books here:

    http://wiki.python.org/moin/PythonBooks

    some on-line code collections:

    http://aspn.activestate.com/ASPN/Python/Cookbook/
    http://effbot.org/zone/librarybook-index.htm

    and don't forget the core references:

    http://docs.python.org/lib/
    http://docs.python.org/ref/

    </F>
     
    Fredrik Lundh, Jun 6, 2006
    #3
  4. Guest

    > Good luck!
    > Laszlo


    I actually managed to get it sorted but i like that way of
    doing it much better actually :)

    Cheers
    David P
     
    , Jun 6, 2006
    #4
  5. Guest

    I took a variable to mean a container for diffirent kinds of
    information
    either strings or integers etc, as i am mainly a asp, php, asp.net
    developer.

    Thanks for the list of references, that will come in very handy

    Cheers Guys
    David P
     
    , Jun 6, 2006
    #5
  6. wrote:

    > I took a variable to mean a container for diffirent kinds of
    > information either strings or integers etc, as i am mainly a
    > asp, php, asp.net developer.


    in python, a variable is a name that refers to a specific object. it's
    the object that has a type and a value, not the variable. this article
    might be somewhat helpful:

    http://effbot.org/zone/python-objects.htm

    </F>
     
    Fredrik Lundh, Jun 6, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    rooster575
    Dec 2, 2003
  2. thomson
    Replies:
    10
    Views:
    2,510
    Eliyahu Goldin
    Jun 20, 2005
  3. thomson
    Replies:
    0
    Views:
    396
    thomson
    Jun 20, 2005
  4. Fred
    Replies:
    3
    Views:
    330
    Alf P. Steinbach
    Aug 10, 2003
  5. Alex Vinokur
    Replies:
    4
    Views:
    484
    Ron Natalie
    Sep 24, 2003
Loading...

Share This Page