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()
In this article, We will see how to write the program to find the given number is Even or Odd Using the Recursion Method. If the number is even returned true else false in Python. For that, we use the Operator Called Modulus Operator. This operator used in the operation when we need to calculate the remainder of that number when divided by any divisor.
Examples: Input: 2 Output: True Explanation : 2 % 2==0 So True Input: 5 Output: False Explanation : 2 % 2 != 0 So, FalseMethods #1: Approach :
Output: 3 num is oddMethods #2: We use the modulus operator to find the even or odd Like if num % 2 == 0 then the remainder is 0 so the given number is even and return True Else if num % 2 != 0 then the remainder is not zero so the given number is odd and return False
Output: 3 num is oddMethods #3: To print all the even number for the range (a, b) Here We will print all the even number for the Given Range n in the Function evenOdd(n)
Output: 2 4 6 8 10
def find(num): # code logic here if num%2 == 0: numtype="even" else: numtype = "odd" return numtype num = int(input('Enter the number: ')) # 1. take your input numtype = find(num) # 2. call the find function print('Given number is',numtype). # 3. print if the number is even or odd coder# python challenge07.py Enter the number: 5 Given number is odd coder# python challenge07.py Enter the number: 8 Given number is even
Avoid usage of else block by assigning a default value (odd) to numtype def find(num): # code logic here numtype = "odd" if num%2 == 0: numtype="even" return numtype num = int(input('Enter the number: ')) # take your input numtype = find(num) # call the find function print('Given number is',numtype) # print if the number is even or odd Output:coder# python challenge07.py Enter the number: 5 Given number is odd coder# python challenge07.py Enter the number: 8 Given number is even Solution #3directly return numtype def find(num): # code logic here if num%2 == 0: return "even" return "odd" num = int(input('Enter the number: ')) # take your input numtype = find(num) # call the find function print('Given number is',numtype) # print if the number is even or odd Output:coder# python challenge07.py Enter the number: 5 Given number is odd coder# python challenge07.py Enter the number: 8 Given number is even Bonusnumtype = num%2 == 0 and "even" or "odd" Find the video explanation also
In this tutorial, we will discuss the Python program to check a number is even or odd using the function In this program, we are going to learn about how to find the odd or even number from given number using function in the Python language. First, we must understand what is even or odd? What is Even or Odd When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10 and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers In my previous post, I have explained the various ways to check whether the number is even or odd in Python language However, in this program, we embed the logic of the check even or odd numbers program in the function You can embed the logic of the program to check even or odd numbers using any of the following approaches in the function Once This program received the number it will check the given number either odd or even. After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output Using modular operatornum=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num): if(num%2==0): print(num," Is an even") else: print(num," is an odd") find_Evenodd(num);//function callWhen the above code is executed, it produces the following results Case 1 Enter a number for check odd or even: 349 349 is an oddCase 2 Enter a number for check odd or even: 234 234 is an evenUsing Bitwise operatornum=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num)://function definition if(num&1==1): print(num, "is an odd number"); else: print(num, "is an even number"); find_Evenodd(num);//function callWhen the above code is executed, it produces the following results Case 1 Enter a number for check odd or even: 678 678 is an even numberCase 2 Enter a number for check odd or even: 765 765 is an odd numberUsing the division operatornum=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num)://function definition number=(num/2)*2 if(number==num): print(num, "is a even number"); else: print(num, "is a odd number"); find_Evenodd(num);//call the functionWhen the above code is executed, it produces the following results Case 1 Enter a number for check odd or even: 678 678 is a even numberCase 2 Enter a number for check odd or even: 987 987 is a odd numberSuggested for you Python operator python if else statements Python function Similar post Python program to check whether a number odd or even Python program to display even and odd number in the given range Python code to display all even and odd numbers from 1 to n Python code to display all even and odd numbers without if |