NoneType List

dn PythonList at DancesWithMice.info
Sat Dec 31 03:58:05 EST 2022


On 31/12/2022 18.45, Goran Ikac wrote:
> Happy New Year, everybody!
> I'm new in the Python List, new in Python world, and new in coding.
> A few days (weeks?) ago, I faced a problem trying to write a program for an
> exercise. I asked for help and nobody answered.
> In the meantime, I found a part of the solution, but a part still remains a
> mystery for me. Please run this small snippet, and help.
> Thanks
> 
> a = [1, 2]
> print()
> 
> print("a ==", a)
> print("type(a) is", type(a))
> 
> b = a.append(3)
> 
> print("b ==", b)
> print("type(b) is", type(b))

Please review https://docs.python.org/3/tutorial/datastructures.html

It is not immediately obvious to people who are not used to 
object-oriented programming.

As observed, "a" is a list. The list contains a pair of numeric-values.

The append is a "method". Methods are functions which belong to classes 
(let's say). Accordingly, the operation is performed on that class, ie 
the appending is to the "a" list.

This can be proven by adding to the above:

print( "\na (appended) =", a )
[1, 2, 3]

As far as the expression on the right-hand side of (the equals sign) 
there is no output-value, hence b == None.


For comparison, try:

b = a + [ 4 ]

and understand why the answer is different, and thus why the first 
'problem' behavior works the way it does...


print( 'Happy new year!' )


PS are you aware of the Python-Tutor Discussion List? It may be more 
suited to assisting your learning intentions...
-- 
Regards,
=dn


More information about the Python-list mailing list