CS1101 Discussion Assignment Unit 1 - The difference between Pyhton 3 and Python 2

Questions

Programming languages like Python are formal languages with strict syntax rules. But those rules can change over time with newer versions of the programming language. Your textbook covers Python 3, but you may only have access to Python 2.

Download and install a working Python environment, preferably Python 3, or get an account with PythonAnywhere. Refer to the Software Requirements/Installation section of the course syllabus for details.

Type the statements below into your Python interpreter. For each statement, copy the output into your Discussion Assignment and explain the output. Compare it with any similar examples in the textbook, and describe what it means about your version of Python.

1
2
3
4
5
>>> print 'Hello, World!'
>>> 1/2
>>> type(1/2)
>>> print(01)
>>> 1/(2/3)

1. Output and Explanation

1.1. Statement 1

Codes:

1
>>> print 'hello world'

Output in Python 3:

1
2
3
4
  File "<stdin>", line 1
print 'hello world'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?

Output in Python 2:

1
hello world

Explanation:
In Pyhton3, a print statement is a function, and we need to use parentheses to indicate this. So, if we miss the parentheses, the interpreter will return a SyntaxError. Hence, we need to add parentheses when we use a print statement in Python3.

In contrast, in Python2, we will get the words “hello world.” This is because the print statement is not a function in Python2, so we do not need to include parentheses.

1.2. Statement 2

Codes:

1
>>> 1/2

Output in Python 3:

1
0.5

Output in Python 2:

1
0

Explanation:
In Pyhton3, the output is a floating-point number 0.5. While in Python2, the output is an integer 0.

In Python3, when we do integer division, the interpreter will convert it to floating-point division. Because 1 and 2 are both integers, when 1 is divided by 2, the result will be a floating-point number.

However, in Python2, when we do integer division, only the integer part of the result is retained. We should note that it is not rounded to the nearest number. Just as 1 divided by 2 should be equal to 0.5, but in Python 2, the result is the integer part 0.

1.3. Statement 3

Codes:

1
>>> type(1/2)

Output in Python 3:

1
<class 'float'>

Output in Python 2:

1
<type 'int'>

Explanation:
In Pyhton3, the result of 1/2 is 0.5. Because 0.5 is a floating-point number, so the result is the type float. However, in Python2, the result of 1/2 is 0. Because 0 is an integer, so the result is the type int.

1.4. Statement 4

Codes:

1
>>> print(01)

Output in Python 3:

1
2
3
4
File "<stdin>", line 1
print(01)
^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

Output in Python 2:

1
1

Explanation:
Python is a formal language with strict syntax rules. Although leading zeros in decimal integer literals are ok in mathematics, In Pyhton 3, it is not permitted. So the interpreter returned a SyntaxError. By contrast, in Python 2, leading zeros in decimal integer literals are allowed, and the interpreter will return an integer without leading zeros. So the interpreter returned an integer 1.

1.5. Statement 5

Codes:

1
>>> 1/(2/3)

Output in Python 3:

1
1.5

Output in Python 2:

1
2
3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

Explanation:
In 1/(2/3), parentheses have the highest precedence. So we first calculate 2/3 and get a result. Then divide 1 by the result.

In Python 3, the interpreter will convert integer division to floating-point division. It will first calculate 2/3 and get a result, which is about 0.6666666666666666, and then divide 1 by this result. Finally, we get 1.5.

However, in Python 2, when we do integer division, only the integer part of the result is retained. So 2/3, the result is 0. But 1/0 is not allowed because the divisor cannot be 0. So the interpreter returns a ZeroDivisionError: integer division or modulo by zero.

2. Comparing statements with similar examples in the textbook

2.1. Statement 1

Statement1 in the discussion:

1
>>> print 'Hello, World!'

Similar example in the textbook:

1
>>> print ('Hello, World!')

Explanation:
The statement print ‘Hello, World!’ can be executed correctly in Python 2. However, in Python3, the interpreter will return a SyntaxError. This is because print is a function, so we need to add parentheses when we use it.

The statement print (‘Hello, World!’) can be executed correctly both in Python3 and Python 2.

2.2. Statement 2

Statement2 in the discussion:

1
>>> 1/2

Similar example in the textbook:

1
>>> 6*7

Explanation:
In Python 3, the interpreter will convert integer division to floating-point division. So by executing 1/2, we get 0.5. However, the interpreter does not convert integer multiplication to floating-point multiplication, so when calculating 6*7, the interpreter returns the integer 42.

2.3. Statement 3

Statement 3 in the discussion:

1
>>> type(1/2)

Similar example in the textbook:

1
>>> type(42.0)

Explanation:
In Python3, the interpreter will convert integer division to floating-point division. So by executing 1/2, we get 0.5, and 0.5 belongs to the type float. Besides, 42.0 is a floating-point number, so it belongs to the type float.

2.4. Statement 4

Statement 4 in the discussion:

1
>>> print(01)

Similar example in the textbook:

1
>>> 3+ = 3$6

Explanation:
Both Python and mathematics are formal languages, and they have strict syntax rules. Leading zeros in decimal integer literals are not permitted In Python 3, just like $ is not allowed in mathematics.

2.5. Statement 5

Statement 5 in the discussion:

1
>>> 1/(2/3) 

Similar example in the textbook:

1
>>> 6**2 + 6

Explanation:
In Python 3, the interpreter will convert integer division to floating-point division. It will first calculate 2/3 and get a result, and then divide 1 by this result. Finally, we get 1.5. Besides, the example in the textbook shows that the operator ** performs exponentiation. So when calculating 6**2, we get 36, and then, 36 + 6, we get 42.

References

1.Downey, A. (2015). Think Python. Green Tea Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf

​


This is the ending, thanks for reading.