LIST Comprehension | SoloLearn: Learn to code for FREE!

+3

LIST Comprehension

I have this code 👇👇👇 1.n=int(input()) 2.for i in range(n+1): 3. for j in range(11): 4. print(i,"*",j,"=",i*j) 5. print("---------------------") & I want implement the above code in this format n=int(input()) [[print(i,"*",j,"=",i*j) for i in range(11)] for j in range(n+1)] But I'm not getting the last print ("------") statement, how to do? https://code.sololearn.com/cpUTTzzvHmTc/?ref=app

10 Answers

New Answer

+4

n = int(input()) [[print(*(i, "*", j, "=", i * j) if j < 11 else "---------------------") for j in range(12)] for i in range(n+1)] Best I could do...still looking into it though.

+6

I think, most of the Parterre u already know. print("a", 0* "b") -->a print("a", 1* "b") -->ab print("a", True* "b") -->ab print("a", False* "b") -->a print("a", (3==3)* "b") -->ab print("a", (3==4)* "b") -->a print("a", (3==3)* "-------") -->a------- print("a", (3==3)* "/n-------") --> a ------- it is almost same Think as rodwynnejones did. so if i==10 the block is finished and you add the line.

+3

ok...now the "----------" outputs correctly:- n = int(input()) [[print(("{} * {} = {}".format(i, j, i * j)) if j < 11 else "--------------") for j in range(12)] for i in range(n+1)]

+2

https://code.sololearn.com/cZEQ6GGalCH7/?ref=app

+2

Oma Falk will you explain me ,it's working

+1

rodwynnejones thanks buddy I want to know about what do we call this format and some more details

+1

@𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Some sort of list comprehension? Maybe someone else can clarify.

+1

Oma Falk yeah I understood your explanation & I appreciate it But as it doesn't match to my code So will you give me, your explanation (above one)small snippet so that I can clear my doubt

0

Tomiwa Joseph how to add 5. Line in 2nd code?

0

rodwynnejones yeah I need explanation of this stuff