0% found this document useful (0 votes)
3 views3 pages

Unit 1 Python

The document discusses key differences between Python 2 and Python 3, focusing on syntax changes like the print function and the behavior of division operations. It emphasizes that Python 3 promotes floating-point results for division and disallows leading zeros in integer literals. The examples illustrate how these changes improve consistency and reduce errors for beginners in programming.

Uploaded by

stardecos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Unit 1 Python

The document discusses key differences between Python 2 and Python 3, focusing on syntax changes like the print function and the behavior of division operations. It emphasizes that Python 3 promotes floating-point results for division and disallows leading zeros in integer literals. The examples illustrate how these changes improve consistency and reduce errors for beginners in programming.

Uploaded by

stardecos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. >>> print 'Hello, World!

'

Output (Python 3):

python

File "", line 1

print 'Hello, World!'

SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello, World!')?

Explanation:

This yields a SyntaxError, since a Python 2 print statement never takes parentheses. In
Python 3 however, print is a function and takes parentheses:

python

print ('Hello, World!')

This is among the various changes that Python 3 brings forth, so I have another guarantee
that I am working in Python 3 here.

2. >>> 1/2

Output (Python 3):

python

0.5

Explanation:

In Python 3, an int divided by int gives a floating-point result and returns a float, while in
Python 2 1/2 would have returned 0 because of integer division. Python 3 is intended to
promote division to return what is generally considered mathematically accurate by default.

3. >>> type(1/2)
Output (Python 3):

python

Explanation:

Since 1/2 evaluates to 0.5, a float, type() identifies it correctly as , which confirms that the /
operator corresponds to the behavior defined in Python 3, and is also a demonstration of
Python's dynamic nature.

4. >>> print(01)

Output (Python-3):

python

File "", line 1

print(01)

SyntaxError: leading zeros in decimal integer literals are not permitted; use a 0o prefix for
octal integers

Explanation:

In Python 3, an integer with a leading zero like 01 is invalid unless it is explicitly defined as
octal using the 0o prefix (0o1 for April in Python 3), so as to avoid subtle bugs in the
program. Python 2 interpreted 01 as an octal representation.

5. >>> 1/(2/3) Output (Python 3):

python 1.5

Explanation: (2/3) results in what is essentially two over three, which is around zero point six
seven. Thus, one divided by that amount yields fifteen halves.

Working on these commands and analyzing the errors and outputs resolves my doubt that
this is a Python 3 environment and I do comprehend the gaps that there are with this syntax
in the other version.
In this case, (2/3) amounts to roughly 0.6667, and calculating 1 divided by that gives us 1.5.
Python follows the rules of arithmetic hierarchy and calculates using floating-point numbers.
The outcome demonstrates that while using parentheses for grouping expressions and
computations, Python performs accurate calculations on provided numbers.

Summary:

These examples highlight the main differences between Python 2 and Python 3 in syntax
(print) and numerical behavior (/). According to Zelle (2017), Python 3 was developed to
enforce consistency and to reduce beginner errors, one of which is the confusion between
integer division and float division.

Discussion Question:

What is the difference between using the / and // operators in Python, and how might using
the wrong one affect the outcome of a program?

Reference:

Zelle, J. M. (2017). Python programming: An introduction to computer science (3rd ed.).


Franklin, Beedle & Associates.

You might also like