dimanche 7 octobre 2018

For Python 3: How can I use a list to avoid repeating 'and/or' operators over and over again?

My code looks something like:

if x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6:
    print('Text')

So I'm wondering if I can combine all those conditions into a list, so something like:

if x == {1,2,3,4,5,6}:
    print('Text')

to make my code cleaner and also it's less work for me.

Thanks.

Aucun commentaire:

Enregistrer un commentaire