Learning Journal 1
Learning Journal 1
Part 1
Exercise 1.1 from your textbook recommends that you try to make mistakes when experimenting with a new
programming feature.
This kind of experiment helps you remember what you read; it also helps when you are
programming because you get to know what the error messages mean. It is better to make
mistakes now and on purpose than later and accidentally. (Downey, 2015, 7)
For this Learning Journal, first, answer the following questions based on Exercise 1.1 Include example Python
code and output with your answers.
Question 1:
If you are trying to print a string, what happens if you leave out one of the quotation marks or both and why?
Answer:
In Python 3, we need both quotation marks to get a value. If one of the quotation marks is left out, it gives us a
“syntax error”. EOL error means, “end of line”, it does not have a closing or a beginning tag. Thus, we need to
add it to get a correct answer.
>>> print('hello')
hello
>>> print('hello)
SyntaxError: EOL while scanning string literal
>>> print(hello')
SyntaxError: EOL while scanning string literal
If you leave both quotation marks out, it will also give you a “syntax error”, because Python 3 requires us to
place quotation marks for storing the value in any variable or defining it.
>>> print(hello)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
print(hello)
NameError: name 'hello' is not defined
Question 2:
You can use a minus sign to make a negative number like -2. What happens for each of the following and why?
Answer:
>>> 2 ++ 2
4
It answers 4, because it does the calculations like a calculator.
>>> 2 -- 2
4
It answers 4, because it does the calculations like a calculator.
>>> 2 +- 2
0
It answers 0, because it does the calculations like a calculator.
>>> 2 -+ 2
0
It answers 0, because it does the calculations like a calculator.
Question 3:
In math notation, leading zeros are OK, as in 02. What happens if you try this in Python
and why?
Answer:
>>> print(02)
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
It gives us an “syntax error” because leading a 0 in decimal integers are not permitted in Python 3 unlike in
math notations
Question 4:
What happens if you have two values with no operator and a space in between them and why?
Answer:
>>> 53
53
When two values are placed together without a space and/or operator between them, then the output would
render the same value as the statement. Basically, it interprets it as one single number. However, if there is no
operator but does have a space between them, it is different.
>>> 5 3
SyntaxError: invalid syntax
It gives an “syntax error” because it does not mean anything without an operator and a space.
Within the number, it is invalid.
Part 2
Question 1:
Next, describe at least three additional Python experiments that you tried while learning Chapter
1. Show the Python inputs and their outputs, and explain what you learned from the results of each example.
Experiment 1: Finding letters in the string in Python 3.
>>> x = 'Mistake'
>>> print(x)
Mistake
>>> 'k' in x
True
>>> 'q' in x
False
Experiment 2: Using Python 3 interpreter as a calculator and doing simple arithmetic calculations.
>>> 6 + 8 * 3 / 2
18.0
The mathematical order for this equation is Division, multiplication and them addition
Experiment 3: A number can be an integer but can also be a string when you give it quotation marks around
the number.
>>> type(2)
<class 'int'>
>>> type('2')
<class 'str'>
Word Count:589
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. This
book is licensed under Creative Commons Attribution-NonCommercial 3.0 Unported
(CC BY-NC 3.0)