+ 6

Is this code is correct? a=2_4 b=5_0 print (a+b)

a=2_4 b=5_0 print (a+b)

8th Jul 2022, 5:21 PM
Dharmendra Pandit
Dharmendra Pandit - avatar
38 Answers
+ 5
Read this, it explains why it works: https://peps.python.org/pep-0515/
8th Jul 2022, 5:45 PM
Paul
Paul - avatar
+ 4
python 3.6 and above allows to use underscores '_' as a visual separator for grouping digits in numeric literals https://peps.python.org/pep-0515/
10th Jul 2022, 5:52 AM
Lothar
Lothar - avatar
+ 3
Yes valid because python provides '_' for grouping number instead of ','
10th Jul 2022, 6:31 AM
filibuster
filibuster - avatar
+ 3
Snehil Pandey LIMO Please don't talk in QA. There is messaging feature too.
10th Jul 2022, 3:04 PM
A͢J
A͢J - avatar
+ 2
Thank you Paul ☺️❤️
8th Jul 2022, 5:47 PM
Dharmendra Pandit
Dharmendra Pandit - avatar
+ 2
Emerson Prado Sorry I misunderstood you 😓
8th Jul 2022, 5:51 PM
Random
Random - avatar
+ 2
Does the code work. Yes Is the code correct? Yes Why does it work? There are many uses for Underscores in Python. However in this case. The underscore is used to separate digits but there is no visual result. a=2_4 b=5_0 print (a+b) Is the same as a=24 b=50 print (a+b) The uses of underscore. With clear examples https://www.javatpoint.com/underscore-in-python
8th Jul 2022, 8:07 PM
Chris Coder
Chris Coder - avatar
+ 2
Samuel Abayomi Pls do not spam or try to hijack other people's questions. If you want to learn, just do the courses. SoloLearn has plenty for Web dev. Make sure to ask questions when in difficulties. Just check others here for instructions on how to write a good question (= one which will attract good answers).
10th Jul 2022, 1:43 AM
Emerson Prado
Emerson Prado - avatar
+ 1
But this code is running 😁
8th Jul 2022, 5:25 PM
Dharmendra Pandit
Dharmendra Pandit - avatar
+ 1
By the way this code is correct ☺️
8th Jul 2022, 5:27 PM
Dharmendra Pandit
Dharmendra Pandit - avatar
+ 1
And pls make one thing clearer: you first asked if the code is correct, then stated it is. What's your point?
8th Jul 2022, 5:31 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Snehil Pandey Thanks! I just don't get why Dharmendra Pandit refuses to do such a simple job.
8th Jul 2022, 5:36 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Dharmendra Pandit No one knew how to share, before we learned it
8th Jul 2022, 5:41 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Snehil Pandey I mean posting the link to the code in the question. This is how the code gets visible, without one having to search one's profile, like you did.
8th Jul 2022, 5:49 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado you can copy posted code from Q&A by selecting Copy Text from the 3-dot menu.
8th Jul 2022, 6:51 PM
Brian
Brian - avatar
+ 1
Snehil Pandey No problem
8th Jul 2022, 9:20 PM
Emerson Prado
Emerson Prado - avatar
+ 1
The code is running
9th Jul 2022, 9:04 AM
Peter Gitau
+ 1
Emerson Prado Dharmendra Pandit Yes that is correct we can put underscore (_) for a numeric value. If you want to write a long numeric value then best practice is use underscore between each digits and I think every language support this feature In Java long num = 1_2_3_4_5_6; This is valid and will be consider as 123456 This is Java code: public class Program { public static void main(String[] args) { int num = 1_2_3_4_5_6; System.out.println (num); } }
9th Jul 2022, 9:13 AM
A͢J
A͢J - avatar
+ 1
This syntax for writing numbers was created to separate thousandths ☺️ print(3.14_159_265_359) You can use the format() function to print integers with spaces: print(format(314159265359, '_'))
10th Jul 2022, 1:16 AM
Solo
Solo - avatar
+ 1
Радимир Нечитайло Did you check 2nd output in Code Playground? It is (2, 4, 5, 0)
10th Jul 2022, 8:29 AM
A͢J
A͢J - avatar