Today, I was given another assignment. (Which I wasn’t expecting.) Today’s assignment was to write a python program which had to meet the conditions:

  1. Write a Python proram that multiplies all the items in a list by the value of the variable factor.

  2. The program must print the list as the output.

  3. The program should allow multiplying the variable factor by a string in case the list contains strings.

  4. You may assume that the value of factor will be a positive integer.

I’ll be doing this with positive integers and string. For positive integers, I’ll be using the numbers 1, 2, 3, 4 and multiply it by 3 to get the output:

[3, 6, 9, 12]

And then for the string, I’m going to use a, b, c, d and then mutiply it by 2 to get the output:

[aa, bb, cc, dd]

Using positive integers

First, I have to find out what function I’m going to use. In my head, I know what I’m going to use. I’m going to use:

  1. lists

  2. for - loops

  3. append

  4. print

Now I have to think of a plan which I already know what I’m going to do. I’m going to:

  1. Make a list called num_list.

  2. Then make another list called factor.

  3. Then make an empty list called output

  4. After making my lists, I’m going to use for loop

  5. And on the last step, I’m then going to use the print function to print the output.

So first, I make a list called num_lists to put the numbers 1, 2, 3, 4. So I do it like this:

num_lists = [1, 2, 3, 4]

Now to the second step. Make another list called factor so that I can use it to multiply with num_lists each number.

In factor, I’m going to put the number I want to multiply with num_lists. So I’m going to put 3:

factor = 3

After my 2nd step, I’m going to my 3rd step which is to make another list but empty. This is so because I’m going to use append to add more items later.

output = []

I’m finally done with three steps. :D Just two more steps…

Fourth step: For loops. I’m going to always start with for and then the variable as always for me, i but it can be anything else. Next I’m going to write my variable and end with colon:

for i in num_lists:

Then when I press enter, there should be the three dots. If so, then I’m on the right track so I’ll do the indented space (four space).

...    (here u write)

Now after my indented, I’m going to finally use the function append. I’m going to put the variable output and then append like:

output.append()

Because I want to add things in the output list. In the parenthesis, I’m going to do i * factor since I want each number in num_lists to multiply three times using out factor.

...    output.append(i*factor)

After that, I am finally done with the step for loops

Now to the last one which is probably the easiest. All I have to do is now to print the variable output.

print(output)

And putting that together, I make:

num_lists = [1, 2, 3, 4]
factor = 3
output = []
for i in num_lists:
...     output.append(i*factor)
...
print(output)

Which print my goal:

[3, 6, 9, 12]

Yay! :D I’ve finished half of this… ‘_’

Using String

Great, I’m running out of time right now. What do I do now? Oh wait, I just remembered, I have a shorter way to do this. Instead, I’m going to put almost everything in one variable.

So I’m going to make my new list:

str_list = ['a', 'b', 'c']

Then I’m going to make another list called factor and then add instead of 3, I’m going to add 2.

factor = 2

After that, I’ll make one more list called output but instead of making it empty, I’m going to do s * factor so that it’ll iterate and times it with 2 in the str_list. And then do the mini for loops by just doing the first part.

output = [s*factor for s in str_lists]

And now the print:

print(output)

And so when putting everything together, I get:

 str_list = ['a', 'b', 'c']
 factor = 2
 output = [s*factor for s in str_lists]
 print(output)

Which gives the output I want:

['aa', 'bb', 'cc']

Finishing up

I’ve finally finished with everything. I hope you liked it. Thanks for reading! :D