0% found this document useful (0 votes)
46 views4 pages

Learning Journal

Printing a string without quotation marks causes a syntax error, as the interpreter cannot determine the start and end of the string. Printing without quotation marks around a number works as expected. Operators like + and - work as expected when placed between numbers. Leading zeros are not allowed for integers in Python. Two values with no operator between them causes a syntax error. True is a built-in boolean type, while true is undefined. Formatting can add leading zeros but only works for numbers 1-9. Experiments showed proper use of print requires parentheses and capitalization matters for boolean values.

Uploaded by

Safa Hamaizia
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)
46 views4 pages

Learning Journal

Printing a string without quotation marks causes a syntax error, as the interpreter cannot determine the start and end of the string. Printing without quotation marks around a number works as expected. Operators like + and - work as expected when placed between numbers. Leading zeros are not allowed for integers in Python. Two values with no operator between them causes a syntax error. True is a built-in boolean type, while true is undefined. Formatting can add leading zeros but only works for numbers 1-9. Experiments showed proper use of print requires parentheses and capitalization matters for boolean values.

Uploaded by

Safa Hamaizia
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/ 4

Part 1:

 If you are trying to print a string, what happens if you leave out one of the quotation marks
or both and why?

Printing a string without


one quotation marks will
encounter a syntax error.
While printing
without both quotation
marks will print the value
as it is like for example:
Printing a string without one quotation mark will get Python displays an error message (syntaxt
error), for example :

>>> print ('Hello World)

SyntaxError: EOL while scanning string literal

That’s because it is not possible to determine the beginning or end of the string to be displayed
without both of the quatation marks (Downey, A., 2015).

Printing without both quotation marks will encounter a syntax error as well because the interpreter
can't parse the code, for example :

>>> print (Hello World)

SyntaxError: invalid syntax

However omitting both of the quotation marks when printing numeric values, makes the output
comes out correct ; which is the same numeric value written in the statement, for example :
>>> print (2021)

2021

 You can use a minus sign to make a negative number like -2. What happens for each of the
following and why?

>>> 2++2
>>> 2--2
>>> 2+-2
>>> 2-+2

>>> 2++2

>>> 2--2

>>> 2+-2

>>> 2-+2

The tokens and operators here are placed correctly and the calculations are made following
arithmetic rules

2 + (+2) = 4

2 - (-2) = 4

2 + (-2) = 0

2 - (+2) = 0

 In math notation, leading zeros are OK, as in 02. What happens if you try this in Python and
why?

>>> 02

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

It returned an error message indicating a syntax error because leading zeros are not allowed in
Python because of ambiguity

However it can pass as a string when leading zero is put in front of an integer

>>> print ('02')


02

 What happens if you have two values with no operator and a space in between them and
why?

Example :

>>> 1 2

SyntaxError: invalid syntax

The interpreter displayed an error message indicating a syntax error because Python didn’t recognize
the space between the two values as a valid statement.

Part 2:

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. 

1.

>>> 05

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

>>> print ('05')

05

>>> print("{:02d}".format(5))

05

I learned from this experiment that leading zeros are not allowed but In order to use a leading zero
when it’s in front of an integer, we can pass it as a string or use print("{:02d}".format(1)) for example
(Stack Overflow, 2008).

I tried that format but apparently it works only with numbers from 1 to 9.

2.

>>> print 1

SyntaxError: Missing parentheses in call to 'print'. Did you mean print(1)?

>>> print '1'

SyntaxError: Missing parentheses in call to 'print'. Did you mean print('1')?


I learned from this that I need a pair of parentheses to print in Python 3 or else the output will be
just an error message.

3.

>>> type (True)

<class 'bool'>

>>> type (true)

Traceback (most recent call last):

File "<pyshell#1>", line 1, in <module>

type (true)

NameError: name 'true' is not defined

Here, I was learning about values and types from Chapter 1 and I typed the same word, which “true”,
once starting with an uppercase letter and once with a lowercase letter. Apparently “True” is a
boolean data type which means that it is one of the built-in data types provided by Python
(GeeksforGeeks, 2020) while “true” with lowercase letter weren’t defined.

References:

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press.

GeeksforGeeks. (2020, December 23). Boolean data type in Python.


https://www.geeksforgeeks.org/boolean-data-type-in-python/

Stack Overflow. (2008, September 25). Display number with leading zeros.
https://stackoverflow.com/questions/134934/display-number-with-leading-zeros

You might also like