Empty List and Tuples | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Empty List and Tuples

Why does the below code returns True for Lists and False for Tuples? def is_it_true (anything): if anything: print ("yes, it's true") else: print ("no, it's false) is_it_true( [[]]) Yes, it's true is_it_true ( (())) No, it's false

23rd Oct 2019, 8:09 AM
Rini - avatar
2 Antworten
+ 3
When you create tuples with 1 item, you need to put a comma to tell that the parentheses are used to create a tuple, not to affect operation precedence. (5,) -> (5,) (5) -> 5
23rd Oct 2019, 9:19 AM
Seb TheS
Seb TheS - avatar
+ 1
def abc(anything): if anything : print('true') else: print('false') abc(((),)) returns true
23rd Oct 2019, 10:32 AM
Rini - avatar