+ 6
Is this code is correct? a=2_4 b=5_0 print (a+b)
a=2_4 b=5_0 print (a+b)
38 ответов
+ 5
Read this, it explains why it works: https://peps.python.org/pep-0515/
+ 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/
+ 3
Yes valid because python provides '_' for grouping number instead of ','
+ 3
Snehil Pandey
LIMO
Please don't talk in QA. There is messaging feature too.
+ 2
Thank you Paul ☺️❤️
+ 2
Emerson Prado Sorry I misunderstood you 😓
+ 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
+ 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).
+ 1
But this code is running 😁
+ 1
By the way this code is correct ☺️
+ 1
And pls make one thing clearer: you first asked if the code is correct, then stated it is. What's your point?
+ 1
Snehil Pandey Thanks! I just don't get why Dharmendra Pandit refuses to do such a simple job.
+ 1
Dharmendra Pandit No one knew how to share, before we learned it
+ 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.
+ 1
Emerson Prado you can copy posted code from Q&A by selecting Copy Text from the 3-dot menu.
+ 1
Snehil Pandey No problem
+ 1
The code is running
+ 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);
}
}
+ 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, '_'))
+ 1
Радимир Нечитайло
Did you check 2nd output in Code Playground?
It is (2, 4, 5, 0)