Difference between revisions of "My Python list notes"

From Got Opinion Wiki
Jump to navigation Jump to search
(initial page creation)
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
My Python list notes
== Create lists ==
<pre>>>> l = []
<pre>>>> l = []
>>> type(l)
>>> type(l)
Line 20: Line 23:
<class 'list'>
<class 'list'>
>>></pre>
>>></pre>
== List slicing ==
<pre>>>> l[:-2]
[0, 1, 2, 3, 4, 5, 6, 7]
>>> l[-2:]
[8, 9]
>>> l[:2]
[0, 1]
>>> l[2:]
[2, 3, 4, 5, 6, 7, 8, 9]
>>> l[2:-2]
[2, 3, 4, 5, 6, 7]</pre>


<center>[[Python programming|To Python Programming]]</center>
<center>[[Python programming|To Python Programming]]</center>

Latest revision as of 10:28, 7 February 2022

My Python list notes

Create lists

>>> l = []
>>> type(l)
<class 'list'>
>>> l.append(1)
>>> l
[1]
>>> l = range(10)
>>> l
range(0, 10)
>>> type(l)
<class 'range'>
>>> l = range(10).list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'range' object has no attribute 'list'
>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> type(l)
<class 'list'>
>>>

List slicing

>>> l[:-2]
[0, 1, 2, 3, 4, 5, 6, 7]
>>> l[-2:]
[8, 9]
>>> l[:2]
[0, 1]
>>> l[2:]
[2, 3, 4, 5, 6, 7, 8, 9]
>>> l[2:-2]
[2, 3, 4, 5, 6, 7]


To Python Programming