Tuple related (python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Tuple related (python)

def cmyk_to_rgb (): x = [] for i in range(4): x.append(float(input())) c,m,y,k= x r = round(255 * (1 - c) * (1 - k)) g = round(255 * (1 - m) * (1 - k)) b = round(255 * (1 - y ) * (1 - k)) ''' result = r,g,b return result #the above lines returns a tuple. #example outputs (1,2,3) instead of 1,2,3''' return str(r)+","+str(g)+","+ str(b) print (cmyk_to_rgb()) Refer to the comment part. Since, it was returning a tuple with parentheses, I had to convert all manually to string and then add two commas in between. Is there a way I could have returned the result in the desired format i.e. 1,2,3 without doing this ??

20th Jan 2021, 4:06 AM
CHANDAN ROY
CHANDAN ROY - avatar
3 Answers
+ 4
CHANDAN ROY That print I did is just another method or alternative, it has no connection with your function. And with your function, you can use f-strings or concatenation using + (like what you did) instead of comma because comma will generate a tuple. (1,2,3) ---> tuple 1,2,3 ---> tuple https://code.sololearn.com/c5aPiYyvv9To/?ref=app
20th Jan 2021, 4:48 AM
noteve
noteve - avatar
+ 4
",".join([str(x) for x in input.split()]) >> 1,2,3,4 #.join method convert lists into string with "," between elements, you can join characters whatever you want. - - - - - - - - - - - - OR print(r, g, b, sep=",") >> 1,2,3 # sep parameter in print, represents the comma that separates each variables. By dafult sep is whitespace but you can costumize or use whatever character you want. - - - - - - - - - - - - And the reason why it outputs tuple is because when Unpacking values and variables, the unpacked or zipped value will return Tuple.
20th Jan 2021, 4:12 AM
noteve
noteve - avatar
+ 1
《 Nicko12 》 print statement also generates None as function doesn't return anything
20th Jan 2021, 4:30 AM
CHANDAN ROY
CHANDAN ROY - avatar