0% found this document useful (0 votes)
4 views8 pages

Programming Assign...

The document discusses common syntax errors in Python related to string literals and the use of operators. It explains the difference between the '*' and '**' operators, the impossibility of displaying integers with leading zeros, and the distinction between string and integer types. Additionally, it includes programming exercises demonstrating basic operations, formatted output, and suggestions for improving code robustness.

Uploaded by

stephensoncory56
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)
4 views8 pages

Programming Assign...

The document discusses common syntax errors in Python related to string literals and the use of operators. It explains the difference between the '*' and '**' operators, the impossibility of displaying integers with leading zeros, and the distinction between string and integer types. Additionally, it includes programming exercises demonstrating basic operations, formatted output, and suggestions for improving code robustness.

Uploaded by

stephensoncory56
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/ 8

Programming Assignment Unit 1

PART 1

A. If you are trying to print your name, what happens if you leave out one of the quotation

marks or both, and why?

1. >>> print ("cory)

Output:

SyntaxError: unterminated string literal (detected at line 1)

The code above produced a syntax error that indicates unterminated string literal. As the rule of

python language states, all strings must be enclosed in quotation (“ ” or ‘’)

Eg

Print (“cory”)

Screenshot:

2. >>> print (cory)

Output:

print (cory)

NameError: name 'cory' is not defined


The above error occurred because without the quotations, python (py) does not recognize “cory”

as a string . In some situations, py see it to be a variable therefore treats it as such however, the

error “NameError: name 'cory' is not defined” returns because cory has not been defined

therefore it is not recognized by py.

Screenshot:

3. >>> print (cory")

Output:

SyntaxError: unterminated string literal (detected at line 1)

This ha been explained in number 1 above.

B. What is the difference between * and ** operators in Python? Explain with the help of

an example.

The “*” known as the multiplication is an operator that is used to perform multiplication

arithmetic in py where as “**” known as exponentiation is used for exponentiation of numbers

Mathematical representation Python representation

34 >>>3**4
= 3x3x3x3 Output

= 81 81

C. In Python, is it possible to display an integer like 09? Justify your answer.

It is not possible this is because py does not retain leading zero thus it is not meaningful for

numeric calculations therefore, py will return a syntax error if this happens.

Screenshot:

Code

>>> print 09

D. Run the commands type('67') and type(67). What is the difference in the output and

why?

>>> type (‘67’)

Output:
<class 'str'>

The above code is used to show the type of value the interpreter displays. In this case, the class

off value been output is a string. This is due to the double quote therefore (‘67’) is a string. On

the other hand,

>>>type (67)

Output:

<class 'int'>

the interpreter recognizes this class of value as an integer therefore (67) is an integer

Screenshot:

Part 2

Write a Python program for each of the following questions a to d.


a. To multiply your age by 2 and display it. For example, if your age is 16, so 16 * 2 = 32

Code:

#This program will multipy me age by 2

>>>26*2

Output

52

Screenshot:

b. Display the name of the city, country, and continent you are living in.

Code:

#This program will display the name of the city, country, and continent I am living in.

print ('City: Accra','' ,'','Country: Ghana','','','Continent: Africa')

Output

City: Accra Country: Ghana Continent: Africa

Screenshot
c. To display the examination schedule (i.e., the starting and the ending day) of

this term.

Code:

>>># This program display the examination schedule (i.e., the starting and the ending

day) of this term.

>>>print ('Examination schedule','','starting : Jan 9 2025','','ending : Jan 12 2025')

Output:

Examination schedule starting : Jan 9 2025 ending : Jan 12 2025

Screenshot:

d. Display the temperature of your country on the day the assignment is attempted by you

Code:

>>>print ('County: Ghana','','Temp: 28 degree celsius')

Output:

County: Ghana Temp: 28 degree Celsius

Screenshot:
These exercises showcase Python's ease of use for performing mathematical operations,

string manipulations, and formatted output. While the solutions effectively fulfill the

tasks, adopting more modern formatting techniques. Additionally, learning to incorporate

error handling or interactive input would make the programs more dynamic and robust.

References:
 Python Software Foundation. (n.d.). NameError. In the Python Standard Library: Built-in

exceptions. Retrieved November 18, 2024, from

https://docs.python.org/3/library/exceptions.html#NameError

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

Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf

You might also like