why does print 2++3 print 5 in python? [duplicate] - python

This question already has answers here:
Why does 1+++2 = 3?
(6 answers)
Closed 7 years ago.
I am new to python and I read that there is no ++ operator in Python but I am not able to understand the below code.
>>>print (2++3)
>>>5

+ and - act as unary as well as binary operators. So,
a ++ b is same as a + (+b)
a -+ b is same as a - (+b)
a -- b is same as a - (-b)
a +- b is same as a + (-b)
As can be seen below
>>> 2++3
5
>>> 2-+3
-1
>>> 2--3
5
>>> 2+-3
-1

This is not the ++ operator. Your code is interpreted as follows:
2 + (+3)
Now since +3 is 3, the final result is 5. For fun, try the following:
2++++++++++++++3
2+++++++++++++++++++++-8
See Expressions for details.

Related

C# mod gives wrong answer not as python mod (%) [duplicate]

This question already has answers here:
modulo of a number - python vs c#
(3 answers)
Mod of negative number is melting my brain
(15 answers)
Closed 7 months ago.
I'm trying to get % result of ( -1%26 )
the result should be 25
in python it's right
but C# gives -1 as result
why and how to get 25 in C# not -1
You can use a different kind of modulus function, like this:
int Modulus(int a, int b) {
return ((a % b) + b) % b;
}
Console.WriteLine(Modulus(-1, 26)); // prints 25

Python: C code for-loop that multiplies the loop variable [duplicate]

This question already has answers here:
Is there any method like divide by or multiply by in python range()?
(4 answers)
Closed 2 years ago.
The C code here:
for(i = 1; i <= 1000; i *= 3){
//task
}
How can I write this in python with a for loop?
So far I can only think of a solution using while loops:
i = 1
while i <= 1000:
#task
i *= 3
Is there a way I can write this with a for loop in python?
try this
step = 3
_i=1
for x in range(0,1000):
if x%_i!=0:
continue
_i*=step
# task
print(x)

Boolean operator precedence [duplicate]

This question already has answers here:
Python operators precedence
(2 answers)
Closed 2 years ago.
I have question about Python operator precedence.
print(not (8 < 4) or (10 == 5 * 2) and not (5 > 3))
The above code prints 'True'. But I think the result is 'False', with the following steps:
>>print(not F or T and not T)
>>print(T or T and F)
>>print(T and F)
>>print(F)
So I don't know why the result is 'True'
Maybe I'm missing the small detail.
and has higher precedence than or.
>>print(not F or T and not T)
>>print(T or T and F)
>>print(T or (T and F)) # Evaluated like this.
>>print(T or F)
>>print(T)
Adding the following (unnecessary) brackets will explain how it's getting evaluated:
print((not (8 < 4)) or ((10 == 5 * 2) and not (5 > 3)))
Since (not (8 < 4)) is evaluated to True (the other side of the or doesn't matter) this is the returned result

Python hangs on consecutive power calculation [duplicate]

This question already has answers here:
Why is exponentiation applied right to left?
(4 answers)
Closed 4 years ago.
Python 2.7.13 interpreter hangs on calculating
1 ** 2 ** 3 ** 4 ** 5
But why?
I think the problem here is your understanding of exponentiation associativity:
>>> 2**3**4
2417851639229258349412352
>>> 2**(3**4)
2417851639229258349412352
>>> (2**3)**4
4096
You need to reed from right to left unlike division associativity.
>>> 2/3/4
0.16666666666666666
>>> 2/(3/4)
2.6666666666666665
>>> (2/3)/4
0.16666666666666666

C++ syntax when declaring an integer [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 8 years ago.
I'm having a difficult time learning python syntax. I've been doing some algorithms for a merge sort and I've run into a bit of an issue.
def arrMerge(a):
for i in range(1,len(a), *2):
for j in range(0,len(a)-1,2*i):
end2 = (2*i < len(a) -j) ? 2*i : len(a) -j
this block in python any ideas on how I should go about executing it?
I assume that you are asking what is the Python equivalent syntax to C++ ternary operator. In Python you would use a conditional expression that has the syntax value if condition else other_value.
So your assignment would become:
end2 = 2 * i if 2 * i < len(a) - j else len(a) - j
It is usually better to use a plain if though:
if 2 * i < len(a) - j:
end2 = 2 * i
else:
end2 = len(a) - j

Categories

Resources