在python中检查数字是奇数还是偶数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21837208/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a number is odd or even in python
提问by user3320350
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
谢谢
采纳答案by DeadChex
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 2has a remainder of 0it's even otherwise odd.
该%标志是象师只是它检查剩余,因此,如果除以数量2有剩余0,它甚至否则奇怪。
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:
或者反转它们以提高一点速度,因为任何大于 0 的数字也被认为是“真”,您可以跳过需要进行任何相等性检查:
if num % 2:
    pass # Odd
else:
    pass # Even 
回答by Esteban Aliverti
One of the simplest ways is to use de modulus operator %. If n % 2 == 0, then your number is even.
最简单的方法之一是使用 de 模数运算符 %。如果 n % 2 == 0,那么您的数字是偶数。
Hope it helps,
希望能帮助到你,
回答by NPE
The middle letter of an odd-length word is irrelevant in determining whether the word is a palindrome. Just ignore it.
奇数长度单词的中间字母与确定该单词是否为回文无关。只是忽略它。
Hint: all you need is a slight tweak to the following line to make this work for all word lengths:
提示:您只需要对以下行稍作调整即可使其适用于所有字长:
secondHalf = word[finalWordLength + 1:]
P.S. If you insist on handling the two cases separately, if len(word) % 2: ...would tell you that the word has an odd number of characters.
PS 如果你非要分开处理这两种情况,if len(word) % 2: ...会告诉你这个词有奇数个字符。
回答by kylie.a
It shouldn't matter if the word has an even or odd amount fo letters:
单词的字母数量是偶数还是奇数都没有关系:
def is_palindrome(word):
    if word == word[::-1]:
        return True
    else:
        return False
回答by Maxime Lorant
Use the modulo operator:
使用模运算符:
if wordLength % 2 == 0:
    print "wordLength is even"
else:
    print "wordLength is odd"
For your problem, the simplest is to check if the word is equal to its reversed brother. You can do that with word[::-1], which create the list from wordby taking every character from the end to the start:
对于你的问题,最简单的就是检查单词是否等于它的反向兄弟。你可以用 来做到这一点word[::-1],它word通过从结尾到开头获取每个字符来创建列表:
def is_palindrome(word):
    return word == word[::-1]
回答by lejlot
Similarly to other languages, the fastest "modulo 2" (odd/even) operation is done using the bitwise andoperator:
与其他语言类似,最快的“模 2”(奇数/偶数)运算是使用bitwise and运算符完成的:
if x & 1:
   return 'odd'
else:
   return 'even'

