Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain in details of the following question and Do not use IDLE 1. # what does

ID: 3875452 • Letter: E

Question

Explain in details of the following question and Do not use IDLE

1.

# what does this function mean ?

#what is "%s %d %f" % ? what does it do ? how it affects the (5,5,5)?

print ("%s %d %f" % (5, 5, 5))

2

n = 10000
count = 0 # what does count = 0 means ?
while n: # what does while n: means ?
count = count + 1 # what does it mean by count =count+1 ? how does it affect the result
n = n // 10 # what n// 10 means ? what // means ?

print (count)

3.

n = 10
while n != 1:
print (n,)
if n % 2 == 0: # n is even # what does % means ? what n % 2 == 0 means?
n = n // 2 # what n // 2 means ?
else: # n is odd
n = n * 3 + 1

4.

mylist = ["now", "four", "is", "score", "the", "and seven", "time", "years", "for"] # why we have to use [] instead of ()
a=0
while a < 7:
print (mylist[a],) # what does the "," in here means? what does it do to the function
a += 2 # what does += means, what does a+=2 do to the function ?

Explanation / Answer

1)

%s refers to string data type. %d to the integer data type and %f to the float data type.

So in the given example of print("%s %d %f" % (5, 5, 5))

%s acts as a placeholder for the string, %d acts as a placeholder for an integer and %f for the float.

So first 5 will be printed as a string, second 5 as an integer and third 5 as a float.


2)

n = 10000
count = 0

Here, count is a variable that is initialized to the value zero.

while n:
count = count + 1
n = n//10


Here, while is a loop that will continue to execute the code inside it as long as the condition provided before the colon is true.

So the condition will be true for all the numbers except zero as zero represents false. So it starts from the value of 10000 for n and divides n by 10 every time. The loop will stop executing once n becomes zero.

The count variable is incremented by 1 every time to count the number of times the while loop executes.

// (double slash) in Python 3 is used for doing integer division. It is also known as floor division. It results in only quotient and not remainder.

The result of division is rounded off to the nearest floor value.

Example:

10/3 results in 3.333333

and

10//3 results in 3 as it rounded off 3.33333 to 3.


3)

% is known as Modulus operator in python. It gives the remainder of the division of the left operand by the right.

For example:

10 % 3 results in 1. As after dividing 10 by 3, we get the remainder of 1.


So n%2 == 0 means that we are dividing the number n by 2 and checking the remainder. The remainder of even numbers when divided by 2 is always 0. So if it's an even number i.e. n % 2 == 0 results is true, then we perform n//2 i.e. divide the n by 2 and round it off to the floor.


4)

We have to use [] for declaring lists in Python. () is reserved for declaring tuples. A tuple is a sequence of immutable objects. Tuples cannot be changed once declared unlike lists, which can be changed.

So therefore, a = [1,2,3] declares a list and a = (1,2,3) declares a tuple.

print (mylist[a],)
In Python2, comma (,) is used for printing the output on the same line. Every element of mylist will be printed on the same line. This will work only in Python 2.


a += 2

a += 2 is same as writing a = a + 2. Take the value in the variable a, and 2 to it and save that result to a. i.e. incrementing the value of a by 2.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote