0% found this document useful (0 votes)
797 views

CS1101 DiscussionAssignmentU1

CS1101, Discussion Assignment Unit 1, UoPeople, Computer Science, Fundamental Programming,

Uploaded by

Ahmad
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
797 views

CS1101 DiscussionAssignmentU1

CS1101, Discussion Assignment Unit 1, UoPeople, Computer Science, Fundamental Programming,

Uploaded by

Ahmad
Copyright
© © All Rights Reserved
You are on page 1/ 3

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 the
Virtual Computer Lab or 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.

print 'Hello, World!'

1/2

type(1/2)

print(01)

1/(2/3)

When you reply to your peers’ submissions, compare their results with yours.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Microsoft Windows [Version 10.0.5]

(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\user>python

Python 3.8.3 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> print 'Hello, World!'

File "<stdin>", line 1

print 'Hello, World!'

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

0.5

>>> type(1/2)

<class 'float'>

>>> 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

>>> 1/(2/3)

1.5

>>>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The first statement:

>>> print 'Hello, World!'

The first output:

File "<stdin>", line 1

print 'Hello, World!'

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

Explaining the output:

This is a syntax error that takes place due to miss parentheses in the print function in python 3. When
we compare it with the similar example in the textbook, we find that in Python 3, the print statement is
a function, so it has to use parentheses. Otherwise, it doesn't use them because the print statement is
not a function in Python 2.

The second statement:

>>> ½

The second output:

0.5
Explaining the output:

We use the operator / to perform division in Python, when we divide one by two, we get answer 0.5.

The third statement:

>>> type(1/2)

The third output:

<class 'float'>

Explaining the output:

We use the type statement to get the category of value, so the value ½ is a floating-point number belong
to float.

The fourth statement:

>>> print(03)

The fourth output:

File "<stdin>", line 1

print(03)

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

Explaining the output:

This is a syntax error that takes place when we use leading zeros in decimal integer literals, which are
not allowed; we do use 0o instead of just 0 to indicate octal integers in Python 3 but it doesn’t necessary
in Python 2.

The fifth statement:

>>> 1/(2/3)

The fifth output:

1.5

Explaining the output:

The Python interpreter executes the arithmetic operator inside the parentheses first.

You might also like