Python - Week 4 - Multiplication Variants and Arrays

As I traveled to Canada, I read about arrays in Python. I also learned about how * operator is applied to repeat / concatenate.

Here is the problem statement:

Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.

Suppose the following inputs are given to the program:
3,5
Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

Here is the code, thanks to gist on github.



Some notes:

  1. In Python * means multiply for numbers, but repeat for strings—it’s like concatenating a string to itself repeatedly.
  2. The range built-in function generates a list of successive integers, starting from 0 be default and ending at upper bound - 1.

Comments

Popular posts from this blog

Python - Week 8 - Dictionaries, Tuples, Sorting and Filtering

Python - Week 3 - Lists, Tuples, Slicing, Math Functions

Python - Week 5 - Comprehensions