Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a number is odd or even in python [duplicate]

Tags:

python

I'm trying to make a program which checks if a word is a palindrome and I've gotten so far and it works with words that have an even amount of numbers. I know how to make it do something if the amount of letters is odd but I just don't know how to find out if a number is odd. Is there any simple way to find if a number is odd or even?

Just for reference, this is my code:

a = 0  while a == 0:     print("\n \n" * 100)     print("Please enter a word to check if it is a palindrome: ")     word = input("?: ")      wordLength = int(len(word))     finalWordLength = int(wordLength / 2)     firstHalf = word[:finalWordLength]     secondHalf = word[finalWordLength + 1:]     secondHalf = secondHalf[::-1]     print(firstHalf)     print(secondHalf)      if firstHalf == secondHalf:         print("This is a palindrom")     else:         print("This is not a palindrom")       print("Press enter to restart")     input() 

Thanks

like image 452
user3320350 Avatar asked Feb 17 '14 19:02

user3320350


People also ask

How do you check if a number is even or odd in Python?

The required code is provided below. num = int (input (“Enter any number to test whether it is odd or even: “) if (num % 2) == 0: print (“The number is even”) else: print (“The provided number is odd”) Output: Enter any number to test whether it is odd or even: 887 887 is odd.

How do you check if a number is odd or even?

If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd. You can check for this using num % 2 == 1 .

How do you check if a number is even or odd without modulus in Python?

Check Even / Odd without using modulus or bitwise operator:number=int(input("Please Enter a Number : ")); x=int(number/2)*2; if(x==number): print("This Number is Even")

What is the meaning of == in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and !=


2 Answers

if num % 2 == 0:     pass # Even  else:     pass # Odd 

The % sign is like division only it checks for the remainder, so if the number divided by 2 has a remainder of 0 it's even otherwise odd.

Or reverse them for a little speed improvement, since any number above 0 is also considered "True" you can skip needing to do any equality check:

if num % 2:     pass # Odd else:     pass # Even  
like image 165
DeadChex Avatar answered Oct 20 '22 02:10

DeadChex


Similarly to other languages, the fastest "modulo 2" (odd/even) operation is done using the bitwise and operator:

if x & 1:     return 'odd' else:     return 'even' 

Using Bitwise AND operator

  • The idea is to check whether the last bit of the number is set or not. If last bit is set then the number is odd, otherwise even.
  • If a number is odd & (bitwise AND) of the Number by 1 will be 1, because the last bit would already be set. Otherwise it will give 0 as output.
like image 30
lejlot Avatar answered Oct 20 '22 04:10

lejlot