Different result for simple math in python 2 and 3 [duplicate] - python

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.

Related

How to run calculations [duplicate]

This question already has answers here:
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 2 years ago.
I am new to programming and just started a course and stuck on a demo section where I have to take a math calculation and program it in Python. When I program it do you follow BEDMAS order of figuring out or is there a logical order. The syntax is correct but I keep being told it is not right
See the equation below to be programmed into python. What are the rules for telling Python to calculate something correctly where 5 is the exponent
Use Python to calculate (((1+2)∗3)/4)5
Exponentiation in Python is done with the ** operator.
For instance:
2**8
is 256.
In Python to calculate exponent you use ** operator.
Similar to multiplication but double asterisk.
(((1+2)∗3)/4)**5
This will give you 32.
That is because you are using values which are all integers, so 9 / 4 will be 2, not 2.25.
Python has operator precedence that follows algebraic rules.

Difference between zip() functions in Python 2 and Python 3 [duplicate]

This question already has answers here:
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 4 years ago.
I was wondering what the difference is between the zip() function in python 2 and python 3 is. I noticed when using the timeit module on both functions that the python 3 function was a lot faster. Thanks so much in advance :)
Difference between Python 2 and Python 3 is Python 3 returns an iterators. Idea of this saving memory.
In Python 3, the zip() function returns an iterator, meaning that you can only exhaust elements once, whereas Python 2 returns an iterable itself.
see here:
Python 2 Doc, Python 3 Doc

Why print(a++) has syntax errors in python, but not print(++a) [duplicate]

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

Python - Division and floating point numbers [duplicate]

This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 6 years ago.
According to the Python tutorial here https://docs.python.org/3/tutorial/introduction.html
this gives a floating point number:
>>> 17 / 3 # classic division returns a float
5.666666666666667
However, when I try it I get:
>>> 17 / 3
5
How come? FWIW, I'm using Python 2.7.10 on the Mac.
That behaviour changed between Python 2.7 and 3.
The answer is in the URL:
https://docs.python.org/3/tutorial/introduction.html
https://docs.python.org/2.7/tutorial/introduction.html
The URL you posted is for Python 3, but you are using Python 2.7.
There's a dropdown at the top of the page. You can select 2.7 from there to get the documentation for Python 2.7:
>>> 17 / 3 # int / int -> int
5

Python is not showing floating decimals correctly [duplicate]

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

Categories