Please tell Differnce between this...
def validate_pin(pin): if pin.isdigit() and len(pin)==(4 or 6): return True else: return False def validate_pin(pin): if pin.isdigit() and len(pin) in [4,6]: return True else: return False What is the difference between these???please help
12/5/2020 6:51:23 AM1000 Subscribers Without Video Challenge
32 AnswersNew Answer
This is a common problem that arises with Python since it tries to be as closer to Plain English as possible. In Python, and some other languages, the keyword "or" refers to the Boolean Operator OR (https://realpython.com/python-or-operator/). The former code (the correct one for the task) checks if the returned value of len(pin) is present in the sequence [4, 6], i.e: "Is it 4 or 6?". For more info on the "in" Operator see: [https://www.w3schools.com/python/ref_keyword_in.asp]. As Benjamin Jürgens corrected me, "4 or 6" evaluates to 4, because in Python any non-zero number (negative included) is evaluated as True, so it returns the first operand, which is 4. So, what's really happening is in the first code is: "if pin.isdigit() and len(pin)==4".
do as Nicko12 said or use tuple.. also you can use list as well that you used in your second function.. if len(pin) in (4,6):
Nop i got this question in codwars ..the pin should return true if 1.length should be 4 or 6 2.contains no string..all should be digit But codwars says first try is wrong..but second is correct
First function : in your first function see if condition.. it will return True only if pin is digit and length of pin is 4.. it will return False if pin is digit and length of pin is 6. second function : in if condition you are checking if len(pin) in [4,6] it means it will return True if pin is digit and length of pin is 4 or 6.
first one does not include "6" Maybe the requirement length is 4 or 6 so in the first one it is only 4, thats why the second is correct print(4 or 6) #Output: 4 print([4,6]) #Output: [4,6]
print(bool(4 or 6)) all numbers except 0 are True and you need a bool syntax to print boolean (True False)
1000 Subscribers Without Video Challenge why it does not include 6? first understand the working of OR >>> 4 or 6 4 >>> 6 or 4 6 this is how it works hope u got it!
if len(string) == 4 or len(string) == 6: pass #Your second code is more effective and compact tho
are the output same?
Oh it works that way?? I didn't knew sorry
That makes sense but logically shouldn't python return true if the length is 4 or 6..that's why i put 'OR' there
Why it does not include 6☹️
yeah, and it is not actually useful to use print 2 or more values with OR in between, if you want to output different numbers everytime you print it, use random module instead maybe im going a little off the topic but this will become useful in other things
(4 or 6) in the first one evaluates to 4 because four is considered as True and the boolean or operator returns the first operand if it is true. So like Ratnapal Shende said only for a length of four it can return true
Ok i got it...then can u guys tell what is the best way to check if that length is either 4 or 6 (should return true if either of them is correct )
vijay sorry, but that is wrong. Please read the other comments for the correct answer
AbdulaOru don't spam! you can write your three answers in just one and also you can edit it whenever you want...