NoneType List
MRAB
python at mrabarnett.plus.com
Sat Dec 31 12:37:39 EST 2022
On 2022-12-31 05: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("\nb =", "a.append(3)")
> print("b ==", b)
> print("type(b) is", type(b))
>
> c = ['1', '2']
> print("\nc ==", c)
> print("type(c) is", type(c))
>
> d = c.append('3')
> print("\nd =", "c.append('3')")
> print("d ==", d)
> print("type(d) is", type(d))
>
> """
> I mean: why b = a.append(something) is the None type, and how to make a new
> list
> that contains all the items from a and some new items?
> I wasn't able to solve it in a few hours :(
> Now I know:
> """
>
> crta = '='
> print('\n', 4 * ' ', crta * (len('The solution:')), sep='')
> print(3 * ' ', 'The solution:')
> print(4 * ' ', crta * (len('The solution:')), sep='')
> print('\nThe solution is the slice, an element of Python syntax that
> allows')
> print('to make a brand new copy of a list, or parts of a list.')
> print('The slice actually copies the list\'s contents, not the list\'s
> name:')
>
> print()
> a = [1, 2]
> print("a ==", a)
> print("type(a) is", type(a))
>
> b = a[:]
> print("\nb = a[:]")
> print("b ==", b)
> b.append(3)
> print("\nb =", "b.append(3)")
> print("b ==", b)
> print("type(b) is", type(b))
> print("\na ==", a)
>
> print('But I still don't know why "b = a.append(something)" is the None
> type.')
> print('Is there anybody out there?!')
Methods that modify in-place usually return None. "a.append(something)"
modifies (appends to) the list 'a' and returns None.
If you want to a new line with something at the end try "b = a +
[something]".
More information about the Python-list
mailing list