New course! Every coder should learn Generative AI!
Try a free lesson+ 4
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".
+ 3
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):
+ 1
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.
+ 1
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]
+ 1
print(bool(4 or 6))
all numbers except 0 are True and you need a bool syntax to print boolean (True False)
+ 1
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!
+ 1
if len(string) == 4 or len(string) == 6:
pass
#Your second code is more effective and compact tho
+ 1
Lololololol
0
are the output same?
0
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
0
(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
0
Привет
0
vijay sorry, but that is wrong. Please read the other comments for the correct answer
0
AbdulaOru
don't spam!
you can write your three answers in just one
and also you can edit it whenever you want...
0
There's no practical difference.
0
Benjamin Jürgens thanks for the correction. I edited the answer and mentioned your help.
- 1
Hi
- 1
First one: == (4 or 6)
Other one: in [4, 6]
- 1
Erick Ruh Cardozo the first code works not as you described. (4 or 6) evaluates to 4. "Or" is a logical operator, but it returns the left side if it evaluates to true, otherwise the right side. So 4 evaluates to True, but the return value is 4, not True
- 3
Ohh, now i understand, sorry, i think "you" have problem, sorrt