Professional Documents
Culture Documents
Discussion Unit 1
Discussion Unit 1
Introduction
● In this Discussion we are required to compare some features of Python 3 and
Python 2 by running five statements and analyzing their outputs.
● Most students only have one version of Python installed on their systems. One of
the possible ways to experiment with both versions of Python is to use
pythonanywhere.com
● This assignment was performed on an Ubuntu 20.10 Operating System (OS).
● To run python 3 in a terminal (the Windows command line counterpart on Linux
systems), I type python - this might differ on your OS, as I have configured my IDE
to suit my personal needs.
● To run python 2 in a terminal, I type python2 - again, this might differ on your OS.
First statement
With Python 3
>>> print 'Hello, World!'
File "<stdin>", line 1
print 'Hello, World!'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean
print('Hello, World!')?
Python 3 throws an eror message because the print statement is a function in python
3 and expects parentheses.
With Python 2
>>> print 'Hello, World!'
Hello, World!
The print statement is not a function in Python 2 and expects a string operand.
Second statement
With Python 3
>>> 1/2
0.5
With Python 2
>>> 1/2
0
This means 1/2 outputs a whole number (int type). In Python 2, the / operator performs
integer division.
Third statement
With Python 3
>>> type(1/2)
<class 'float'>
The type function returns the type of 1/2 and it is a floating-point number (fractional
number).
With Python 2
>>> type(1/2)
<type 'int'>
In Python 2, the type function returns the type of 1/2 and it is an integer number (whole
number).
Fourth statement
With Python 3
>>> print(01)
File "<stdin>", line 1
print(01)
^
SyntaxError: leading zeros in decimal integer literals are not
permitted; use an 0o prefix for octal integers.
This is a syntax error meaning that Python 3 does not support leading zeros before
numbers as they are reserved to a special category of numbers called octal integers.
If the output was expected to be 1, we can go around this error by wrapping the octal
number into strings and then casting it to integer as follows:
>>> print(int('01'))
1
With Python 2
>>> print(01)
1
Python 2 supports leading zeros and automatically casts them into integers.
Fifth statement
With Python 3
>>> 1/(2/3)
1.5
To better understand how this statement is executed according to PEMDAS rules
(Downey, 2015, 12), we can decompose this statement into small statements as
follows:
>>> 2/3
0.6666666666666666
>>> 1/0.6666666666666666
1.5
With Python 2
>>> 1/(2/3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> 2/3
0
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
We observe that the first statement outputs a whole number (0). So when we divide 1 by
0, Python 2 throws a ZeroDivisionError with a message explaining that integers
cannot be divided or modulated by zero.
References
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press