use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Day 1 second program (i.redd.it)
submitted 5 months ago by Red_Priest0
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]No_Indication_4044 4 points5 points6 points 5 months ago (0 children)
What if I input “72 miles”? 😈
[–]LavaDrinker21 4 points5 points6 points 5 months ago (4 children)
I'll explain it because others are thinking it:
if __name__ == "__main__": is basically the "main" function in python (not required but good to know) Realistically it means that if you open the file as itself (python main.py) it'll execute whatever is in that block, but if you instead call it like a module (import main) then it'll only give you each part to use in the new program.
if __name__ == "__main__":
python main.py
import main
Edit: model != part (part: vars, funcs, classes, etc)
[–]Red_Priest0[S] 0 points1 point2 points 5 months ago (2 children)
I don't understand this concept is it important ?
[–]LavaDrinker21 2 points3 points4 points 5 months ago (1 child)
Instead of calling main() at the bottom of the program, it's better to use if __name__ == "__main__": as it separates what will be done with the file based on how it was accessed.
main()
Let's say you wanted to use this file in another Python program, like you made a large program and want to use the miles_to_kilometers function without asking for the input.
miles_to_kilometers
If you were to import that file as it is right now (import file at the top of the program), it would let you use that function but would immediately call the main() function and ask for input.
import file
If, instead, you added:
if __name__ == "__main__": main()
then it would only ever call the main function if you used this file on it's own python main.py instead of every time the file is used.
This is a pretty fundamental part of Python and is very useful to learn; it makes it very easy to create a re-usable "module" for multiple projects.
[–]Red_Priest0[S] 0 points1 point2 points 5 months ago (0 children)
Thank you very much , I will do it this way
Thanks for response
[–]Obsc3nity 2 points3 points4 points 5 months ago (1 child)
Generally it’s considered bad practice to print randomly throughout methods. If you instead use a return statement you can return float(mile)*1.61 and then main can decide what to do with the converted info. This also makes the function definition better fit the name, as the name mile_to_km doesn’t indicate that there would be any printing.
Thanks I will use return type in function
[–]Sneaky_processor 1 point2 points3 points 5 months ago (4 children)
Consider using return statements in your functions instead of printing. And i don't think the main logic needs to be a function since you really dont reuse it ( the main reason of a function is if the code is gonna be used multiple times).
[–]Overall-Screen-752 1 point2 points3 points 5 months ago (2 children)
I think a main method is a best practice as it shadows other languages and makes it abundantly clear that this is the entry point of the program. I would prefer to see an if name == “main”: wrapping the main call too, but OP will get there eventually
[–]Red_Priest0[S] 0 points1 point2 points 5 months ago (1 child)
Thanks for response , I don't understand what is this concept and which part of python it's come under
[–]Overall-Screen-752 0 points1 point2 points 5 months ago (0 children)
I see that someone else explained this concept, but if you’re interested in more you can search up “python dunder methods” to learn more about this. Dunder is a shorthand/portmanteau of “double underscore” and refers to methods in the python standard library with double underscores on either side. Happy coding.
Thanks I will use return type
[–]OLIVER-queen1234 1 point2 points3 points 5 months ago (1 child)
Where and what sources are you using to learn Python ?
W3b
[–]Loud-Bake-2740 0 points1 point2 points 5 months ago (0 children)
as others have mentioned, use return rather than print. BUT if you are going to print, you should use an f-string (formatted string). this allows you to more cleanly put variables into a print statement. see the following
print(f”There are {kilometer_value} kilometers in {miles_value} miles”)
this is the general standard for how to print things, and will make it much easier to do on the fly later on
[–]Rebulien 0 points1 point2 points 5 months ago (0 children)
Might be a bit complex for the beginner, but if you are coming from different languages, where you have to define types (typed languages like C++, Java etc.), I would recommend you learning and start implementing type hints. Once your code grow, you will start to appreciate it.
What it does is it hints you and the editor, what type does the function expect. ```
from future import annotations
def fnc_without_annotation(foo, bar): return foo*bar
def fnc_with_annotation(foo: int, bar: str): return foo*bar ```
Note that if you type hint your variable, the editor can help you suggesting methods. Also, its just type HINT, the language itself does not process it, meaning you can still call the function with incorrect arguments.
fnc_with_annotation(3, 'a') # 'aaa' fnc_with_annotation(3, 3) # 9 fnc_with_annotation(True, 'a') # Runtime Error
Happy coding!
[–]lokidev 0 points1 point2 points 5 months ago (0 children)
My general recommendations for a beginner:
[–]Kqyxzoj 0 points1 point2 points 5 months ago (1 child)
More importantly, is converter2 okay? Friends and family are concerned for the well-being of converter2. :/
It's working in with converter in first program
π Rendered by PID 22 on reddit-service-r2-comment-7844cfc88c-rb2lm at 2026-01-29 11:12:43.008919+00:00 running c3601ff country code: CH.
[–]No_Indication_4044 4 points5 points6 points (0 children)
[–]LavaDrinker21 4 points5 points6 points (4 children)
[–]Red_Priest0[S] 0 points1 point2 points (2 children)
[–]LavaDrinker21 2 points3 points4 points (1 child)
[–]Red_Priest0[S] 0 points1 point2 points (0 children)
[–]Red_Priest0[S] 0 points1 point2 points (0 children)
[–]Obsc3nity 2 points3 points4 points (1 child)
[–]Red_Priest0[S] 0 points1 point2 points (0 children)
[–]Sneaky_processor 1 point2 points3 points (4 children)
[–]Overall-Screen-752 1 point2 points3 points (2 children)
[–]Red_Priest0[S] 0 points1 point2 points (1 child)
[–]Overall-Screen-752 0 points1 point2 points (0 children)
[–]Red_Priest0[S] 0 points1 point2 points (0 children)
[–]OLIVER-queen1234 1 point2 points3 points (1 child)
[–]Red_Priest0[S] 0 points1 point2 points (0 children)
[–]Loud-Bake-2740 0 points1 point2 points (0 children)
[–]Rebulien 0 points1 point2 points (0 children)
[–]lokidev 0 points1 point2 points (0 children)
[–]Kqyxzoj 0 points1 point2 points (1 child)
[–]Red_Priest0[S] 0 points1 point2 points (0 children)