Can you actually have python do math for you?


I’m stuck on the variables and Arithmetic. I am only a kid and want to be a programmer and I want to learn python first, and then Java Script.


Actually the updating variables page is hard too!


Python can handle math operations but it has nothing built in. We have to write the code that we then call upon to do the math we intend.

>>> a + b
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    a + b
NameError: name 'a' is not defined

Python could not run this code because it does not know what a is. Notice it didn’t get to b?

>>> a, b = 6, 7
>>> a + b

As far as we’re concerned, Python knows how to add two values but in fact it does not. It has to reach out to its language interpreter to get a definition for the + symbol. Then carry out the addition process and return a sum. At the low level this is anything but simple. Computers are dumb, and languages are no different. We are the ones who make it all work.

What this essentially means is two things…

  1. We need to understand the math we are doing.
  2. We need to understand the language we are using.

Being at the very beginning as we are, then it may be advisable to stay focused on the lessons. It will lead up to doing basic math in Python. In the meantime, be sure to brush up on your arithmetic skills; addition, subtraction, multiplication, division, exponents and roots are essential.


Yes you can do math using Python!


I was doing learn Python 2 and not the beginner one!


They both start pretty much the same way, at the beginner level. One can learn either, or both, and find they are pretty much the same thing with slight differences. The concepts are the same in both.

Variables have no special meaning or attributes. They are just names. They do not contain anything, regardless what we might read. It’s just a way to describe them that a beginner might understand.

variable => object id => [address in RAM] => value

If two variables are said to have the same value, then they will also have the same id since the value only occurs once in memory. It is held in memory as long as one of the variables still refer to it. Once the variable references change, the value ceases to be.


>>> a = 42
>>> id(a)
>>> b = 42
>>> id(b)
>>> id(a) == id(b)

The number 42 is an object, which object can have several variables referring to it, but the object id will never change.

>>> c = a
>>> id(c)

Note that it is the object that has an id, not the variable.

Consider the following…

>>> t = []
>>> id(t)
>>> u = t
>>> id(u)
>>> t.append(42)
>>> id(u[0])

Notice the two list objects are actually only one and the same. Now notice that the value in the first element has the same id as earlier. The value can only ever be in one place.

>>> d = {'meaning of life': 42}
>>> id(d)
>>> id(d['meaning of life'])

This is fairly heady stuff and should not get more than a passing glance, for now. It is not important to a beginner. What is important is understanding variables and the role they play in identifying objects in memory. Don’t read more into them than is necessary. They are just names.


I tested this and what i have seen is that even after altering the original value and than going back to the original value the id of the object never changes. How does this work with memory ? is it ever going to overwrite this value or will it live forever ?

Its not like every possible value exists already in the memory right ?
Here is the code i used to test this.

a = 42
b = 42

print (id(a))  	#output -> 1500153200
print (id(b))	#output -> 1500153200
print ("-----")

a = a + 2
b = b + 2

# at this point the object 42 should not excist ?
print (id(a))	#output -> 1500153264
print (id(b))	#output -> 1500153264

print ("-----")
a = a - 2
b = b - 2

# expected this id to be diffrent since original value whas overridden. but its not.
print (id(a))	#output -> 1500153200
print (id(b))	#output -> 1500153200


This post was flagged by the community and is temporarily hidden.


When we change a reference, we will get the id of the new object. However, I suspect that the old object will disappear eventually with garbage collection. Can’t say for sure.


Not as I understand it. Integers don’t really exist, as such, but their count does. This could be way off, though.

How does Python memory management work?

Understanding Python variables and Memory Management

The above links were found in this SERP:

how does python work with numbers at the low level in terms of memory

String confusion

Ah thanks, that explained it. It does keep a memory for integers from -5 till 256. once you set the varriables to higher than 256 it starts to make sence.
you can even see the id of value 602 being reused after you subtract 2.

a = 600
b = 600

print (id(a))  	#output -> 2628645334768
print (id(b))	#output -> 2628645334768
print ("-----")

a = a + 2
b = b + 2

print (id(a))	#output -> 2628645332080
print (id(b))	#output -> 2628675926832

print ("-----")
a = a - 2
b = b - 2

print (id(a))	#output -> 2628675926672
print (id(b))	#output -> 2628645332080