0% found this document useful (0 votes)
31 views5 pages

CS1101Week1 Assignment

Uploaded by

adeelmshll
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)
31 views5 pages

CS1101Week1 Assignment

Uploaded by

adeelmshll
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/ 5

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?

Explanation:

in Python there are syntax rule that we must follow. A very clear example is when we

want call the function print() and want to print a text, we need to use quotation marks. It

is a rule in Python. If we leave out one of both of the quotation while using print function,

then Python shows Syntax error, which means that we made a mistake while writing the

code and Python could not read the statement.

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

example.

Explanation:

In Python, * is used to multiply two numbers however, ** is used for exponentiation

(Downey, 2015). As shown above, 5 * 5 = 25. On the other hand, 5 ** 5 means 55 = 3125.
b. In Python, is it possible to display an integer like 09? Justify your answer

Explanation:

Yes, we can print 09 in Python however, it is important to know that we cannot print 09

using simply print(09) function. Because when we type 09, the Python interpreter reads

as an Octal number (Base-8), not a decimal number. So, if we enter an integer starting

with 0 that exceeds 7, Python will print a syntax error. With a little customization, we can

print 09. In the above code, we have assigned 9 to number and “{:02d}” is a formation

specifier that means that number which is 9 has a minimum width of 2, and it starts with

0, d means that number is an integer. Using a format specifier, we can print 09 as shown

above.

c. Run the commands type('67') and type(67). What is the difference in the output and why?

Explanation:

In Python, the type function shows the type of the data (W3Schools, n.d.). The type

function with ‘’ means that whatever comes between the quotation marks is string (a
series of characters). So, when we use type function with quotation marks, it will return

string as a result. On the other hand, when we use type(67), it will only return as an

integer, the interpreter will read 67 as integers literals.

Part 2:

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

I created a variable called my_age that has integer as the data type and the value is 27.

Then, it is multiplied by 2 = 54. Using this example, I learned that we can directly and

declaring variables multiply numbers and also perform arithmetic operations on them.

This is not possible in Java.

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

First, I declared three variables which holds the name of the city, country and continent

that I am living. Then, I used the print function to print them all in one sentence. Doing

this exercise, I learned that I can print multiple variables in one print function. Moreover,
I learned that I can combine and print text along with the variables. For example, the text

“I am currently living in City:” with city, country and continent, however using coma

between them is important.

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

First, to store the date value in variable we need to import date and time module. Then I

have declared the start_date and end_date to store the dates. Then I have printed them

using print function. Through this example, I learned using and storing date values. Date

values cannot be stored as numbers. We can store them as a string however, the

interpreter will always read it a string not a date. Therefore, we use built-in module of

date and time.

d) Display the temperature of your country on the day the assignment is attempted by you
First, I stored the values of the highest temperature and lowest temperature in the

variables and printed them. Through this exercise, I learned that I can store negative

numbers as well as integers.

References:

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

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

W3Schools. (n.d.). Python type() Function. Retrieved from

https://www.w3schools.com/python/ref_func_type.asp

You might also like