I've created a very small Python code to output the sum of two input numbers.. can it be made smaller?? 🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I've created a very small Python code to output the sum of two input numbers.. can it be made smaller?? 🤔

Hi folks.. 😊! This one is my first shared code in the app! I'm a pretty new user with the app and just learning many things here.. 😄! These bit-sized lessons are really helping me to learn things in a nice and much easier way with much comfort! I'm focusing mainly on Python, JS, Web stuffs (HTML, CSS).. But want to learn many other things too.. 😏! Here, this is a very little piece of code which I wrote while solving a coding qs in a 'Python Core' lesson which introduced to me the 'Walrus' operator which is :=. I was familiar with few langs but never knew abt this kinda weird looking operator..😅! Here I just tried to make a code to print the sum of two input numbers just within a line.. I think this is the shortest program I've ever written to output the sum of 2 inputs in any lang.. 😎! Share any thought abt it..! And is it possible to make this code any shorter doing the same task..? 🤔 FYI, the code is here: print(x:=int(input())+int(input())) https://code.sololearn.com/c5tHPnF2Xrf1/?ref=app

21st Nov 2022, 7:15 PM
Himubab Cryptic
Himubab Cryptic - avatar
5 Answers
+ 7
In your example code, you are not using the variable x, so you could shotren the code by not using any variable.
21st Nov 2022, 7:21 PM
Lisa
Lisa - avatar
+ 2
I can't think of anything besides what Lisa already said, but I love the question!
21st Nov 2022, 8:03 PM
Caroline Russell
Caroline Russell - avatar
+ 1
Thnx a lot Lisa.. 🙏! Actually I thought that the input functions would only work while assigned to a variable.. Thatz why I used the Walrus there!! But checked now and I found that the task can be done without using the var..! 😊 But if a program needs to keep sum of 2 nos in a var and then has to use that sum later, the Walrus one might come a bit handy.. ☺️
21st Nov 2022, 9:10 PM
Himubab Cryptic
Himubab Cryptic - avatar
+ 1
This is short indeed.. but my aim was to get inputs from the user and then output the sum finally
27th Nov 2022, 2:55 PM
Himubab Cryptic
Himubab Cryptic - avatar
0
Yes , absolutely. It can be made even more smaller. For example, print(8+10) Output:- 18
27th Nov 2022, 11:52 AM
Anonymous
Anonymous - avatar