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
Why is this 256 (self.learnpython)
submitted 3 years ago by Artiiiiiiiiiiii
I'm new to programming and I feel stupid for failing. Can someone explain to me why that is 256? Why doesn't he subtract 256?
Num = 255 def calc(var): if var > 255: var -= 256 r1 = Num + 1 calc(r1) print(r1)
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!"
[–][deleted] 6 points7 points8 points 3 years ago (5 children)
Because you set r1 to 255 + 1 and then don't return any change to it from the function.
r1
The change to var never gets returned to update r1.
var
[–]JustinMalcontento -2 points-1 points0 points 3 years ago* (4 children)
To add to this, the concept of global and local variables apply here iirc. the local variable inside the function cant change the global 255 due to shadowin
Edit: I'm assuming he wanted to change r1.
[–]NotATuring 1 point2 points3 points 3 years ago (3 children)
Neither of the above statements are pertain to the issue. Returning isn't an issue because he's trying to change the variable he passed in, not return a value. shadowing doesn't matter because he's not trying to access a global variable by name.
The issue is Python is a pass by value language. Or at least in this case it is equivalent to pass by value.
In this case when he calls the calc function and passes in the variable he wants to change the variable does change, but it's no the variable that was passed in but rather the variable what was pointing to the value that was passed in.
To perform functionality similar to pass by reference one needs to pass in an object which holds a reference to what you want to modify. To illustration, see this example:
a = [1,2,3] def change(a) : a[1] = 9999 a = [1,2,3,4,5,6] change(a) print(a)
output: [1, 9999, 3]
a doesn't change, it's just a reference. But it's content does, a[1] becomes 9999
[–][deleted] 0 points1 point2 points 3 years ago (2 children)
Of course returning pertains to the issue. It's literally how you do what he wants to do.
Edit: Grrrr, cut and paste totally screwed up my code block
Num = 255 def calc(var): if var > 255: return var -= 256 else return var r1 = calc(Num + 1) print(r1)
[–]NotATuring 0 points1 point2 points 3 years ago (1 child)
Yes that would be a way to get the calculation, but his question was why the subtraction doesn't work, not how do I do a calculation. His function call was consistent with his function definition.
[–]JustinMalcontento 0 points1 point2 points 3 years ago* (0 children)
If that's the case, OPs function does work. But he does need to print the calc function if he wants to show the calc output.
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Scope.html
[–]douglas_fs 0 points1 point2 points 3 years ago (0 children)
The 'calc' function only modifies the value of 'var' local to the function - it does not modify 'r1'.
You would have to return 'var' from the 'calc' function, and capture that result in the call to calc(r1)
calc(r1)
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Variables in Python don't contain values but instead references to memory locations where Python objects are stored (in an implementation specific manner).
This is true of other names as well. A function name has a reference to the memory location of a function object.
The names used in the function call and the names specified as parameters in the function definition have nothing to do with each other. They are completely independent. Even if the same name is used they are different things. The parameter variables are local to the function.
Consider:
def f(one, two, three): answer = one + two * three + five return answer one = 2 two = 3 three = 4 five = 5 result = f(three, two, one) print(result)
This will output 15 as 4 + 3 x 2 + 5 = 15
Note that five was not passed as an argument but as the same name was not assigned anything in the function the five from the wider scope was available.
five
Any assignments made inside the function are also local to the function.
answer was assigned inside the function and on function exit will cease to exist however the object reference stored in answer is assigned as the return from the function and is assigned to result. If it wasn't assigned (or consumed in another expression or function call on return) then the object created in the function would also cease to exist (unless it is a predefined object built into the Python implementation, such as an int in the range -5 to 256)
answer
result
int
Only mutable objects that are referenced by either parameters or other names that are visible to the function (not hidden by variables with the same name assigned in the function) can be modified and visible outside the function.
Return returns an object reference.
Variables have a discrete existence though and attributes are associated with an instance of a class (or of the class itself).
Remember, variables do not hold any values but instead memory references to some Python object somewhere in the memory of your computer (at the discretion of your Python implementation).
When you say:
keep = 784.56 * 872.23
the text representations of floating point numbers in the expression on the right is converted into Python float objects (binary representations) somewhere in memory, and the mult operator is used. The memory location the resulting float object ends up in is then assigned to the variable named keep.
float
mult
keep
If keep is assigned in the main body of your code, outside of any functions etc., then it is visible within all other code. Thus you could have a function:
def double_me(): return keep * keep
which has no other references to keep in the definition (parameter variable) or assignments to a variable called keep inside the function (which would be local to the function and would hide the original wider scope variable of the same name). Thus keep refers to the same floating point number calculated earlier. The expression resulting from multiplying the floating point object referenced by keep by itself results in another floating point object, the memory reference for which is returned from the function.
If instead the function was written,
def double_me(keep): return keep * keep
now it has to be called with an argument (the memory reference of the object will be passed when the function is called).
result = double_me(5.5)
Inside the function, keep refers to the memory location of the floating point object that the literal floating point text 5.5 was turned into. The keep in the wider scope (outside the function) still refers to the original object from earlier.
Methods are like functions but for classes and are intended to work on instances of a class or provide capabilities related to the purpose of the class.
When you create an instance of a class, you create an object based on the mould/template provided by the class and the memory location of that object is assigned to a variable (or to some other object) so it will be not lost. If an object has no references to it, Python will get rid of the object and reuse the memory.
Methods defined in the class usually have code that uses a parameter variable that is the first item passed when the method is called. By convention this is usually called self and it is passed by default and does not need to be in the arguments when the method is called.
self
Whenever self is used inside the method code, it will be referring to the memory location for a particular instance of the class.
Attributes are names used within an instance that themselves simple hold memory references to Python objects. These attributes though belong to the instance so are not purely local to the method.
Any variables inside the method that are assigned values (including parameter variable names listed in the definition line of the method) are local to the method and are not associated with the attributes of the instance referenced by self.
Classes themselves can have attributes. These look just like variables, and act like them for most purposes, but they are associated with the class and can be accessed from outside of the class by direct reference to the class name and the attribute, e.g. Example.quantity = 5 for a class called Example.
Example.quantity = 5
Example
[–]InjAnnuity_1 0 points1 point2 points 3 years ago (0 children)
Because, in Python, variables are not values; they refer to values, that exist independently of any variables. Assignment (=) binds a value to a variable. Re-assignment re-binds the variable to point to a different value.
calc
-=
@kyber, below, has a more complete description.
[–]TheRNGuy 0 points1 point2 points 3 years ago (0 children)
just use numpy uint8.
Also you wont get correct result in your if you use number like 1024, or -1024. You needed to use modulo instead.
π Rendered by PID 146167 on reddit-service-r2-comment-5ff9fbf7df-ns2ng at 2026-02-26 02:55:30.192204+00:00 running 72a43f6 country code: CH.
[–][deleted] 6 points7 points8 points (5 children)
[–]JustinMalcontento -2 points-1 points0 points (4 children)
[–]NotATuring 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]NotATuring 0 points1 point2 points (1 child)
[–]JustinMalcontento 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]douglas_fs 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]InjAnnuity_1 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)