Sololearn: Learn to Code
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".
6th Dec 2020, 4:34 PM
Erick Ruh Cardozo
Erick Ruh Cardozo - avatar
+ 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):
5th Dec 2020, 7:28 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 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.
5th Dec 2020, 7:02 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 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]
5th Dec 2020, 7:05 AM
noteve
noteve - avatar
+ 1
print(bool(4 or 6)) all numbers except 0 are True and you need a bool syntax to print boolean (True False)
5th Dec 2020, 7:14 AM
noteve
noteve - avatar
+ 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!
5th Dec 2020, 7:15 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
if len(string) == 4 or len(string) == 6: pass #Your second code is more effective and compact tho
5th Dec 2020, 7:26 AM
noteve
noteve - avatar
+ 1
Lololololol
6th Dec 2020, 12:53 PM
Narsingrao Biradar
Narsingrao Biradar - avatar
0
are the output same?
5th Dec 2020, 7:02 AM
noteve
noteve - avatar
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
5th Dec 2020, 7:18 AM
noteve
noteve - avatar
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
5th Dec 2020, 7:18 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Привет
5th Dec 2020, 5:44 PM
Абдулазим Макуев
Абдулазим Макуев - avatar
0
vijay sorry, but that is wrong. Please read the other comments for the correct answer
6th Dec 2020, 7:35 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
AbdulaOru don't spam! you can write your three answers in just one and also you can edit it whenever you want...
6th Dec 2020, 4:41 PM
Ratnapal Shende
Ratnapal Shende - avatar
0
There's no practical difference.
6th Dec 2020, 5:20 PM
Leonardo Bandeira
Leonardo Bandeira - avatar
0
Benjamin Jürgens thanks for the correction. I edited the answer and mentioned your help.
7th Dec 2020, 1:42 PM
Erick Ruh Cardozo
Erick Ruh Cardozo - avatar
- 1
Hi
6th Dec 2020, 2:40 PM
Freelancer Imran Hossain
Freelancer Imran Hossain - avatar
- 1
First one: == (4 or 6) Other one: in [4, 6]
6th Dec 2020, 3:06 PM
AbdulaOru
AbdulaOru - avatar
- 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
6th Dec 2020, 6:07 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
- 3
Ohh, now i understand, sorry, i think "you" have problem, sorrt
6th Dec 2020, 4:39 PM
AbdulaOru
AbdulaOru - avatar