When used together with Booleans, the IndexError may not be raised.
For example, assume
list 1 = [1, 2, 3]
This will return True .
True or True and list1[3] > 3
But this will raise IndexError .
False or True and list1[3] > 3
The first line will read the True
and not continue because there is an or
so the list[3] > 3
doesn't matter and won't be evaluated, instead True is returned.
The second line starts with a False
+ or
requiring it to read the next boolean expressions to return the output. It will read True
and try to evaluate the list[3] > 3
expression which will raise the IndexError
This is because the nature of boolean operatorTrue or True and list1[3] > 3
since you have written this as True or some logic
, as it encountered True
and a trailing or
it will simply ignore rest of the logic because a logic will always be True
if it starts with True
and there is a or
in between but in case of and
it could be False
since True and False
return False
thus it will check for the rest of the logic. And your second one starts with False
so False
can be True if it is being followed by an or
thus it checks for your rest of the logic where it encounters an error
Because index number is till 2. List indexes begin with 0. This will work:
Make your list name right first:
list1 = [1, 2, 3]
False or True and list1[2] > 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.