environment: Win7 pycharmce_2017.2 python3.6.2
Take one last review. 3.list and tuple
The list is also a data type. You can delete, add elements at any time
And the elements in the list are not restricted by type, as opposed to arrays in C, or int or char. and the list can
>>> a=[1,2,3, ' Hi ', [4, ' hi~ ']]
>>> a
[1, 2, 3, ' Hi ', [4, ' hi~ ']]
>>> a[0]
1
>>> a[4][1]
' hi~ '
>>> a[-1]
[4, ' hi~ ']
>>>
That is, the list of what data types can be, but can be indexed by subscript, there is a difference between C, Python can be indexed from the end, subscript '-1 ' represents the last one, and the same-2 is the penultimate second. The list in the list is as good as a two-dimensional array, and it's easy to understand.
★ Add elements to the end of the list
>>> a
[1, 2, 3, ' Hi ', [4, ' hi~ ']]
>>> a.append (2)
>>> a
[1, 2, 3, ' Hi ', [4, ' hi~ '], 2]
>>>
★ Add elements to the specified position in the list
>>> a
[1, 2, 3, ' Hi ', [4, ' hi~ ']]]
>>> a.insert (2, ' Word-word ')
>>> a
[1, 2, ' Word-word ', 3, ' Hi ', [4, ' hi~ ']]
>>>
Note that a.insert means to add an element, such as A.insert ( -1,99), to the front of the first element, and not to add a 99 to the penultimate position, but to add a 99 to the front of the penultimate element.
>>> a
[2, ' Word-word ', 3, 3,, ' Hi ']
>>> a.insert ( -1,99)
>>> a
[2, ' Word-word ', 3, 3,,,, ' Hi ']
>>>
★ List Delete element at specified location
List.pop (i): delete the first element of the list (the element is numbered from 0), I do not fill in-1, that is, delete the last element, the element at the end
>>> a
[1, 2, ' Word-word ', 3, ' Hi ', [4, ' hi~ ']]
>>> a.pop ()
[4, ' hi~ ']
>> > A
[1, 2, ' Word-word ', 3, ' Hi ']
>>> a.pop (0)
1
>>> a
[2, ' Word-word ', 3, ' Hi ']
>>>
Next is tuple. Tuple and the list are very similar, but tuple and C language of the const is very similar, once the definition is not modified. And the list of the difference is also the list with brackets [x,x,x,x],tuple with parentheses, of course, curly braces is a dictionary.
★ There are 2 small details, first:
If the list is only 1 digits, then it's OK, but tuple is using parentheses, in order for Python to know that you are here to define a tuple of 1 elements, a= (1,): A=[x.
>>> A=[1]
>>> a
[1]
>>> a= (1)
>>> A A
it's a number, not a tuple.
>>> a= (1,)
>>> a
(1,)
>>>
Second small detail: the invariant of a tuple refers to the tuple. Once each element is defined, "point" does not change. For example:
>>> a
[' A ', ' B ']
>>> b
(1, 2, [' A ', ' B '])
>>> b[0]=1
traceback (most Recent call last):
File "<stdin>", line 1, in <module>
typeerror: ' Tuple ' object does not support item Assignment
>>> b[2][0]=1
>>> b
(1, 2, [1, ' B '])
>>> a[1]=2
>> > B
(1, 2, [1, 2])
>>> c=[' Hi ']
>>> b[2]=c
traceback (most recent call last): C16/>file "<stdin>", line 1, in <module>
typeerror: ' Tuple ' object does not support item assignment
>>>
b The 2nd element of this tuple is the list of a, once the definition can no longer be replaced by the C, but the elements in the list are free to add deletion changes.
As for exercise:
# Coding:utf8
L = [
[' Apple ', ' Google ', ' Microsoft '],
[' Java ', ' Python ', ' Ruby ', ' PHP '],
[' Adam ', '] Bart ', ' Lisa ']
] # print
Apple: Print
(l[0][0])
# print Python:
print (l[1][1])
# print Lisa:
print (l[2][2])
#结果:
Apple
Python
Lisa
4. Conditional Judgment and circulation
Conditional judgment and C language very much like, If–else If–else If–else, just became if–elif–elif–else, nothing to say like ~
Loops and C are also more like, there are two kinds, a for loop a while loop, first said for loop:
The python for loop format is
For x in (. ) Pass
Which is for...in ... Structure, in followed by the type of data that can be iterated, that is, the iteration, which is reviewed in subsequent iterations, is often followed by a list, a tuple, a dictionary, and a function that returns the three data types above, such as range:
>>> a= (1,2,3)
>>>> for temp in a: ... Print (temp) ...
1
2
3
>>> for temp in range (0,5):
... Print (temp) ...
0
1
2
3
4
>>>
The while loop is similar to C.
>>> a=5
>>> while a>0:
... A-=1 ... Print (a) ...
4
3
2
1
0
>>>
and C Also, there are break and continue, use the same. :
>>> a=5
>>> while a>0:
... A-=1 ... if (a==3): .. Break
... Print (a) ...
4
>>> a
3
>>> a=5
>>> while a>0:
... A-=1 ... if (a==3): .. Continue ... Print (a) ...
4
2
1
0
>>>
As for the exercise, write a for, while the do not write, simple
#-*-Coding:utf-8-*-
l = [' Bart ', ' Lisa ', ' Adam '] for
temp in L:
print (' Hello ' +temp)
#结果:
Hel Lo Bart
Hello Lisa
Hello Adam
5.dict and set
Dict is a dictionary and is a built-in data type, using key-value (Key-value) storage. A dictionary can query value by key, or add an element by adding value to a key, such as:
>>> a={' Tom ': "John":
{} >>> a
{' Tom ': ", ' John ':}
>>> a[' John ']
>>> a[' adm ']=92
>>> a
{' Tom ': The "John": "The", ' adm ': "The}
>>>
Can also be queried by get, found will return the value of this key, not found on the return of None or a specified value:
>>> a
{' Tom ': "John": Mr, ' ADM ':
a.get} >>> a.get (' Tom ')
Tom ', yes ')
>>> a.get (' tommm ', ' not ') ' not
'
>>>
It must be noted that the dictionary key must be immutable objects, such as list this variable object can not be a key:
>>> a={' Tom ': "John": Mr, ' ADM ': 92,[1,2]:2}
Traceback (most recent call last):
File <stdin> ", line 1, in <module>
typeerror:unhashable type: ' list '
>>> a={' Tom ':", ' John ': ", ' ADM ': 92 , 2:[3,2]}
>>> a[2]
[3, 2]
>>>
Also want to say, the dictionary key is unique.
>>> a={' Tom ': "John": "$, ' adm ':", "ADM": "
>>> a
{' Tom ':", "John": 34}
>>>
To remove the elements in the dictionary using the Pop method:
>>> a
{' Tom ': "John": Mr, ' ADM ':}
>>> A.pop (' adm ')
>>> A
{' Tom ': "John": 89}
Let's talk about set. Set and dictionary are more like, but only key has no value. Defining a set to pass in a list, plus key cannot be repeated, means that there are no identical elements in set:
>>> A=set ([3,333,33,3333,3,33333,333])
>>> a
{3, 3333, 333, 33333}
>>>
Delete and add the Add method and remove method:
>>> a
{3, 3333, 333, 33333}
>>> A.add (333333)
>>> a
{33, 3, 3333, 333, 333 333333}
>>> A.add (3)
>>> a
{3, 3333, 333, 33333, 333333}
>>> A.remove (3)
>>> a
{3333, 333, 33333, 333333}
>>>
Mr. Liao said:
Set can be regarded as a mathematical sense of unordered and no repeating elements of the set, therefore, two sets can do in the mathematical sense of intersection, and set and other operations
Other words:
>>> a
[1, 3, 4]
>>> a=set ([1,3,4])
>>> b=set ([1,5,6,7,2,88,4])
> >> a|b
{1, 2, 3, 4, 5, 6, 7, $}
>>> a&b
{1, 4}
>>>