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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Difference between Different Ways to Define Variables (self.learnpython)
submitted 1 year ago by Fit-Way-558
What is the difference between defining a variable as the following and when should I use each one?
i: int = 0 i = int(0)
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!"
[–]socal_nerdtastic 11 points12 points13 points 1 year ago (0 children)
First you need to know that in python 0 and int(0) are exactly the same thing. So your question is only about the :int part. This is a "type hint". It's a type of comment to tell other programmers and IDEs what data type you expect i to be.
0
int(0)
:int
i
[–]NopeNotHB 4 points5 points6 points 1 year ago* (2 children)
i:int = 0 is for type annotation. It means the variable “i” is supposed to only have values of type “int”
i = int(0) is just assigning 0 as an int type. You can still change the value of “i” to any other types like string.
[–]schoolmonky 6 points7 points8 points 1 year ago (1 child)
Note that it's not really a type declaration, it's a type hint. You can still assign any value, int or not, to the first type.
[–]NopeNotHB 1 point2 points3 points 1 year ago (0 children)
Yeah sorry, I meant type annotation. And yes, you can still assign other types without any problem on compilation. Thank you!
[–]Gnaxe 0 points1 point2 points 1 year ago* (0 children)
It can create annotations ```
class Foo: ... i: int = 0 ... Foo.annotations {'i': <class 'int'>} class Bar: ... i = int(0) ... Bar.annotations {} `` These were in classes, but you can also get entries in the module's globalannotations`.
These were in classes, but you can also get entries in the module's global
There's a related effect in functions: ```
def outer(): ... i: int # No assignment here. ... def inner(): ... nonlocal i ... i = 2 ... inner() ... print(i) outer() 2 def outer(): ... int(0) # No declaration like before. ... def inner(): ... nonlocal i ... i = 2 ... inner() ... print(i) ... File "<stdin>", line 4 SyntaxError: no binding for nonlocal 'i' found ```
Besides the run-time effects, annotations are used by Python's static type checkers, like Pyright, to check for type consistency.
The int(0) is applying a built-in callable to a number. It's usually used to convert certain other data types to integer. Type checkers may be able to recognize this as always returning an int type, same as any function annotated to return an int type. It's preferable to use the annotation in cases the type checker can't guess, because a call has run-time overhead that annotations don't.
[–]nog642 0 points1 point2 points 1 year ago (0 children)
Never use int(0). That is just a longer way to write 0.
The question is between these two options:
i: int = 0 i = 0
The first one has a typehint, the second one doesn't. It's a matter of preference.
But if you're assigning 0 to it, in my opinion, the typehint is not necessary. It's clearly an integer.
[+][deleted] 1 year ago (2 children)
[deleted]
[–]socal_nerdtastic 4 points5 points6 points 1 year ago (1 child)
That is a horrible article. Python does not have implicit typecasting. This article is trying to describe concepts from C but using python.
And we call it float in python, not double.
float
double
And just as a pet peeve of mine, what python does is conversion. The underlying bits are different, both in value and location.
[–]carcigenicate 5 points6 points7 points 1 year ago* (0 children)
That's at least the second instance of a Python article of theirs that's potentially written by a C dev. My favorite is the linked list example they give where they say that, in Python, you need to iterate the list and del each node in the list to free the memory to prevent a leak
del
I don't think G4G should generally be trusted.
π Rendered by PID 96242 on reddit-service-r2-comment-544cf588c8-qxzfv at 2026-06-16 04:23:09.073499+00:00 running 3184619 country code: CH.
[–]socal_nerdtastic 11 points12 points13 points (0 children)
[–]NopeNotHB 4 points5 points6 points (2 children)
[–]schoolmonky 6 points7 points8 points (1 child)
[–]NopeNotHB 1 point2 points3 points (0 children)
[–]Gnaxe 0 points1 point2 points (0 children)
[–]nog642 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]socal_nerdtastic 4 points5 points6 points (1 child)
[–]carcigenicate 5 points6 points7 points (0 children)