Is there a proper way to evaluate against Python tuples? [duplicate] - python

This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 5 years ago.
I just caught an issue with something I wrote, and was hoping I could get a proper explanation of what was happening, and the best way to get around it. I have a fix in place, but am not sure if it's the best solution
I have a variable interval that is a string. In this scenario, let's say interval = 'Day'
Code compares this against a tuple in an if and elif expression:
if interval in ('FiscalWeekDaily','Daily'):
...
...
elif interval in ('DayHourly'):
Now I thought 'Day' in ('DayHourly') would evaluate as False but it turns out it evaluates to True. What's the exact explanation for this? I would assume that since there is only 1 element, 'Day' is being compared against that element.
So if I try 'Day' in ('DayHourly', 'whatever'), that evaluates as False because 'Day' is evaluated against each element, right?
So my fix is simply elif interval in ['DayHourly']:. Is that the proper way to go about doing this?

Tuples with single objects in them need to have a comma, it is the comma that makes the tuple, not the parentheses.
In other words:
('DayHourly') == 'DayHourly'
You want ('DayHourly',)

Related

Slicing a String gives no output in python [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 7 months ago.
When i run this code it returns nothing
name = "Azura"
print(name[-1:-6])
Why does this happen ?
Because you're slicing everything in the string from index -1 to index -6. In the case of the string name, after converting negative index to positive, that is index 5 to index 0. In otherwords, when you do print(name[-1:-6]), you're doing print(name[5:0]), which obviously doesn't work, because python slices left to right. Therefore, if you want print(name[0:5]), just do print(name[-6:-1]).
TLDR: BAD print(name[-1:-6]) GOOD print(name[-6:-1])
If you want to use a minus index that's going from right to left you must always use a negative step
print(name[-1:-6:-1])
The direction of the slicing must be the same as the direction of the steps.
Also it's easier to use string[::-1] if you want to reverse a string.

Why does "x and y" return "y" when "x" and "y" are integers? [duplicate]

This question already has answers here:
Python's Logical Operator AND [duplicate]
(7 answers)
Closed last year.
So I came across the following syntax for a coding-game solution:
.
.
n and 9
and I didn't knew how to interpret it, thus I tried
2 and 3 #3
3 and 2 #2
.
Why does x and y (seems to) equal y i.e how is this calculated/understood?
This is called short-circuiting in boolean expressions if the first is true(non zero considered as true) it goes to 2nd if this was or if the first was false(0 is considered false) it goes to 2nd try it or as or integers hope this clears stuff up
Chain of logical operators returns the first (counting from left-most value) item that lets you know the logical value of the whole statement. So, in case of and, there are 2 options - one of the values is False, at which point you know that whole statement is False and you return that value without checking anything further on the right, or all of them are True and you return the last one, as only then you know that it will be True.

What's the name of this operator in Python? [duplicate]

This question already has answers here:
Python list - True/False first two elements? [duplicate]
(2 answers)
Closed 2 years ago.
In a Clash of Code, I've seen this interesting operator thing:
print(["false","true"][i == n])
I haven't seen this before. What's the name of this and what does it do?
It's not exactly an operator but rather the second condition is being used as an index for the ["false", "true"] list.
In case i == n this will be true. Let me remind you that true in python equals to 1
int(True) // = 1
Therefore if i == n, it will be equal to one and will print the element with index one from the list which is "true".
In case i != n, it will be False, equals to 0 which will print the first element from the array which is "false".
This a comparison Operator,
it compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None.
Output: True or False
Usage: Is used to check whether 2 expressions give the same value.

Why is it printing "hurrah" even when the condition are false? [duplicate]

This question already has an answer here:
Why is a tuple of falsey objects truthy? [duplicate]
(1 answer)
Closed 2 years ago.
The highlighted part of the code is telling me to "remove redundant parentheses", but when I remove them, there is a syntax error regarding the comma.
The parentheses and the comma is making the interpreter thinking that the condition a tuple, which then evaluates to (False, False), and python treats tuple with any elements as true.More info about tuples here
To fix this, you have to replace the "," with a boolean operator depending on the situation.
you have to use and/or while u adding two conditions also a==9 included in the condition a>4 so no need of a==9
a=4
if(a>5 or a==9):
print ("hurrah")
you should use and / or instead of comma and remove brackets
if a > 5 or a == 9:
print('hurray')

Conditional with list comprehension (NOT list comprehension with conditional) in python [duplicate]

This question already has answers here:
How do Python's any and all functions work?
(10 answers)
Closed 3 years ago.
Just thought I would specify in the title, because I have found the latter all over the internet, but not the former. I am curious how to get a conditional that contains a list comprehension working in python. Specifically, I am curious about how to do something like the following:
if (abs(value - any_x)) > 100 for any_x in x:
Essentially, I want the program to proceed if the absolute value of the difference between the value and any value in the x array is greater than 100. But the syntax as it stands is incorrect. What exactly am I missing? Thanks and best regards,
-AA
Use any:
if any(abs(value - any_x) > 100 for any_x in x):
...
Don't use a list comprehension here as any will return True on the first True value it finds. Thus providing it a generator is the most efficient method as it will be lazily evaluated.
You can use any.
if any(abs(value - any_x) > 100 for any_x in x):
Pretty simple,
True in [abs(k-value)>100 for k in x]

Categories