A, list list
1 Overview:
Through the first two days of learning, we know that the variable can store data, but a variable can only store one data, there is now a class, the class has 20 people, average age is now seeking class.
If accordance with the previous way to solve
age1 = 10
age2 = 12
...
age20 = 12
average = (age1 + age2 +...+age20)/20
Here, it is clear that this approach is cumbersome, and provides a python solution is to use a list of
Essence: the essence of the list is an ordered list of collection
2. Create a list
Syntax: list name = [element 1, element 2, element 3 ... Description: The List option is called elements, similar with string, subscript is from 0 count
Use: Create a list
#创建空列表
list1 = []
#创建带有元素的列表
list2 = [10, 20, 30, 10]
print(list2)
In the list of data elements can be different types (flexibility)
list3 = [33, "good", True, 10.32]
print(list3)
3. Access a list of elements
3.1 List of values
Function: access list element value list
Syntax: list name [index]
list4 = [22, 33, 12, 32, 45]
#下标从0开始,最大值为len(list4)-1
print(list4[0])
Note: When the index value is greater than len (list4) -1, the following error occurs:
print(list4[5])
IndexError: list index out of range
This error is out of bounds [subscript subscript out of range can be represented]
Replace 3.2 list elements
Function: Change the list element value
Syntax: list name [index] = value
list4 = [22, 33, 12, 32, 45]
list4[0] = "hello"
print(list4[0])
4. Operation List
4.1 List combination
Syntax: List List 3 = List 2 + 1
list1 = [1, 2, 3]
list2 = ['hello', 'yes', 'no']
list3 = list1 + list2
print(list3)
4.2 repeat the list
Syntax: List List 1 * n = 2
list1 = [1, 2, 3]
list2 = list1 * n
print(list2)
4.3 determine whether the elements in the list
Syntax: element in the list
If there returns True, otherwise False
list1 = [1, 2, 3]
print(1 in list1)
4.4 interception list
Syntax: List [start: end] retrieves from the beginning to the underlying index at the end of all the elements [start, end)
list1 = [1, 2, 3, 'hello', 'yes', 'no']
print(list1[2:4])
#若不指定start,则默认从0开始截取,截取到指定位置
#若不指定end,则从指定位置开始截取,截取到末尾结束
4.5 two-dimensional list
Syntax: = list [List 1, List 2, List 3, ..., list n]
#创建二维列表,即列表中的元素还是列表
list1 = [[1, 2, 3],[2, 3, 4],[5, 4, 9]]
4.5 two-dimensional list value
Syntax: list name [subscript 1] [subscript 2]
Note: subscript 1 represents the n-th list (index starts from 0), the subscript n represents the number 2 in the list of n elements
list1 = [[1, 2, 3],[2, 3, 4],[5, 4, 9]]
print(list1[0][0])
5. The method of list
5.1 list.append (elements / list)
Function: Add a new element added to the end of the list of elements in the original [list]
Note: The values () may be in append a list can also be a common element
>>> list1 = [3, 4, 6]
>>> list1.append(6)
>>> print(list1)
[3, 4, 6, 6]
5.2 list.extend (list)
Function: a plurality of additional one-time value is added at the end of a list in the list
Note: The value () extend only in a list / tuple [a iterable], the element can not be
>>> list1 = [1,2,3]
>>> list2 = [3, 4,5]
>>> list1.extend(list2)
>>> print(list1)
[1, 2, 3, 3, 4, 5]
5.3 list.insert (subscript values, elements / list)
Function: the subscript of the insertion element, does not cover the original data, the original data is extended rearwardly
Note: The inserted data can be also be a list of elements
>>> list1 = [1,2,3]
>>> list1.insert(1,0)
>>> print(list1)
[1, 0, 2, 3]
>>> list1.insert(1,[2, 4, 8])
>>> print(list1)
[1, [2, 4, 8], 0, 2, 3]
5.4 list.pop (index value)
Function: Removes the element at the index specified in the list (the default last element removed), and returns the data to remove
>>> list1 = [1, [2, 4, 8], 0, 2, 3]
>>> list1.pop()
3
>>> print(list1)
[1, [2, 4, 8], 0, 2]
>>> list1.pop(2)
0
>>> print(list1)
[1, [2, 4, 8], 2]
5.5 list.remove (element)
Function: Remove an element in the list of the first match result
>>> list1 = [1, 2, 3]
>>> list1.remove(2)
>>> print(list1)
[1, 3]
5.6 list.clear()
Function: Clear all the data list
>>> list1 = [1, 2, 3]
>>> list1.clear()
>>> print(list1)
[]
5.7 list.index (subscript value [, start] [, stop])
Function: to find out the value of an index value of the first match from the list of specified range
If the specified range, the default is the entire list.
>>> list1 = [1, 2, 3]
>>> list1.index(2)
1
>>> list1.index(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 4 is not in list
Note: If you can not find this element in the list, it will error.
5.8 list.count (element)
Check the number of elements that appear in the list: function
>>> list1 = [1, 2, 3, 1]
>>> list1.count(1)
2
5.9 len (list)
Function: Gets the number of elements in the list
>>> list1 = [1, 2, 3, 1]
>>> len(list1)
4
5.10 max(list)
Syntax: get the maximum list
>>> list1 = [1, 2, 3, 1]
>>> max(list1)
3
5.11 min(list)
Syntax: get the minimum list
>>> list1 = [1, 2, 3, 1]
>>> min(list1)
1
5.12 list.reverse()
Syntax: flashback list
>>> list1 = [1, 2, 3, 1]
>>> list1.reverse()
>>> print(list1)
[1, 3, 2, 1]
5.13 list.sort()
Syntax: list is sorted in ascending order by default
>>> list1 = [1, 2, 3, 1]
>>> list1.sort()
>>> print(list1)
[1, 1, 2, 3]
5.14 shallow copy
Syntax: list1 = [1, 2, 3]
list2 = list1
>>> list1 = [1, 2, 3, 1]
>>> list2 = list1
>>> print(list2)
[1, 2, 3, 1]
>>> print(id(list1))
4314476424
>>> print(id(list2))
4314476424
Note: shallow copy copy as a reference, like shortcuts
5.15 deep copy
Syntax: list1 = [1, 2, 3]
list2 = list1.copy()
>>> list1 = [1, 2, 3, 1]
>>> list2 = list1.copy()
>>> print(list2)
[1, 2, 3, 1]
>>> print(id(list2))
4314525320
>>> print(id(list1))
4314524808
Note: deep copy is memory copy, opened up a new memory space
5.16 list (tuple)
Function: tuples into a List
>>> list1 = list((1, 2, 3, 4))
>>> print(list1)
[1, 2, 3, 4]
6. traverse the list
6.1 traverse the list using a for loop
grammar:
for the variable name in the list:
Statement
Function: for loop is mainly used to traverse the list
Traversing: means sequentially accessing each element in the list, obtaining the value of each element corresponding to subscript
Description: According to acquire each element in the list order, assigned to the variable name, and then execute the statement, and so on ad infinitum, until the list of all the elements to take complete date
>>> list1 = ['hello', 78, '你好', 'good']
>>> for item in list1:
... print(item)
...
hello
78
你好
good
6.2 Using while loops through the list [a subscript cycle]
grammar:
Index = 0
while the subscript <length of the list:
Statement
Index = 1 +
>>> list1 = ['hello', 78, '你好', 'good']
>>> index = 0
>>> while index < len(list1):
... print(list1[index])
... index += 1
...
hello
78
你好
good
6.3 while traversing the index of the element
grammar:
for index variables in enumerate (list)
Statement
>>> list1 = ['hello', 78, '你好', 'good']
>>> for index,item in enumerate(list1):
... print(index, item)
...
0 hello
1 78
2 你好
3 good
enumerate()[枚举]函数用于一个可遍历的数据对象(如列表,元组或者字符串)组合为一个索引序列,同时列出数据与数据下标,一般使用在for循环中
enumerate(obj, [start =0])
obj:一个可迭代对象
start:下标起始位置
Exercise:
1. For a list bubble sort
2. Remove the list of repeating elements
3. To achieve such flip string: hello => oleo
Two, turtle graphics module
1.turtle use
turtle is a simple drawing tools, he was offered a small turtle, you can understand it as a small robot
Only understand a limited instruction.
Use when you need to import turtle library: import turtle
In the middle of the origin (0,0) of the drawing window
By default, the turtle moves to the front-right
2. Operation command
import turtle
#程序继续执行,也就是代码不执行完毕但不关闭窗口
turtle.done()
2.1 motion command
turtle.forward (d): move forward length d
turtle.backward (d): move backward length d
turtle.right (d): the number of degrees turned to the right
turtle.left (d): the number of rotation of the left
turtle.goto (x, y): is moved to the coordinate (x, y) position
turtle.speed (speed): brushed speed [0, 10]
2.2 Stroke Control Command
turtle.up (): Stroke lift, while on the move will not only brush drawing [mobile]
turtle.down (): pen down, moving the drawing
turtle.setheading (d): changing the orientation of the brush, the number of
turtle.pensize (d): changing the thickness of the brush
turtle.pencolor (color): change the color of the brush
turtle.reset (): restores all set, clear the window, the reset state turtle
turtle.clear (): Empty window
2.3 Draw graphics
turtle.circle (r, steps = n): Default draw a circle, r is the radius, n is equal to a few few side row
turtle.begin_fill (): start filling
turtle.fillcolor (color): Fill Color
turtle.end_fill (): End fill
2.4 Other commands
turtle.done (): the program continues
turtle.undo (): one action on the revocation
turtle.hideturtle (): Hide the baby turtles
turtle.showturtle (): displays a small turtle
turtle.screensize (x, y) set the window size
Three, tuple tuple
1 Overview
It is essentially an ordered set, and the list is very similar to the list using the [], a tuple using () represents
Features: Once initialized, you can not change
2. Create a tuple
format:
Tuple name = (element 1, element 2, element 3, ...)
#创建空的元组
tuple1 = ()
print(tuple1)
#创建带有元素的元组
tuple2 =(22, 33, 14, 109)
print(tuple2)
#与列表类似,元组中的元素类型可以不同
tuple3 = (23,True,"good")
print(tuple3)
#定义只有一个元素的元组
tuple4 = (1) #这定义的不是元组而是整数1
tuple4 = (1,) #定义一个元素的元组定义时必须加一个逗号",",用来消除歧义
3. Access tuple elements
3.1 Accessing element tuple:
Format: tuple name [index]
tuple1 = (20, 40, 201, 401)
print(tuple1[0])
#注意使用下标取值的时候,要注意下标的取值范围,不要下标越界
#获取最后一个元素
print(tuple1[-1])
3.2 modify tuples
In the tuple defined when we all know that a tuple can not be changed once initialized, but if I want to change the tuple now how to do it?
Tuples can not be changed, but the list may be, the data type of the element tuple can be of different types, so we can add a list of tuples, and the list can be modified, and then to "modify" We tuples
tuple1 = ('hello', 'you',[20, 30])
#修改元组
tuple1[0] = 'hi'
#报错,元组不能修改
tuple1[2][1] = 'good'
Note: On the surface our tuple indeed changed, but not in fact change our tuple, but the list of elements, the so-called tuple is unchanged, it said, pointing to each element of the tuple will never change, Once it was pointed at the list, you can not change that point to other objects, but point to the list itself is variable!
3.3 Delete tuple
Tuples are immutable, but we can use the del statement to delete the entire tuple
tuple1 = ('hello', 'hi')
del tuple1
print(tuple1)
#此时会报错
4. Operation tuple
4.1 yuan group connection composition
grammar:
Element Group 1 Group 2 element
3-tuple tuple = 2 1 + tuple
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print(tuple1 + tuple2)
#结果
(1, 2, 3, 4, 5, 6)
Note: the connection element does not change the original composition and the tuple, but generates a new tuple.
4.2 yuan sets of duplicate
grammar:
Tuple = 2 1 * n-tuple
tuple1 = (1, 2, 3)
tuple2 = tuple1 * 3
print(tuple2)
#结果
(1, 2, 3, 1, 2, 3, 1, 2, 3)
4.3 Analyzing element is in the tuple
grammar:
Element in the tuple
If there returns True, otherwise False
tuple1 = (1, 2, 3)
print( 1 in tuple1)
#结果
True
tuple1 = (1, 2, 3)
print( 5 in tuple1)
#结果
False
Groups taken 4.4 yuan
grammar:
Tuple name [start: end]
Taken tuple range [start, end)
Function: Get start index to all elements before the end of the subscript.
If not specified, the default is to start from the beginning to the interception of a specified location
If the default is taken from a specified end position to the end of the specified
tuple1 = (1, 2, 3, 8, 'hello', 'good')
tuple2 = tuple1[2:5]
print(tuple2)
#结果
(3, 8, 'hello')
tuple1 = (1, 2, 3, 8, 'hello', 'good')
tuple2 = tuple1[:5]
print(tuple2)
#结果
(1, 2, 3, 8, 'hello')
tuple1 = (1, 2, 3, 8, 'hello', 'good')
tuple2 = tuple1[3:]
print(tuple2)
#结果
(8, 'hello', 'good')
5. tuple
5.1 len (tuple)
Function: Get the number of elements tuple
tuple1 = (1, 2, 3, 8, 'hello', 'good')
print(len(tuple1))
#结果
6
5.2 max(tuple)
Function: Get the maximum value of the elements in the tuple
tuple1 = (1, 2, 3, 8, 20, 13)
print(max(tuple1))
#结果
20
5.3 min(tuple)
Function: Get the minimum value of the elements in the tuple
tuple1 = (1, 2, 3, 8, 20, 13)
print(min(tuple1))
#结果
1
5.4 tuple(list)
Function: Convert list is a tuple
list1 = [1, 2, 3, 8, 20, 13]
print(tuple(list1))
#结果
(1, 2, 3, 8, 20, 13)
6. The two-dimensional tuples
And two-dimensional list is similar to one tuple element is still the tuples, become two-dimensional tuples.
Name = tuple (a tuple, the tuple 2, ...)
tuple1 = ((1, 2, 3),(4, 5, 6),(7, 8, 9))
Two-dimensional tuple values
Tuple name [index 1] [index 2]
tuple1 = ((1, 2, 3),(4, 5, 6),(7, 8, 9))
#获取第一个元组的第一个元素
print(tuple1[0][0])
#结果
1