The task of determining the rightmost digit of the binary record number
def binar(): b=int(input()) b=bin(b) num=[] num=b.split ln=len(b) g=int(b[ln-1]) if g==1: print ("Yes") elif g==0: print ("No") a=int(input()) for i in range(a): binar() The task of determining the rightmost digit of the binary record number
def binar(): b=int(input()) b=bin(b) num=[] num=b.split ln=len(b) g=int(b[ln-1]) if g==1: print ("Yes") elif g==0: print ("No") a=int(input()) for i in range(a): binar() It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
If you need to determine the least significant digit of a number, then in my opinion, the easiest way is to find out the parity of the number. Obviously, all even numbers have the least significant digit 0, and odd numbers have 1
a=int(input()) if a % 2 == 1: # нечётное print("Yes") else: # чётное print("No") print("Yes" if a % 2 else "No") - gil9redSyntaxError: invalid syntax , and I'm SyntaxError: invalid syntax about print("Yes" if a % 2 else "No") , you can print("Yes" if a % 2 == 1 else "No") , but this is not shorter :) - gil9redprint("Yes" if int(input()) % 2 else "No") - AndreyWithout any checks if:
num = 2 print("This number is"+" not"*(num&1)+" odd") num=b.split is not a call to the split method, it is writing the method itself to the variable num . To call a function, method, or object with the ability to call ("callable") you need parentheses: num = b.split() . Be careful, the absence of brackets when calling can lead to errors. You do not have an error due to the fact that this value is then not used in any way (see clause 2).num . Feel free to throw out two lines.If there is a string with a binary representation of a number, simply take the last character, and check if it is equal to the string "1" without converting the character to an integer. The last character can be obtained at index -1 (respectively, the penultimate - at index -2, etc.). If a symbol has two states ("1" or "0"), then if the symbol is not equal to "1", then it is automatically equal to "0", so you can simply use else instead of elseif .
b=int(input()) b=bin(b) g=b[-1] if g=="1": print ("Yes") else: print ("No") You can check the value of the low-order bit without converting a number to a string in a binary representation — via bitwise operations (see, for example, tproger.ru: About bitwise operations ):
b = int(input()) if b & 1: print ("Yes") else: print ("No") Source: https://ru.stackoverflow.com/questions/897091/
All Articles