Programming Assignment
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?
You get an error in your output. A name is a string character and whenever you want to
output string characters, you put them in quotation marks so that python understands
that it needs to output text, otherwise it gives a syntax error of an incorrect output.
b) What is the difference between * and ** operators in Python? Explain with the help of an
example.
* is used to compute multiplication while ** is used to compute exponentials or power.
For example, 2*3 will give you and answer of 6, it simply means 2 multiply by 3. On the
other hand, 2**3 will give you an answer of 8, which means 2 exponential 3.
c) In Python, is it possible to display an integer like 09? Justify your answer.
NO, in python it is not possible to display an integer like 09 because it is a leading zero
integer.
d) Run the commands type('67') and type(67). What is the difference in the output and why?
The difference is that type(‘67’) gives a class of string while type(67) gives a class of
integer, reason being, whenever you write anything in python with quotation marks, it is
taken as a string character.
Part 2
a) To multiply your age by 2 and display it. For example, if your age is 16, so 16 * 2 = 32
>>>print(“Your age multiplied by 2 is:”, 27*2)
This code multiplies 27 by 2 then displays it after the text “your age multiply by 2 is:”
b) Display the name of the city, country, and continent you are living in.
>>>print(“Nairobi”, “Kenya”, “Africa”)
This code prints the three names in one line.
c) display the examination schedule (i.e., the starting and the ending day) of this term.
Explanation. Define two tuples start_date and end_date with the format date, month,
year. Print the start and end date as a string, the %i placeholder are filled with the
values of the start_date and end_date
d) Display the temperature of your country on the day the assignment is attempted by you
Explanation.
The first code assigns the active API key from OpenWeatherMap website
The second code assigns “Nairobi” to city name variable
The third code assigns “254 Kenya” to city code
The fourth code requests the active temperature data from OpenWeatherMap website
The fifth line of code helps to Parse the JSON response to get the temperature
The print statement displays the temperature (Quinn.,Lewis.,Briggs.,Werkema & Lane,
2020, p.58)
References
Horstmann, C. S., & Necaise, R. D. (2019). Python for everyone. John Wiley & Sons.
Hetland, M. L. (2005). Lists and Tuples. Beginning Python: From Novice to Professional, 31-52.
Domanski, M., Quinn, D., Day‐Lewis, F. D., Briggs, M. A., Werkema, D., & Lane Jr, J. W.
(2020). DTSGUI: A python program to process and visualize fiber‐optic distributed temperature
sensing data. Groundwater, 58(5), 799-804.
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf