[Tutor] Taking user input from keyboard inside list comprehension
Manprit Singh
manpritsinghece at gmail.com
Sun Sep 6 02:30:07 EDT 2020
Dear sir ,
Consider a problem in which i have to input a string made of 0 &1(Binary
characters) in the following form:
1111,1011,1010,1001,1110
the string contains groups of four characters made of 0&1 and each group
separated with a comma.
Now the output that I want should only contain those groups separated with
a comma whose binary to decimal conversion is divisible by 5. The output
for the string given above will be :
'1111,1010'
If i write a single line code like this in python shell :
>>> ",".join([i for i in input("Enter the string").split(",") if int(i,
base=2) % 5 ==0])
it will first prompt you to input the binary string through keyboard and
then it will immediately display the result, as given above
My question is, as you can see in the example I have taken input from
keyboard inside the list comprehension, is it valid to do so in python ?
Regards
Manprit Singh
More information about the Tutor
mailing list