How to compare several different terms in a simplified way in Python?

Asked

Viewed 69 times

0

I wanted to know how to show that several terms are different from another term, without having to keep repeating ? As in the example:

if termo1 != valor_aleatorio and termo2 != valor_aleatorio and termo3 != valor_aleatorio:

I tried a few ways, but they were wrong.

2 answers

3


One possibility is to make a pertinence test:

if valor_aleatorio not in (termo1, termo2, termo3):

In the above case it is checked whether the value of the variable valor_aleatorio is not present in the tuple (termo1, termo2, termo3).

-1

You can use all:

if all(termo != valor_aleatorio for termo in (termo1, termo2, termo3)):
  • 1

    Thank you, I understand the logic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.