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

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')

Related

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.

convert list into string? [duplicate]

This question already has answers here:
How to convert list to string [duplicate]
(3 answers)
Closed 2 years ago.
Here is my list ['k:1','d:2','k:3','z:0'] now I want to remove apostrophes from list item and store it in the string form like 'k:1 , d:2, k:3, z:0' Here is my code
nlist = ['k:1','d:2','k:3','z:0']
newlist = []
for x in nlist:
kk = x.strip("'")
newlist.append(kk)
This code still give me the same thing
Just do this : print(', '.join(['k:1','d:2','k:3','z:0']))
if you want to see them without the apostrophes, try to print one of them alone.
try this:
print(nlist[0])
output: k:1
you can see that apostrophes because it's inside a list, when you call the value alone the text comes clean.
I would recommend studying more about strings, it's very fundamental to know how they work.
The parenthesis comes from the way of representing a list, to know wether an element is a string or not, quotes are used
print(['aStr', False, 5]) # ['aStr', False, 5]
To pass from ['k:1','d:2','k:3','z:0'] to k:1 , d:2, k:3, z:0 you need to join the elements.
values = ['k:1','d:2','k:3','z:0']
value = ", ".join(values)
print(value) # k:1, d:2, k:3, z:0
What you have is a list of strings and you want to join them into a single string.
This can be done with ", ".join(['k:1','d:2','k:3','z:0']).

Meaning of the second bracket [] in the print statement in python [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
Below is the code.
I don't understand the meaning of the last bracket in [::-1][:-1], and how come can you write two brackets at the same time. I understand that the first slice bracket reverses the order of the string, but what does the second do?
for i in range(n,0,-1):
temp = list(alphabets[:n][i-1:])
print('-'.join(temp[::-1][:-1]+temp).center(4*n-3,'-'))
Thanks for cooperation!
To answer your question I will use an example:
list = [1,2,3,4,5]
As you said, the first brackets will reverse your list.
Then, you will get as a result: [5,4,3,2,1]
On that list you will do the slicing: [:-1]. That will give you as a result [5,4,3,2].
The brackets meaning is the same:
[<startIndex>:<endIndex>:<how to go through>]
For more information about the <how to go through> part, please read here: https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/

Python (int) and (int,) [duplicate]

This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 3 years ago.
Why type((1)) is int and not a tuple? Whereas type((1,)) gives tuple.
That's also an answer to the question why we should use commas while defining a tuple with one value. Because tuples are not like lists which is unique in a way that we define it (using squared brackets) we have to add the comma to the value. In the first one type((1)) inner paranthesis have no effect, so it's just a basic integer nothing else. Like when you define expressions in paranthesis to give them priority. Hope it helps :)
Python compiler treated (1) as 1 because of that it is showing as int. that is inbuilt behavior of python compiler.
>>> a = (1)
>>> print(a)
1
>>> a = (1,)
>>> print(a)
(1,)

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

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',)

Categories