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,)
Related
This question already has answers here:
python: why does replace not work?
(2 answers)
Closed 1 year ago.
a = 'dog'
a.replace('dog', 'cat')
print (a)
Really basic question, the function seems to be fairly straightforward but it just isn't replacing in this instance for some reason - is it because replace doesn't inherently change "a"?
Yes, you are correct. It won’t modify a.
Replace function will return a replaced string.
So, if you like to replace the text in a. Use the below code.
a = 'dog'
a = a.replace('dog', 'cat')
print (a)
Strings are immutable data types in Python which means that its value cannot be updated. Variables can point at whatever they want.
str.replace() creates a copy of string with replacements applied. See documentation.
Need to assign a new variable for the replacement.
a = 'dog'
b = a.replace('dog', 'cat')
print(b)
Output:
cat
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')
This question already has answers here:
What is the syntax rule for having trailing commas in tuple definitions?
(10 answers)
Closed 5 years ago.
Can somebody explain why this creates a list of 5 elements:
['']*5
While this create a tuple of 1 element
('')*5
But this creates a tuple with six elements:
('', '')*3
Question: is there a way to create tuple with an odd number of elements without using a generator (i.e. by using *)?
('') is actually not a tuple, it is a string. You want to write ('',).
This question already has answers here:
How does tuple comparison work in Python?
(4 answers)
Closed 5 years ago.
What is this python code doing?
min((2,3),(6,'f',1))
Output: (2, 3)
I am not able to follow the documentation.
Can someone explain why the output in (2,3) and not an error?
Because (2,3) < (6,'f',1)
Meaning tuples are compared itemwise, therefore 2 < 6 yields that the first tuple is less than the second one
While this code works on Python 2 and Python 3, it should fail on Python 3 if both items in 1st place were the same. Because it would compare 3 to the string 'f' and such comparison is now invalid.
The min function will call the comparator methods of the objects you pass. In this case, all tuples. It is returning the minimum tuple with respect to lexicographic order.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
Please explain me what does this piece of code do.
h should be 32Byte result from sha256 calculation.
I am rewriting parts of this code for my project in C++ and I'm not sure if this switches byte order per 4byte chunk or change byte order on whole 32byte number.
def reverse_hash(h):
return struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
And, how does this array index work ?
[::-1]
Thanks for any and all info
[::-1] creates a new list with reversed order of elements
[::-1] reverse order of elements in a list, so this script change order of each 4-bytes subsequence (in order to change endiannes, I suppose):
>>> h = ''.join(map(str, range(0,21)))
>>> h
'01234567891011121314151617181920'
>>> struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
'32107654019821114131615181710291'
Equivalent expression:
>>> struct.pack('<IIIIIIII', *struct.unpack('>IIIIIIII', h))
'32107654019821114131615181710291'