Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
259 views

CS 1101 Programming Fundamentals - Discussion Assignment Unit 1

CS 1101 Programming Fundamentals - Discussion Assignment Unit 1 Solution - University of the People.

Uploaded by

Steafan Taylor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views

CS 1101 Programming Fundamentals - Discussion Assignment Unit 1

CS 1101 Programming Fundamentals - Discussion Assignment Unit 1 Solution - University of the People.

Uploaded by

Steafan Taylor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Input:

print 'Hello, World!'

Result:
File "", line 1
print 'Hello, World!'
    ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello, World!')?

The result outputted by the Python console is an error in python known as a parsing or syntax error. This
error arises simply because the version being run is Python 3.

In Python 3, the print() function now replaces the print statement, with parameters eliminating the use
of most of the special syntax that is required by the old print statement used in python 2. (van Rossum,
n.d).

Had the line of code been run in version 2 of python, the interpreter would have successfully printed out
Hello, World! to the screen. As stated by Downey (2015), with regards to the print statement, it is not a
function and therefore does not use parentheses.

Furthermore, to help locate the file from which the input originated, the file name and line number are
printed. The parser then printed out the erroneous line of code and marks the earliest point where the
error was detected to further aid with debugging. (“8.1 Syntax Errors”, n.d). Finally, the last line or the
error message indicated in detail what caused the error and offers a solution.

2. Input:
1/2

Output:
0.5

Here is the result of a computation between two integers done by the Python interpreter using an
arithmetic operator. The operator performed a division and because I am running Python 3, it produced
the expected decimal or floating point number of which the type is called float. (Downey, 2015, pp. 3-4).

Conversely, when the same 2 integers were divided in Python 2 the result was 0. What is essentially
happening here is that both numbers in the division are integers, and by association the result is also
expected to be an integer. Therefore anything after the decimal point is cut off. Hence, what should be
0.5 results as 0.

3. Input:
type(1/2)

Output:
As Downey, (2015) states, “If you are not sure type a value has, the interpreter can tell you”. (p. 4). The
type() function returns the type/class of the result of the calculation. Some classes include, ‘int’ or
integers, ‘str’ or strings, and ‘float’ or floating point numbers.

As expected in Python 3, the class of the result is a floating point number. However, as discussed in the
previous example, Python 2 would result in a class ‘int’ because an integer being divided by another is
expected to produce an integer by association.

4. Input:
print(01)

Output:
File "", line 1
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal
integers

“Integers may be coded in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2)”.
(“Python – Hexadecimal, octal, and binary literals,” n.d). In Python 2, octal literals can be coded either by
prefix 0o or 0O (zero and lower-uppercase o) or by just a leading 0.

However in Python 3, the format prefix ‘0o’ now replaces the previous octal form of leading by ‘0’.
(“Python – Hexadecimal, octal, and binary literals,” n.d).
In Python 3, octal literals can only be coded with a leading 0o or 0O (zero and lower-uppercase o).
Therefore as the error states, leading zeros in decimal integer literals are not permitted, and also
suggested corrective action in using the 0o prefix for octal integers.

5. Input:
1/(2/3)

Output:
1.5

The result is as expected of this mathematical calculation. Python 3 handles the change in type where
the division of two integers (2/3), results in a floating point number, 0.6666666666. By which 1 is then
divided (1/0.66666666) resulting in a type float, 1.5.

The result in Python 2 would be different as the initial result of the division of (2/3), would still remain a
class/type ‘int’. Thus 2/3 will result in 0 NOT 0.6666666 and then the final calculation of 1/0 will cause a
zerodivisionerror as it is not possible to divide by 0.

References

Downey, A. (2015). Think python: How to think like a computer scientist (2nd ed.). Needham,
Massachusetts: Green Tea Press.

Van Rossum, G. (n.d.). What's New In Python 3.0. (n.d). Retrieved from
https://docs.python.org/3/whatsnew/3.0.html
Python – Hexadecimal, octal, and binary literals. (n.d). Retrieved from
http://www.java2s.com/example/python-book/hexadecimal-octal-and-binary-literals.html

8.1 Syntax Errors. (n.d.). Retrieved from


https://docs.python.org/2.0/tut/node10.html

You might also like