Python Forum
Using the and operator to check if two values equal a third
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the and operator to check if two values equal a third
#11
Another related question here, I would like to check and see if a value is equal to multiple values using if/or.  Would it be correct to use the statement below?


if a == 'no' or 'yes' or 'maybe':
    do something
Reply
#12
See: https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#13
if a in ('no', 'yes', 'maybe'):
    do something
Reply
#14
(Oct-17-2016, 09:02 PM)nilamo Wrote:
(Oct-17-2016, 09:00 PM)sparkz_alot Wrote: Never mind... took me a moment to wrap my head around what was happening

Exactly why I don't think that should be used :p

You think wrong  Naughty Fresh Python users get confused by list comprehensions - should those be disavowed too?

Or Pythonic non-boolean bolean expressions - instead of writing long chains of if...elses.
Python is ... Pythonic  Heart

BTW, another simple way to check equality of more than one variable - for immutables, like strings, numbers, tuples, frozensets
len(set([a, b, c])) == 1

(Oct-12-2016, 12:06 AM)Yoriz Wrote: The first one would work.
The second would always be true regardless of value a if b and c where equal.
The third would be true if b was either a or c

The second statement is wrong - if a is Null, zero or empty: string, tuple, list, dict -  it will be False
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 491 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Check if two matrix are equal and of not add the matrix to the list quest 3 819 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  is there equal syntax to "dir /s /b" kucingkembar 2 981 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Can a variable equal 2 things? Extra 4 1,481 Jan-18-2022, 09:21 PM
Last Post: Extra
  Python greater than equal to comparison operator Ilangos 4 2,418 Sep-26-2020, 03:53 AM
Last Post: buran
  Check for specific values on screen and2handles 2 2,376 Jun-17-2020, 05:24 AM
Last Post: and2handles
  Not equal a dictionary key value bazcurtis 2 1,916 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,256 Mar-20-2019, 08:01 PM
Last Post: stillsen
  How many variables/values are limited to single assignment operator? Prabakaran141 1 2,027 Sep-06-2018, 03:32 PM
Last Post: Larz60+
  Misunderstanding with the “if” statement and “not equal” scriptoghost 6 4,421 Jun-23-2017, 09:43 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020