Introducing to Python Sequence Types — ListTuple & Range

Lists are mutable sequences for storing collections of homogeneous items.
Lists may be constructed in the following ways:
Using a pair of square brackets to denote the empty list

>>> myList = []
>>> myList
[]
>>> type(myList)
<class 'list'>

 

Using square brackets, separating items with commas
>>> myList = [10, 20, 30]
>>> myList
[10, 20, 30]
>>> type(myList)
<class 'list'>

>>> myList2 = ['a','b','c']
>>> myList2
['a', 'b', 'c']
>>> type(myList2)
<class 'list'>

# Replace 'b','c' with 'd','e'
>>> myList2[1:3] = ['d','e']
>>> myList2
['a', 'd', 'e']

# Delete 'd' & 'e'
>>> del myList2[1:3]
>>> myList2
['a']

# Append 'p' to the end of the list
>>> myList2.append('p')
>>> myList2
['a', 'p']

# Shallow Copy myList2 to myList3
>>> import copy
>>> myList3 = copy.copy(myList2)
>>> myList3
['a', 'p']
>>> type(myList3)
<class 'list'>

# Remove All items from the list
>>> myList3.clear()
>>> myList3
[]

# Comparison of Shallow Copy and Deep Copy
The difference between shallow copy and deep copy is only relevant for compound objects (objects that contain other objects such as lists or class instances).

Shallow Copy: changes made to the copied object DO CHANGE the original object as well.
>>> import copy
>>> myList1 = [1, 2, [3,6], 5]
>>> myList1

[1, 2, [3, 6], 5]
>>> myScopy = copy.copy(myList1)
>>> myScopy

[1, 2, [3, 6], 5]
# Make change to the compound object
>>> myScopy[2][1] = 4
>>> myScopy

[1, 2, [3, 4], 5]
>>> myList1
[1, 2, [3, 4], 5]



Deep Copy: changes made to the copied object DO NOT CHANGE the original object.
>>> import copy
>>> myList1 = [1, 2, [3,6], 5]
>>> myList1

[1, 2, [3, 6], 5]
>>> myDcopy = copy.deepcopy(myList1)
>>> myDcopy

[1, 2, [3, 6], 5]
# Make change to the compound object
>>> myDcopy[2][1] = 4
>>> myDcopy

[1, 2, [3, 4], 5]
>>> myList1
[1, 2, [3, 6], 5]

Using a list comprehension: [x for x in iterable]
>>> myList = [x for x in range(10)]
>>> myList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> type(myList)
<class 'list'>


Using the type constructor: list() or list(iterable)
>>> myList = list('Danbrother')
>>> myList
['D', 'a', 'n', 'b', 'r', 'o', 't', 'h', 'e', 'r']
>>> type(myList)
<class 'list'>

Tuples are immutable sequences for storing collections of heterogeneous data.
Tuples may be constructed in the following ways:
Using a pair of parentheses () to denote the empty tuple

>>> myTuple = ()
>>> myTuple
()
>>> type(myTuple)
<class 'tuple'>

 

Using a trailing comma (,) for a singleton tuple
>>> myTuple = 10,
>>> myTuple
(10,)
>>> type(myTuple)
<class 'tuple'>

>>> myTuple2 = (10,)
>>> myTuple2
(10,)
>>> type(myTuple2)
<class 'tuple'>

 

Separating items with commas (,)
>>> myTuple = ('John' , 'Bob' , 'Cathy')
>>> myTuple
('John', 'Bob', 'Cathy')
>>> type(myTuple)
<class 'tuple'>

>>> myTuple2 = (10, 20, 30)
>>> myTuple2
(10, 20, 30)
>>> type(myTuple2)
<class 'tuple'>

Using the tuple() built-in: tuple() or tuple(iterable)
>>> myTuple = tuple()
>>> myTuple
()
>>> type(myTuple)
<class 'tuple'>

>>> myTuple2 = tuple( [1, 2, 3])
>>> myTuple2
(1, 2, 3)
>>> type(myTuple2)
<class 'tuple'>

Range type represents an immutable sequence of numbers and is commonly used for looping a specific number of items in for loops.
 

class range(stop)
>>> myRange = range(10)
>>> myRange
range(0, 10)
>>> list(myRange)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> type(myRange)
<class 'range'>

 

class range(start, stop[, step])
>>> myRange = range(2,21,3)
>>> myRange
range(2, 21, 3)

>>> list(myRange)
[2, 5, 8, 11, 14, 17, 20]

 

[Reference]
https://docs.python.org/3/library/stdtypes.html#list
http://www.geeksforgeeks.org/copy-python-deep-copy-shallow-copy/

 

 

 

arrow
arrow
    文章標籤
    Python
    全站熱搜

    DanBrother 發表在 痞客邦 留言(0) 人氣()