Checking var is a number?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dav.phillips@ntlworld.com

    #1

    Checking var is a number?

    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

  • Laszlo Nagy

    #2
    Re: Checking var is a number?

    dav.phillips@nt lworld.com írta:[color=blue]
    > 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.
    >[/color]
    There are different types of number in Python.

    Integers and Long integers (type: 'int', 'long')
    Decimal numbers (class 'decimal.Decima l')
    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:

    ....[color=blue][color=green][color=darkred]
    >>> print_type(12)[/color][/color][/color]
    this is an integer[color=blue][color=green][color=darkred]
    >>> print_type(12L)[/color][/color][/color]
    this is a long integer[color=blue][color=green][color=darkred]
    >>> print_type(3.5)[/color][/color][/color]
    this is a float[color=blue][color=green][color=darkred]
    >>> print_type('hel lo world')[/color][/color][/color]
    this is something else...[color=blue][color=green][color=darkred]
    >>> print_type('44' )[/color][/color][/color]
    this is something else...[color=blue][color=green][color=darkred]
    >>> d = Decimal('123')
    >>> print_type(d)[/color][/color][/color]
    this is a decimal[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    [color=blue]
    > The project i have been given to work in is all CGI written
    > in Python.
    >[/color]
    Probaby you wanted to convert a string into a number? For example:
    [color=blue][color=green][color=darkred]
    >>> int(s)[/color][/color][/color]
    1234[color=blue][color=green][color=darkred]
    >>> s = 'Foo'
    >>> int(s)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: invalid literal for int(): Foo[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Or you can catch the exception:
    [color=blue][color=green][color=darkred]
    >>> s = 'Foo2'
    >>> try:[/color][/color][/color]
    .... intval = int(s)
    .... except ValueError:
    .... print "This cannot be converted to an int."
    ....
    This cannot be converted to an int.[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Good luck!

    Laszlo


    Comment

    • Fredrik Lundh

      #3
      Re: Checking var is a number?

      dav.phillips@nt lworld.com wrote:
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > Also can anyone recommend a reference book apart from dive into python
      > preferably a reference with good examples of how to impliment code.[/color]

      you can find an extensive list of available books here:



      some on-line code collections:




      and don't forget the core references:

      While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python. It...

      This reference manual describes the syntax and “core semantics” of the language. It is terse, but attempts to be exact and complete. The semantics of non-essential built-in object types and of the ...


      </F>

      Comment

      • dav.phillips@ntlworld.com

        #4
        Re: Checking var is a number?

        > Good luck![color=blue]
        > Laszlo[/color]

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

        Cheers
        David P

        Comment

        • dav.phillips@ntlworld.com

          #5
          Re: Checking var is a number?

          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

          Comment

          • Fredrik Lundh

            #6
            Re: Checking var is a number?

            dav.phillips@nt lworld.com wrote:
            [color=blue]
            > 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.[/color]

            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:



            </F>

            Comment

            Working...