0

[SOLVED] Somebody please explain this :

>>> a = 'python' >>> ( a.upper() or a.lower() ) == 'PYTHON' True >>> ( a.lower() or a.upper() ) == 'PYTHON' False >>> ( a.upper() or a.lower() ) == 'python' False

4th Apr 2019, 10:29 AM
Manish Kumar
Manish Kumar - avatar
14 Réponses
+ 7
Anna thanks for explanation
4th Apr 2019, 12:01 PM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 5
Anna Sir..... Then why this so a = 'python ' print(( a.upper() or a.lower() ) == 'PYTHON') print(( a.lower() or a.upper() ) == 'PYTHON') print(( a.upper() or a.lower() ) == 'python') Output 👇 False False False
4th Apr 2019, 11:55 AM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 5
Manish Kumar got it man
4th Apr 2019, 12:07 PM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 3
Sam That's wrong. Whether a string contains a space or not doesn't affect that it will evaluate to True. Even a string that is nothing but a space (' ') will evaluate to True.
4th Apr 2019, 11:48 AM
Anna
Anna - avatar
+ 3
Because when you add additional spaces the strings won't be equal. 'test ' is not the same as 'test'. But both 'test ' and 'test' evaluate to True.
4th Apr 2019, 11:59 AM
Anna
Anna - avatar
+ 3
Manish Kumar Use "if a.lower() == 'add'". a.upper() can never equal an all lowercase string and you don't need any construct with "or" here
4th Apr 2019, 12:54 PM
Anna
Anna - avatar
+ 2
#1. >>> a = 'python' >>> ( a.upper() or a.lower() ) == 'PYTHON' True a.upper () converts the value "a" to "PYTHON", if the first command is triggered before "or", then after "or" the command is ignored, which means "PYTHON" == "PYTHON". #2. >>> ( a.lower() or a.upper() ) == 'PYTHON' False a.lower() converts the value "a" to "python", if the first command is triggered before "or", then after "or" the command is ignored, which means "python" != "PYTHON". #3. >>> ( a.upper() or a.lower() ) == 'python' False With this code, I think you will figure it out yourself.
4th Apr 2019, 11:17 AM
Solo
Solo - avatar
+ 1
The variable a contains a non-empty string ('python'). A non-empty string evaluates to True. If you compare several objects with "or", the first object that evaluates to True will be returned. So the result of a.upper() or a.lower() is 'PYTHON', the result of a.lower() or a.upper() is 'python'. The rest should be obvious
4th Apr 2019, 11:18 AM
Anna
Anna - avatar
+ 1
sam, coz >>>'python ' == 'python' False
4th Apr 2019, 12:03 PM
Manish Kumar
Manish Kumar - avatar
+ 1
Anna thanks.
4th Apr 2019, 1:25 PM
Manish Kumar
Manish Kumar - avatar
0
Only the first function will be executed therefore only the second is true
4th Apr 2019, 10:51 AM
Dragonxiv
Dragonxiv - avatar
0
Probably each line was executed .
4th Apr 2019, 11:10 AM
Manish Kumar
Manish Kumar - avatar
0
Mr Anna , what command should I use in order to run a program which needs user input and that should not be case sensitive ?
4th Apr 2019, 12:08 PM
Manish Kumar
Manish Kumar - avatar
0
#LANGUAGE = PYTHON 3.0 #SIMPLE CALCULATOR # Its working fine. while True: print('*************************WELCOME***************************') print('ENTER \'ADD\' FOR ADDITION.') print('ENTER \'SUB\' FOR SUBTRACTION.') print('ENTER \'MUL\' FOR MULTIPLICATION') print('ENTER \'DIV\' FOR DIVISION.') print('ENTER \'EXIT\' TO END/EXIT THE PROGRAM.') a = input("ENTER ANY OF THE OPTIONS : ") if (a.lower() or a.upper()) == 'add': x = input('ENTER A NUMBER : ') y = input('ENTER ANOTHER NUMBER : ') print('ADDITION OF THE NUMBERS IS : ' + (str(float(x) + float(y)))) elif (a.lower() or a.upper()) == 'sub': x = float(input('ENTER A NUMBER : ')) y = float(input('ENTER ANOTHER NUMBER : ')) print('SUBSTRACTION OF THE NUMBERS IS : ' + (str(float(x) - float(y)))) elif (a.lower() or a.upper()) == 'mul': x = float(input('ENTER A NUMBER : ')) y = float(input('ENTER ANOTHER NUMBER : ')) print('PRODUCT OF THE NUMBERS IS : ' + (str(float(x) * float(y)))) elif (a.lower() or a.upper()) == 'div': try: x = float(input('ENTER A NUMBER : ')) y = float(input('ENTER ANOTHER NUMBER : ')) print('DIVISON OF THE NUMBERS IS : ' + (str(float(x) / float(y)))) except ZeroDivisionError: print('0.0') elif (a.lower() or a.upper()) == 'exit': print('QUITTING THE PROGRAM.') break else : print('POSSIBLY WRONG INPUT , TRY AGAIN.')
4th Apr 2019, 12:49 PM
Manish Kumar
Manish Kumar - avatar