This question already has answers here:
What does & mean in python [duplicate]
(5 answers)
Closed 11 months ago.
print(2 & 3)
I came across a problem statement in one of my technical assessments, I can't understand the usecase of this '&' operator. Can anyone help me, with how this & operator work in python3
& is a bitwise operator, so this is simply where the binary bits line up between 2 and 3
https://wiki.python.org/moin/BitwiseOperators
>>> bin(2)
'0b10'
>>> bin(3)
'0b11'
>>> int("0b10", base=2) # binary string -> int (base10)
2
Here's an example with some bigger numbers
>>> bin(12)
'0b1100'
>>> bin(10)
'0b1010'
>>> 12&10
8
>>> bin(8)
'0b1000'
>>> bin(~8) # NOTE 8 is signed
'-0b1001'
>>> 8&-8
8
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 2 years ago.
I've written some script to help me out finding if something is divisible by other thing, and it fails with larger numbers:
print(x, d)
90744169766518547761620274468374441720233236940 10
print(x/d)
9.074416976651854e+45
print(x / (x/d))
10.0
print(x % (x/d))
2.535301200456459e+30
Since 10.0 is clearly lacking decimal part I don't undertand why % is giving me this trash output?
Does this do what you expect?
>>> print(x//d)
9074416976651854776162027446837444172023323694
>>> print(x // (x//d))
10
>>> print(x % (x//d))
0
The difference is that / in Python 3 always produces a floating point result, even if the operands are integers. In this it differs from C. If you want integer division you need to use //.
This question already has answers here:
Integer division in Python 2 and Python 3
(4 answers)
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 2 years ago.
In python2, if you did
n = 12
n /= 10
n would become 1.
In python 2,
the above would cause n to be 1.2, even if it were passed in an integer parameter like
def foo(self, n: int) -> bool:
print (n / 10)
return True
The simple fix is to just cast it to an integer like so:
n = int(n/10)
But this is quite memory/time costly. Are there better alternatives in python3?
// is integer division, so it removes the decimal from the result.
This question already has answers here:
What do these operators mean (** , ^ , %, //)? [closed]
(3 answers)
Closed 4 years ago.
Why is ^ not squaring in Python? I know exponentiation is ** instead, but what exactly is ^ and why wasn't that operator used instead?
For example 2^2=0, 3^2=1.
The ^ operator was already used for bitwise xor.
>>> x = 42; format(x, '08b')
'00101010'
>>> y = 137; format(y, '08b')
'10001001'
>>> z = x ^ y; format(z, '08b')
'10100011'
That leaves the old Fortran-style ** operator for exponentiation.
>>> base = 5
>>> exp = 2
>>> base ** exp
25
The "^" symbol in python is a bit-wise exclusive OR (XOR) operator. An OR gate is true if one of the inputs OR another is true. The XOR gate is true if and only if just a single input is true. 00 and 11 are false. 01 and 10 are true. The bit-wise XOR can be used to check how many bits differ.
For example,
2^2 = 10
^10
= 00
= 0
3^2 = 11
^10
= 01
= 1
This question already has answers here:
Why does Python return 0 for simple division calculation?
(6 answers)
Closed 6 years ago.
So a list from 0 to 100 is created and I apply the formula in line two to make list B. The issue is that it's rounded to an int and I have been trying how to get it out to 3 decimals.
A = range(0,101)
B = [(x-10)/3 for x in A]
Ex:
If A = 11
(11-10)/3 = 0.333 instead of (11-10)/3 = 0.
Thanks!
You can use the function round(B, 3)
this should work
from __future__ import division
A = range(0,101)
B = [(x-10)/3 for x in A]
This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 7 years ago.
I have to convert this value to 23.69 or 23.70 not like that talll number 23.69098712446352
a=552.0
b=23.3
c=a/b
print(c)
str.format the output:
print("{:.2f}".format(c))
Or round:
round(c, 2)
Output:
In [9]: print(c)
23.6909871245
In [10]: print("{:.2f}".format(c))
23.69
In [11]: print(round(c, 2))
23.69
Use round function -
c = round(a/b , 2)
Two ways:
round(a.b, 2)
Or just for display use this:
>>> "%.2f" % 3.14159
'3.14'
>>> print("%.2f" % a)