Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14126

How to filter odd and even numbers with Python code? [closed]

$
0
0

I am learning Python. 🐍
I wrote a simple program that filter odd and even numbers.

Can we have optimization for this code?

while not 0:    a = int(input("a = "))    Aa = abs(a)    q = Aa // 2    r = Aa % 2    # q, r = divmod(Aa, 2)    if q >= 0 and r > 0: print(a, "odd")    elif q > 0 and r == 0: print(a, "even")    elif q == 0 and r == 0: print(a, "neutral")

Output:

me@amadeus:~$ python3 Templates/Aaa = 33 odda = 22 evena = 11 odda = 00 neutrala = -1-1 odda = -2-2 evena = -3-3 odda = 

🍎🐍

P.S.: some colleagues argued that "zero is even".

Dear friends,
I used this logical deductive reasoning.

  1. snake has zero legs.
  2. zero is even.

Therefore, mathematically resulting:

  1. snake has an even number of legs.

Parity visual example

3 apples [🍎🍎🍎]  2 apples [🍎🍎  ]  1 apple  [🍎    ]  0 apples [      ]  

Zero has neutral parity, because zero counts neither an odd nor an even number of apples, as we can see in the example above for 0 apples case.

I used following definition for odd and even numbers.

Definition:
Any integer counts a number of units 1, corresponding to counted objects.
The number of units is odd, if divisible by 2 with remainder.
The number of units is even, if divisible by 2 without remainder.
Zero counts zero units 1, therefore zero has neutral parity,
meaning counting neither an odd nor an even number of units 1,
corresponding to counted objects.


Viewing all articles
Browse latest Browse all 14126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>