This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 5 years ago.
I have python2.7 installed on ubuntu 16.04. It's showing strange behavior. If I check for (5/4), it will just display 1 as output.
for (6/7) it is showing 0
Can anyone help me to fix this?
Not a strange error at all. Python 2.7 does Integer division, so you will always get an integers as the result of the expressions you have named. You can view it this way if you want:
5/4=int(5/4)=int(1.25) = 1
And this is how you can understand the expression you have shown:
float(5/4)= float(int(5/4)) = float(1) = 1.0
you need to use floats:
5.0/4.0
Related
This question already has answers here:
Division in Python 3 gives different result than in Python 2
(3 answers)
Closed 1 year ago.
Can someone help me figure out why the same simple calculation gets different answers in python 2 and 3? The expression is (0.2**(-2)-1)**(1/2).
When I use python 2 in a Canopy IDE, I get 1.
When I use python 3 in google colab, I get 4.98.
In both cases I am literally running exactly the above expression. Any ideas?
Integer division works differently in Python 2 and 3.
For example (1/2) will return
0 in Python 2, and
0.5 (a float) in Python 3.
This question already has an answer here:
Preincrement operators in python
(1 answer)
Closed 5 years ago.
I am a beginner in python and I am using Python 3.5. The python console complains invalid syntax for the below statement:
a = 5
print(a++)
But print(++a) works fine. Can anyone help me understand the difference?
Btw, it seems that print(a+=1) also doesn't work.
Thanks!
++a is just the same as doing (+(+a)). I.E: You're using the mathematical addition operator on the variable a (with implied zeroes). So the result is a
a++ is not valid python syntax (unlike other languages).
a += 1 is an assignment. It is equivalent to a = a + 1 - you can not print an assignment
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
https://i.imgur.com/YYNhvNv.png
Its adding in 1 at the end of that console log output. Why?
Answer is suppose to be only 13.37
Likely because it is adding to numbers of type double (I am not a huge Python expert though, so I could be wrong). The doubletype, much like single does not have perfect precission. Hence some decimal errors can occur...
This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I was playing around with Python. I had a doubt about the power operation in Python. So, I tried this:
0726**13 = 54609997061205831773270000000000000L
726**13 = 15565965698792536237226936270158258176L
Why is there a difference between these two? I know it might be trivial. But, I could not figure it out. Could someone please explain? Thanks.
It's because an integer constant beginning with 0 is taken to be an octal value. In this case, 0726 is interpreted as 470:
>>> 0726
470
>>> 470**13
54609997061205831773270000000000000L
>>>
Numbers starting with 0 in Python are represented in Base 8 (octal numbers). That's why you're getting different results.
This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I am trying to solve a problem using python. In which I have to deal with large integers (upto 500 digits). According to my current stage of understanding, python can handle any numbers in same traditional way. But I have problem in simple addition like this:
>>> p= 1001101111101011011100101100100110111011111011000100111100111110111101011011011100111001100011111010
>>> q= 0011111011111010111101111110101101111001111111100011111101101100100011010011111011111110110011111000
>>> p+q
1001101111105557844987142979708366943425581971579987152809865568761000527613931421735161949470823522L
Can anyone please explain why i got such an error.
Var q starts with a zero, making it an octal number, rather than decimal