Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Mark Lawrence

On 19/01/2015 09:01, contro opinion wrote:


In the python3 console:

 >>> a=18
 >>> b='18'
 >>> str(a) == b
 True
 >>> int(b) == a
 True


Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
a1=0xf4
a2=0o36
a3=011

b1='0xf4'
b2='0o36'
b3='011'




Giving a completely different take on previous answers how about.

a1, b1 = b1, a1
a2, b2 = b2, a2
a3, b3 = b3, a3

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Peter Otten
Jussi Piitulainen wrote:

> Peter Otten writes:

>> >>> "{:o}".format(0xf4)
>> '364'

>> To add a prefix just put it into the format string.
 
> There's also these (in Python 3.2.3):
> 
>   >>> hex(0xf4)
>   '0xf4'

D'oh!

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Jussi Piitulainen
Peter Otten writes:
> contro opinion wrote:

> > Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
> > a1=0xf4
> > a2=0o36
> > a3=011
> > 
> > b1='0xf4'
> > b2='0o36'
> > b3='011'
> 
> Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
> [GCC 4.8.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> "{:o}".format(0xf4)
> '364'
> >>> "{:x}".format(0xf4)
> 'f4'
> >>> "{}".format(0xf4)
> '244'
> 
> To add a prefix just put it into the format string.

There's also these (in Python 3.2.3):

  >>> hex(0xf4)
  '0xf4'
  >>> oct(0xf4)
  '0o364'
  >>> bin(0xf4)
  '0b0100'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Frank Millman

"contro opinion"  wrote in message 
news:ca+ydq_651x0ndpw1j203wgbedtxy_mw7g0w3vh1woagr1iv...@mail.gmail.com...
> In the python3 console:
>
>>>> a=18
>>>> b='18'
>>>> str(a) == b
>True
>>>> int(b) == a
>True
>
>
> Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
> a1=0xf4
> a2=0o36
> a3=011
>
> b1='0xf4'
> b2='0o36'
> b3='011'
>
I am no expert, but does this answer your question ?

C:\>python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.


>>> a1 = 0xf4
>>> a1
244
>>> b1 = '0x{:x}'.format(a1)
>>> b1
'0xf4'
>>> int(b1, 16)
244


>>> a2 = 0o36
>>> a2
30
>>> b2 = '0o{:o}'.format(a2)
>>> b2
'0o36'
>>> int(b2, 8)
30


>>> a3 = 011
  File "", line 1
a3 = 011
   ^
SyntaxError: invalid token
>>>

Frank Millman





-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Peter Otten
contro opinion wrote:

> In the python3 console:
> 
> >>> a=18
> >>> b='18'
> >>> str(a) == b
> True
> >>> int(b) == a
> True
> 
> 
> Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
> a1=0xf4
> a2=0o36
> a3=011
> 
> b1='0xf4'
> b2='0o36'
> b3='011'

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "{:o}".format(0xf4)
'364'
>>> "{:x}".format(0xf4)
'f4'
>>> "{}".format(0xf4)
'244'

To add a prefix just put it into the format string.

You can specify the base for int() or trigger automatic base recognition by 
passing a base of 0:

>>> int("f4", 16)
244
>>> int("0xf4", 0)
244
>>> int("0o36", 0)
30

The classical prefix for base-8 numbers is no longer recognised in Python 3:

>>> int("011", 0)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 0: '011'
>>> int("011")
11
>>> int("011", 8)
9


-- 
https://mail.python.org/mailman/listinfo/python-list


How to change the number into the same expression's string and vice versa?

2015-01-19 Thread contro opinion
In the python3 console:

>>> a=18
>>> b='18'
>>> str(a) == b
True
>>> int(b) == a
True


Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
a1=0xf4
a2=0o36
a3=011

b1='0xf4'
b2='0o36'
b3='011'
-- 
https://mail.python.org/mailman/listinfo/python-list