Asked  7 Months ago    Answers:  5   Viewed   37 times

When I type small integers with a 0 in front into python, they give weird results. Why is this?

>>> 011
9
>>> 0100
64
>>> 027
23

I'm using Python 2.7.3. I have tested this in Python 3.0, and apparently this is now an error. So it is something version-specific.

They are apparently still integers:

>>> type(027)
<type 'int'>

 Answers

36

These are numbers represented in base 8 (octal numbers). Some examples:

Python 2 (old format)

Note: these forms only work on Python 2.x.

011 is equal to 1?8¹ + 1?8? = 9,

0100 is equal to 1?8² + 0?8¹ + 0?8? = 64,

027 is equal to 2?8¹ + 7?8? = 16 + 7 = 23.

Python 3 (new format)

In Python 3, one must use 0o instead of just 0 to indicate an octal constant, e.g. 0o11 or 0o27, etc. Python 2.x versions >= 2.6 supports both the new and the old format.

0o11 is equal to 1?8¹ + 1?8? = 9,

0o100 is equal to 1?8² + 0?8¹ + 0?8? = 64,

0o27 is equal to 2?8¹ + 7?8? = 16 + 7 = 23.

Tuesday, June 1, 2021
 
Joegramming
answered 7 Months ago
34

If variable_1 evaluates to False , x is set to 0, otherwise to variable_1

Think of it as

if variable_1:
  x = variable_1
else:
  x = 0
Wednesday, July 28, 2021
 
braindamage
answered 5 Months ago
72

This only makes sense with NumPy arrays. The behavior with lists is useless, and specific to Python 2 (not Python 3). You may want to double-check if the original object was indeed a NumPy array (see further below) and not a list.

But in your code here, x is a simple list.

Since

x < 2

is False i.e 0, therefore

x[x<2] is x[0]

x[0] gets changed.

Conversely, x[x>2] is x[True] or x[1]

So, x[1] gets changed.

Why does this happen?

The rules for comparison are:

  1. When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

  2. When you order a numeric and a non-numeric type, the numeric type comes first.

  3. When you order two incompatible types where neither is numeric, they are ordered by the alphabetical order of their typenames:

So, we have the following order

numeric < list < string < tuple

See the accepted answer for How does Python compare string and int?.

If x is a NumPy array, then the syntax makes more sense because of boolean array indexing. In that case, x < 2 isn't a boolean at all; it's an array of booleans representing whether each element of x was less than 2. x[x < 2] = 0 then selects the elements of x that were less than 2 and sets those cells to 0. See Indexing.

>>> x = np.array([1., -1., -2., 3])
>>> x < 0
array([False,  True,  True, False], dtype=bool)
>>> x[x < 0] += 20   # All elements < 0 get increased by 20
>>> x
array([  1.,  19.,  18.,   3.]) # Only elements < 0 are affected
Sunday, August 8, 2021
 
Goudgeld1
answered 4 Months ago
42

1) Invoking python 2.7

In short: don't do this. There are reasons why the path is called '/usr/lib/python*2.6*/site-packages/'.

One reason is, that in this directory typically the 'compiled' python files (.pyc) are stored. python 2.6 and python 2.7 .pyc files are not compatible:

$ python2.7 /usr/lib/python2.6/sitecustomize.pyc
RuntimeError: Bad magic number in .pyc file

python will skip pyc files which it cannot understood, but you loose at least the benefits of precompiled files.

Another reason is, that things might get mixed up:

$ strace -f python2.7 /usr/lib/python2.6/sitecustomize.py
...
stat("/etc/python2.6", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/etc/python2.6", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/etc/python2.6/apport_python_hook", 0x7fffa15601f0) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hook.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hookmodule.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hook.py", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hook.pyc", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib/python2.7/apport_python_hook", 0x7fffa15601f0) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hook.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hookmodule.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hook.py", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hook.pyc", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib/python2.7/plat-linux2/apport_python_hook", 0x7fffa15601f0) = -1 ENOENT (No such file or directory)
...

I would in your case install the modules needed also for python 2.7 in the python2.7 directory.

2) Invoking python 2.6

You might want to have a look at the part of the man page where PYTHONHOME is described:

PYTHONHOME: Change the location of the standard Python libraries. By default, the libraries are searched in ${prefix}/lib/python[version] and ${exec_prefix}/lib/python[version], where ${prefix} and ${exec_prefix} are installation-dependent directories, both defaulting to /usr/local

You can store the python 2.7 specific files / modules in the appropriate directory in your local installation. Those files / modules will only be picked up when you run the specific version of python. In this case you must not set the PYTHONPATH (or PYTHONHOME).

Note: this is exactly the way Debian (and maybe other distributions) manage different simultaneously installed versions of python.

[Edit: Added section 1 after receiving a comment from niboshi.]

Friday, August 13, 2021
 
tedders
answered 4 Months ago
58

use the following to convert to a timestamp in python 2

int((mod_time.mktime(first_run.timetuple())+first_run.microsecond/1000000.0))

Sunday, August 22, 2021
 
waylaidwanderer
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share