Bug! Problem in integer datatype

While working on this simple reverse of integer problem I faced one problem in get the last ‘Zero’ element present at the last.
if I not use while or any loop but we can simple use String conversion and reversing it and again converting it to integer
In string there is no issue with zero at the last but when we use String to integer conversion the last zero is not taken to concern .
Kindly look onto this topic…

Thank You!

Hello, @pritamrajak, and welcome to Python Software Foundation Discourse!

Please copy, paste, and format your code for posting, rather than post a picture or captured image of it.

You can use fences of three backticks before and after code to format it, as follows:

```
print("Hello, world!")
```

The following links provide information about that, along with other useful information:

The following is something to thing about, regarding your post:

>>> int("065")
    65

After "065" is converted to an int, the leading 0 is not needed to represent the value of the number, and that’s why it is not there.

EDIT:

Let’s clarify this still further. Regarding your code, after the conversion to int, the value of rev is just 65. The fact that there was a leading "0" in s prior to the conversion becomes irrelevant.

1 Like

0123 and 123 are the same number.

If you need leading zeros, don’t convert back to int from str.

Well, not quite the same when written as a literal in a Python program.

1 Like

0123 as a literal will lead to disappointment. :cry: But the response may also have some educational value. :slight_smile:

On the other hand, "0123" can be converted successfully to an int.

>>> 0123
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
>>> int("0123")
123

>>> 0o123
83

I agree with you, but if you use it for reverse of number or armstrong number. we will file a issue for eg:- 340, 450,500.
Even we use while loop to solve the problem then also we will find the same problem.

Yes, this is the issue the 0 is not detected by python. Just like if we use this for reverse of a number or armstrong number, we will problem .
example :
for a number : - 450
expected output should be : 054
but actual output is : 54

Even if we use while loop to reverse a number it not accepting the last Zero.
Example :
let we take a number say 350
expected number is 053
but the actual output is 53

Hi, @pritamrajak,

Try this, which displays the types and values of variables at each step:

n = int(input("Enter an integer: "))
print(f"n (int): {n}")
s = str(n)
print(f"s (str) before reversal: {s}")
s = s[::-1]
print(f"s (str) after reversal: {s}")
rev = int(s)
print(f"rev (int): {rev}")

When a str that contains leading zeroes is converted to an int, there’s no reason for the leading zeroes to be incorporated into the resulting int.

EDIT:

Consider a str to be stored as a sequence of characters. That could include leading zeroes.

Consider an int to be stored as a numerical value, and not as a sequence of characters. For example, you could store a number as a box of peas. To represent the number 65, you would store 65 peas in the box. Characters are irrelevant, until you ask to display the value of the number. When that happens, 65 is displayed to express the number of peas in the box. There’s no reason for a leading zero to appear.

Not entirely correct, as it’s all about how the number is displayed, not its type or how it is stored.

This simple code will display the number 042, that is referenced as a string object, type convert it to an integer, then display is as 042:

number = "042"

print(f"{int(number):03}")

In fact you could have number = "42" and it would still display as 042

Edit: maybe I should say that the ‘number’ is being converted back to a string, before being displayed.

In math, they’re the same number.

In modern Python 065 is an invalid literal.

In 1.x and 2.x, 065 is an octal constant.

Not entirely correct, as it’s all about how the number is displayed, not its type or how it is stored.

This isn’t really what the OP was asking for though. The OP wanted to reverse digits, not pad a field to a fixed width.

Maybe his/her numbers are always 3 decimal digits long, but that is an assumption at this point.

Fair point.

From what I can see I think that the thread title is a little off, as there is no “Bug!”, just a misunderstanding about how to display numbers in whatever way one needs to.

As we know, there is no bug in how Python handles integers in this example, so the title can be misleading in that regard. In reality, we know that the bug is in the program.

The issue here is likely that Python beginners sometimes mistakenly believe that Python should store numbers as a series of decimal digits. By extension, they may then conclude that if we specify a number with leading zeroes, those zeroes, along with the digits that follow, should all get stored when we assign the number to a variable. The mindset there matches one that is appropriate for a string, rather than one that is appropriate for a number.

1 Like

Yes; it’s not often that I display numbers, within the apps that I build, without first constructing the format with which to display said number, dev code aside, that is.

Edit: @pritamrajak I’ll add this in here, rather than in another post.

To solve the issue (as I understand it to be, but maybe my understanding is off), I would simply add to the reverse.py script like this.

n = int(input("Enter the number: "))

s = str(n)
s = s[::-1]
rev = int(s)
fill = len(s)
print(f"reverse {str(rev).zfill(fill)}")

Institutions such as museums and botanical gardens typically assign accession numbers to objects or plants as they acquire them. Often, this consists of a four digit year, followed by a four digit number, with leading zeroes preserved, when necessary. That second component starts at 0001 at the beginning of the year, and increments by 1 with each new object that is acquired.

Essentially, then, each accession number is a string of eight characters, but conceptually, it has a blend of the characteristics of a number and those of a string. Sometimes a hyphen is placed between the year and the second component.

The idea behind @pritamrajak’s original post has merit with regard to the above.