How to find the type ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lad

    How to find the type ...

    Hello
    How can I find out in Python whether the operand is integer or a
    character and change from char to int ?
    Regards,
    L.

  • robert.dowell@gmail.com

    #2
    Re: How to find the type ...

    >>> thisisastring = "1"[color=blue][color=green][color=darkred]
    >>> thisisanint = 1
    >>> type(thisisastr ing)[/color][/color][/color]
    <type 'str'>[color=blue][color=green][color=darkred]
    >>> type(thisisanin t)[/color][/color][/color]
    <type 'int'>[color=blue][color=green][color=darkred]
    >>> thisisastring = int(thisisastri ng)
    >>> thisisanint = str(thisisanint )
    >>> type(thisisastr ing)[/color][/color][/color]
    <type 'int'>[color=blue][color=green][color=darkred]
    >>> type(thisisanin t)[/color][/color][/color]
    <type 'str'>[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comment

    • Magnus Lycka

      #3
      Re: How to find the type ...

      Lad wrote:[color=blue]
      > Hello
      > How can I find out in Python whether the operand is integer or a
      > character and change from char to int ?[/color]

      I'm not sure what you mean by "character" in a Python context.
      A string? "i = int(i)" will make sure both 5 and "5" are used
      as 5, and "five" will be rejected with a ValueError.
      [color=blue][color=green][color=darkred]
      >>> def f(x):[/color][/color][/color]
      .... i = int(x)
      .... print i, type(i)
      ....[color=blue][color=green][color=darkred]
      >>> f(5)[/color][/color][/color]
      5 <type 'int'>[color=blue][color=green][color=darkred]
      >>> f('42')[/color][/color][/color]
      42 <type 'int'>[color=blue][color=green][color=darkred]
      >>> f('infinity')[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 2, in f
      ValueError: invalid literal for int(): infinity

      Comment

      • Steve Holden

        #4
        Re: How to find the type ...

        Lad wrote:[color=blue]
        > Hello
        > How can I find out in Python whether the operand is integer or a
        > character and change from char to int ?
        > Regards,
        > L.
        >[/color]
        Easiest would just be to apply the int() type function to whatever you
        have and trap any resulting exception.
        [color=blue][color=green][color=darkred]
        >>> getInt(1)[/color][/color][/color]
        1[color=blue][color=green][color=darkred]
        >>> getInt("32767")[/color][/color][/color]
        32767[color=blue][color=green][color=darkred]
        >>> getInt('have a banana')[/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "<stdin>", line 5, in getInt
        ValueError: getInt called with non-integer value

        Unfortunately we should also consider:

        [color=blue][color=green][color=darkred]
        >>> getInt("3.14159 ")[/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "<stdin>", line 5, in getInt
        ValueError: getInt called with non-integer value[color=blue][color=green][color=darkred]
        >>> getInt(3.14159)[/color][/color][/color]
        3[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        which may or may not be what you want.

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC www.holdenweb.com
        PyCon TX 2006 www.python.org/pycon/

        Comment

        • Rocco Moretti

          #5
          Re: How to find the type ...

          robert.dowell@g mail.com wrote:[color=blue][color=green][color=darkred]
          >>>>thisisastri ng = "1"
          >>>>thisisani nt = 1
          >>>>type(thisis astring)[/color][/color]
          >
          > <type 'str'>
          >[color=green][color=darkred]
          >>>>type(thisis anint)[/color][/color]
          >
          > <type 'int'>
          >[color=green][color=darkred]
          >>>>thisisastri ng = int(thisisastri ng)
          >>>>thisisani nt = str(thisisanint )
          >>>>type(thisis astring)[/color][/color]
          >
          > <type 'int'>
          >[color=green][color=darkred]
          >>>>type(thisis anint)[/color][/color]
          >
          > <type 'str'>[/color]
          [color=blue][color=green][color=darkred]
          >>> print repr(thisisastr ing)[/color][/color][/color]
          1[color=blue][color=green][color=darkred]
          >>> print repr(thisisanin t)[/color][/color][/color]
          '1'[color=blue][color=green][color=darkred]
          >>> thisisastring = 'a'
          >>> thisisanint = 98
          >>> thisisastring = ord(thisisastri ng) #Using ASCII rep.
          >>> thisisanint = chr(thisisanint )
          >>> type(thisisastr ing)[/color][/color][/color]
          <type 'int'>[color=blue][color=green][color=darkred]
          >>> type(thisisanin t)[/color][/color][/color]
          <type 'str'>[color=blue][color=green][color=darkred]
          >>> print repr(thisisastr ing)[/color][/color][/color]
          97[color=blue][color=green][color=darkred]
          >>> print repr(thisisanin t)[/color][/color][/color]
          'b'




          Comment

          • Steven Bethard

            #6
            Re: How to find the type ...

            Lad wrote:[color=blue]
            > How can I find out in Python whether the operand is integer or a
            > character and change from char to int ?[/color]

            Python doesn't have a separate character type, but if you want to
            convert a one-character string to it's ASCII number, you can use ord():
            [color=blue][color=green][color=darkred]
            >>> ord('A'), ord('z')[/color][/color][/color]
            (65, 122)

            The answer to your first question is that you probably don't want to.
            You probably want two separate functions, one that takes an integer and
            one that takes a character. What's your actual function look like?

            STeVe

            Comment

            • Xavier Morel

              #7
              Re: How to find the type ...

              Lad wrote:[color=blue]
              > Hello
              > How can I find out in Python whether the operand is integer or a
              > character and change from char to int ?
              > Regards,
              > L.
              >[/color]
              You may want to try the "type" command.
              And there is no character type in cPython (unless you're using ctypes
              that is)

              There is not much point though, you can use the "int" construct on your
              expression, Python'll try to convert the expression to an integer by
              itself (and throw an exception if it can't)
              [color=blue][color=green][color=darkred]
              >>> a = 3
              >>> int(a)[/color][/color][/color]
              3[color=blue][color=green][color=darkred]
              >>> a = "3"
              >>> int(a)[/color][/color][/color]
              3[color=blue][color=green][color=darkred]
              >>> a = "e"
              >>> int(a)[/color][/color][/color]

              Traceback (most recent call last):
              File "<pyshell#5 >", line 1, in -toplevel-
              int(a)
              ValueError: invalid literal for int(): e[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              You can even do base conversions with it:
              [color=blue][color=green][color=darkred]
              >>> a="0xe"
              >>> int(a)[/color][/color][/color]

              Traceback (most recent call last):
              File "<pyshell#7 >", line 1, in -toplevel-
              int(a)
              ValueError: invalid literal for int(): 0xe[color=blue][color=green][color=darkred]
              >>> int(a,16) # Silly me, 0xe is not a decimal[/color][/color][/color]
              14[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              Comment

              • Martin Christensen

                #8
                Re: How to find the type ...

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1
                [color=blue][color=green][color=darkred]
                >>>>> "Lad" == Lad <python@hope.cz > writes:[/color][/color][/color]
                Lad> How can I find out in Python whether the operand is integer or a
                Lad> character and change from char to int ?

                In Python, the canonical way of doing this would be to simply assume
                that the argument can be converted to an integer and catch any errors
                that occur:

                def f(x):
                try:
                x = int(x)
                except ValueError:
                # It's a non-number string.
                do stuff
                except TypeError:
                # It's neither a number nor a string.
                do some other stuff

                Martin
                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.1 (GNU/Linux)
                Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

                iEYEARECAAYFAkO ZwtkACgkQYu1fMm OQldXEzACgqdDVv x29UBVSIfQWnGRi AAk9
                xPsAn0yN5jWrUN+ 6SKIHdwtILRBVyQ wR
                =HZQq
                -----END PGP SIGNATURE-----

                Comment

                • Scott David Daniels

                  #9
                  Re: How to find the type ...

                  Xavier Morel wrote:[color=blue]
                  > You can even do base conversions with it:[color=green][color=darkred]
                  > >>> a="0xe"
                  > >>> int(a)[/color][/color]
                  > Traceback (most recent call last):
                  > File "<pyshell#7 >", line 1, in -toplevel-
                  > int(a)
                  > ValueError: invalid literal for int(): 0xe[color=green][color=darkred]
                  > >>> int(a,16) # Silly me, 0xe is not a decimal[/color][/color]
                  > 14[/color]
                  Or even say "look to the text for a clue about the base":
                  [color=blue][color=green][color=darkred]
                  >>> int('0xe', 0)[/color][/color][/color]
                  14[color=blue][color=green][color=darkred]
                  >>> int('010', 0)[/color][/color][/color]
                  8[color=blue][color=green][color=darkred]
                  >>> int('10', 0)[/color][/color][/color]
                  10

                  --
                  -Scott David Daniels
                  scott.daniels@a cm.org

                  Comment

                  • Lad

                    #10
                    Re: How to find the type ...

                    Thank you ALL for help and explanation
                    Regards,
                    L.

                    Comment

                    Working...