Filipe Andrade Filipe Andrade - 2 months ago 23
Python Question

How to pick one number of an Integer

How can I pick one of the digits from an Integer like: 97723
and choose (for example) the number 2 from that number and check if its an odd or an even number?

Also, can I print only the odd numbers from an Integer directly? (Is there any default function for that already?)

Thanks in advance

Answer

2 is the 4th digit.

You can get the digits of a number using this construct.

digits = [int(_) for _ in str(97723)]

This expression will be true if the 4th digit is even.

digits[3] % 2 == 0