How do I determine an odd number in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I determine an odd number in python?

`if x %2 == 0:` does it for even but what would include 1 using modulo as an odd number?

29th Jun 2022, 10:47 AM
ρү૨œdԌ૨ ×
ρү૨œdԌ૨ × - avatar
9 Answers
0
If x%3 == 0: print("odd") Else: Print("even")
29th Jun 2022, 10:49 AM
Waqar Ahmed
+ 4
Taking the Waqar's code: Suppose x = 6 Then 6 % 3 = 0 but it would print "odd". Suppose x = 7 Then 7 % 3 = 1 but it would print "even". (edited)
29th Jun 2022, 11:03 AM
Lisa
Lisa - avatar
+ 4
ρү૨œdԌ૨ × if x % 2 == 0 is even then... x % 2 != 0 would be odd Of course you will have to determine how you want to classify 0
29th Jun 2022, 12:39 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
Even: If you can express the number as 2*k where k is an integer, then its remainder wgen divided by 2 is 0 Odd: 2*k + 1 So because (2*k + 1) % 2 != 0, it's odd. (edit: the divisor is 2, the quotient is k, remainder is 1) So you're still using mod 2. There's no mod 1 if that’s what you're asking. adding 1 makes an odd num even: (2*k + 1) + 1 = 2*k + 2 = 2*(k +1) --------- Mods: ---------- if x is equivalent to c in mod a Then in the decimal system (base 10), x = ka + c where the quotient k is a natural number (N = {0, 1, 2,...}), such that: if mod 3: x =~ 0 if x = {0, 3, 6,...} remainder 0 if divided by 3 aka multiples of 3 x =~ 1 if x = {1, 4, 7,...} rem 1 x =~ 2 if x = {2, 5, 8,...} rem 2 So if you have mod x the largest integer in mod x is x-1 So, if 1 is the largest number you can use, you are dealing with mod(2). You cannot have mod 1, because no number will have a remainder when divided by 1, it'd be just zero. Every number would be equivalent to zero, so long as they are integers.
29th Jun 2022, 3:41 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Lisa so 0 is odd? that would more sense than it being even but less sense than it being zero... OH i see. Its less false.
29th Jun 2022, 11:06 AM
ρү૨œdԌ૨ ×
ρү૨œdԌ૨ × - avatar
+ 1
ρү૨œdԌ૨ × The number which is divided by 2 (reminder 0) is a Even number otherwise Odd number (reminder 1)
29th Jun 2022, 3:12 PM
A͢J
A͢J - avatar
0
Alternative to "if x % 2 != 0": If x % 2 > 0
30th Jun 2022, 11:05 AM
Python-Fakir
Python-Fakir - avatar
- 1
Waqar Ahmed thank you 🤖
29th Jun 2022, 10:50 AM
ρү૨œdԌ૨ ×
ρү૨œdԌ૨ × - avatar
- 1
if x%2== 0: print("odd") else: print("even") If a number divided by 2 and remainder become 0 then it will an even number .
29th Jun 2022, 4:25 PM
Vaibhav
Vaibhav - avatar