is theres a way to make one variable to have multiple meanings

Viewed 67

is there any way that i make x = "y" = "Y", so when i type x == "y" or x == "Y" both shows True. ive tried :

x = ["y", "Y"]

x == "y"

x == "Y"

but both still says False

2 Answers

IIUC, you need .any() and check each element with 'y' or 'Y' like below:

>>> [i == 'y' for i in x]
[True, False]

>>> any(i == 'y' for i in x)
True


>>> [i == 'Y' for i in x]
[False, True]

>>> any(i == 'Y' for i in x)
True

This would work. Please check if it did:

x = ("y", "Y")
for i in enumerate(x):
    print(i[1] == "y")

What this code does is it creates a tuple of both the values and then runs a for loop using an enumerate class, which tells the current index as well as the current index's value and stores it in a variable (the variable is 'i' in here). Example - i = (0, "y") it means that its the first loop (0) and the value is 'y'. then it checks whether i[1] ("y" as per the above example) is == "y" (or whatever) or not.

Hope this answer helped you solve your problem and understand it as well.

Related