Chapter 2. LISTS & TUPLES. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

FUNCTIONS of LISTS
>>> list ('Hello')
['H', 'e', 'l', 'l', 'o']'
__________________
Item assignments

>>> x = [1,1,1]
>>> x[1]=2
>>> x
[1, 2, 1]
_________________________
Deleting elements

>>> names = ['debora','Alice','Trinity','Sofia']
>>> del names[2]
>>> names
['debora', 'Alice', 'Sofia']
_______________________________________
Slicing
>>> name = list('Perl')
>>> name
['P', 'e', 'r', 'l']
>>> name[2:]=list('ar')
>>> name
['P', 'e', 'a', 'r']
________________________________________
Different length of initial and final list after slicing

>>> name[1:]=list('ython')
>>> name
['P', 'y', 't', 'h', 'o', 'n']
_________________________________________
Slicing without replacing

>>> numbers= [1,5]
>>> numbers[1:1]=[2,3,4]
>>> numbers
[1, 2, 3, 4, 5]
__________________________________________
Reversing of previous action

>>> numbers
[1, 2, 3, 4, 5]
>>> numbers[1:4]=[]
>>> numbers
[1, 5]
______________________________________
LISTS METHODS

append

>>> lst = [1,2,3]
>>> lst
[1, 2, 3]
>>> lst.append(4)
>>> lst

[1, 2, 3, 4]
_____________________________
count

>>> ['to','be','or','not','to','be'].count('to')

2
_______________________________
extend

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]

it's not concatenation, where lists are not modified after action

>>> a+b
[1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3]
____________________________________

index

>>> knights = ['c','v','f','h','l']
>>> knights.index('v')
1
>>> knights[4]
'l'
__________________________________

insert

>>> numbers =[1,2,3,5,6,7]
>>> numbers
[1, 2, 3, 5, 6, 7]
>>> numbers.insert(3,'four')
>>> numbers
[1, 2, 3, 'four', 5, 6, 7]

or like slicing

>>> numbers
[1, 2, 3, 5, 6, 7]
>>> numbers [3:3]=['four']
>>> numbers
[1, 2, 3, 'four', 5, 6, 7]
___________________________________

pop   (deleting the last element)

The pop method is the only list method that both modifies the list and returns a value (other than None).The generally accepted names for the two stack operations (putting things in and taking them out) are push and pop. Python doesn’t have push, but you can use append instead.

>>> x=[1,2,3]
>>> x
[1, 2, 3]
>>> x.pop()
3
>>> x.pop(1)

2
____________________________________________________________________

remove (the first occurence of a value)

>>> x = ['to', 'be', 'or', 'not', 'to', 'be']
>>> x
['to', 'be', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
________________________________________
reverse

>>> x
[1, 2, 3]
>>> x.reverse()
>>> x
[3, 2, 1]
>>> list(reversed(x))
[1, 2, 3]
>>> x
[3, 2, 1]
________________________________________
sort

>>> x
[2, 4, 3, 1, 5, 7, 6, 9, 8]
>>> y=x[:]
>>> y
[2, 4, 3, 1, 5, 7, 6, 9, 8]
>>> y.sort()
>>> y
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x
[2, 4, 3, 1, 5, 7, 6, 9, 8]
________________________________________
Another way of getting a sorted copy of a list is using the sorted function
>>> x
[2, 4, 3, 1, 5, 7, 6, 9, 8]
>>> y = sorted(x)
>>> y
[1, 2, 3, 4, 5, 6, 7, 8, 9]
___________________________________
>>> sorted('python')
['h', 'n', 'o', 'p', 't', 'y']
__________________________________
comparison function, of the form
compare(x,y), which returns a negative number when x < y, a positive number when x > y,
and zero when x == y (according to your definition).

>>> cmp(32,42)
-1
>>> cmp(42,32)
1
>>> cmp(32,32)
0
___________________________________

TUPLES

IT's unmodified sequences with COMMAs

>>> 42,
(42,)
>>> 3*(40+2)
126
>>> 3*(40+2,)
(42, 42, 42)
__________________________
The tuple function

>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple('Python')
('P', 'y', 't', 'h', 'o', 'n')
________________________

>>> x
(1, 2, 3)
>>> x[1]
2
>>> x[1:2]
(2,)
>>> x[1:3]
(2, 3)

Popular posts from this blog

Ketamine: The Past, Present, and Potential Future of an Anesthetic Drug

Fast track anesthesia approaches