+ 1

Is there a way to have this code read like a single line sentence?

Python adding numbers from input. https://code.sololearn.com/c6w5wad53vl5/?ref=app

13th Jan 2019, 6:55 AM
Wi$e Xhi8F
Wi$e Xhi8F - avatar
4 ответов
+ 4
When you print different variables separated by commas the output separates each value with a blank space by default. If you want to set the separator you have to put sep=new_separator in print. Example: print(1,2,3) # blank space by default 1 2 3 print(1,2,3,sep="_") 1_2_3 print(1,2,3,sep="") # no separator 123 map applies an operation to each element of array. for example, here map convert a character of a number to a float number: array1=["1","2","3"] array2=map(float,array1) for element in array2: print(element) 1.0 2.0 3.0
13th Jan 2019, 9:38 AM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 5
Like this? x, y = map(float, input().split()) (sample input: '25 38' instead of '25 38') You can write the output in one line like this: print("You entered ", x, " & ", y, "\nThem added is = ", x + y, sep='') Or the whole code like this: print('You entered %.2f & %.2f\nThem added is %.2f' % (lambda a, b: (a, b, a+b))(*tuple(map(float, input().split()))))
13th Jan 2019, 7:41 AM
Anna
Anna - avatar
+ 5
"sep" means separator. If you print multiple strings like this: print('a', '=', str(42)) , python will automatically use a space as separator and the result will look like this: a = 42 This might look ok in most cases. In your case, I set sep to '' (an empty string) to avoid that the second line begins with a space (which is more of an aesthetical issue than a real problem). You can use triple quotes: print('''first line second line''') , but you'll have to change how you use your variables in this case. You could do something like: print('''a = {} b = {}'''.format(a, b)) or: print(f'''a = {a} b = {b}''') map is kind of complicated. It means that you use a certain function for all items of an iterable (for example a list). If you have a list lst = ['1', '2', '3'] (note that '1', '2' and '3' are strings) and use map(float, lst), float() will be applied to each item in lst and they will all be converted to floats. If you wrap the whole thing in a list(), the result will be a list of floats => list(map(float, lst)) = [1.0, 2.0, 3.0].
13th Jan 2019, 9:27 AM
Anna
Anna - avatar
+ 2
Thanks Anna could help explain further I'm progressing my way through the python tutorial I am still on the first module! I understand the first output sequence somewhat. A couple of questions! 1. At the end of the first output what does the (sep=") do? 2. Also would I be able to use triple quotes to avoid explicitly having to devote new line by \n? 3. What is map? Thanks
13th Jan 2019, 9:12 AM
Wi$e Xhi8F
Wi$e Xhi8F - avatar