Learning Journal - CS1101 Programming Fundamentals - Unit 1
Learning Journal - CS1101 Programming Fundamentals - Unit 1
PART 1
1 a. In the following example I attempt to output the words “Hello, World” to the screen but
intentionally omitting one of the quotation marks.
Code:
>>> print('Hello, World)
File "<stdin>", line 1
print('Hello, World) SyntaxError: EOL while scanning string literal
^
>>>
On running the line of code, the error “SyntaxError- EOL while scanning string literal” shows. This
particular error is an EOL (End of line) error that indicated that the interpreter expected a certain
character or characters in a specific line of code but wasn’t found before the line ended. In this case it
expected the closing quotation mark.
1 b. In this instance of the code, I attempted to print out the word “Hello” while omitting the quotation
marks entirely.
Code:
>>> print(Hello)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Hello' is not defined
>>>
Upon running this code, a traceback occurs, which is essentially a report containing function calls made
at a specific point in the code. As we know, the typical information is provided to help located the
file/script and line that the input could have originated from and most importantly, specifics on the
error.
In this case it is a NameError, which signifies, that there was a reference made to a name that hasn’t
previously been defined.
2. In the following lines of code, some simple mathematical calculations are carried out. The results of
the codes below are as such because the Python interpreter adheres to the basic math rules regarding
positives and negative integers.
Rules:
(Positive & Positive = Positive)
(Negative & Negative = Negative)
(Negative & Positive = Negative)
(Positive & Negative = Negative)
Code:
a.
>>> 2++2
4
Rule Applied: (Positive & Positive = Positive)
b.
>>> 2--2
4
Rule Applied: (Negative & Negative = Positive)
c.
>>> 2+-2
0
Rule Applied: (Positive & Negative = Negative)
d.
>>> 2-+2
0
>>>
Rule Applied: (Negative & Positive = Negative)
3.
>>> print(02)
File "<stdin>", line 1
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal
integers
>>>
It is true that in math notation, leading zeros are ok, but in Python it is not. The results differ depending
on which version of Python being used. It is relevant to state that Integers may be coded in decimal
(base 10), hexadecimal (base 16), octal (base 8), or binary (base 2)
In Python 2, both leading zero and prefix 0o or 0O (zero and lower-uppercase o) can be used to code
Octal literals.
However in Python 3, only the prefix 0o or 0O (zero and lower-uppercase o) Is allowed for octal integers.
Therefore the leading zero must be removed in order to parsed.
4.
>>> 2 2
File "<stdin>", line 1
22
^
SyntaxError: invalid syntax
>>>
What happened in the code above is simply a syntax error as the code is parsed, the interpreter expects
certain character/characters; in this case an operator (+,-,*,/), but doesn’t find it.
PART 2
Python Experiments:
1 of 3 – Using double quotes instead of single quotes to print string.
>>> print("Hello")
Hello
>>>
What I’ve learned here is that double quotation and single quotation marks can be used
interchangeably.
2 of 3 – Starting string with single quote but ending with double quotes
>>> print('Hello")
File "<stdin>", line 1
print('Hello")
SyntaxError: EOL while scanning string literal
>>>
What I’ve learned from this experiment is that while there are interchangeable, the double quote and
single quotes cannot be mixed as shown above.
3 or 3 - Nesting double quotes within single quotes.
Also, I wanted to see how Python would behave if I wanted to output a string that contained double
quotes as part of the string.
Incorrect
>>> print(""How are you?",said the boy.")
File "<stdin>", line 1
print(""How are you?",said the boy.")
^
SyntaxError: invalid syntax
Correct
>>> print('"How are you?",said the boy.')
"How are you?",said the boy.
>>>
What I’ve learned in this experiment, is that in order to nest quotation marks, one must utilize both
single quotation and double quotation, nested one inside of the other.
The same type of quotations cannot be nested inside of another. For Example, “”Hello”, said the boy.”.
This is incorrect and produces a syntax error.