Python learning notes, python
Display type
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import types>>> type([])<class 'list'>>>> type(1)<class 'int'>
In REPL, if you want to display the type in the source code file, you must add print
import typesprint(type([]))a = 123;print(type(a))
Common Data Structures
List
The list is variable-length and can dynamically add elements. The element types are not required to be uniform, ordered, and repeatable.
Create an empty list in two ways
alist = []blist = list()
Directly create a list with elements
alist = ['one', 'two', 3, True];
Append an element to the end
alist.append('four') # api list.append(obj)
Add an element to a specified index
alist.insert(1, 'one point five') # api list.insert(index, obj)
Queries the elements of an index.
element1 = alist[0]element2 = alist[2]print(element1)print(element2)
Modify the element of an index
alist[3] = 'three'
Delete an element at an index location.
Alist = [1, 2, 3, 4] a = alist. pop (1) # Delete api, where the parameter is to be deleted, print (a) #2 print (alist) # [1, 3, 4] alist. pop () # delete the end print (alist) When no parameter is given # [1, 3]
Delete An element. If you do not know the index location
Alist = [1, 2, 2, 3] alist. remove (2) # delete an api. A parameter is the element to be deleted. If the element value is repeated, only the top element is deleted. No returned value print (alist) # [1, 2, 3]
Get list Length
size = len(alist)
print(size)
Traverse list
For v in alist: print (v) # v is the element value.
Retrieve indexes during Traversal
For idx, val in enumerate (alist): print (idx, val) # idx is the index and val is the element value
Reference: http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops
There are several elements for getting a value.
list = [1, 2 , 2, 4, 'a', 'a','b']print(list.count(2)) #2print(list.count('a')) #2
Obtain the maximum element
llist = [1, 2 , 3, 4, True]max = max(list) # apiprint(max) # 4
Be careful when using this api for mixed lists. For example, an error occurs when mixing int and str.
List = [1, 2, 3, 4, 'a'] max = max (list) print (max) run error Traceback (most recent call last): File "c: \ Users \ myname \ Documents \ pythonporjects \ test. py ", line 5, in <module> max = max (list) TypeError: '>' not ororted between instances of 'str' and 'int'
Tuple
Once the elements of the tuples are determined to be unchangeable, they can only be queried, And the tuples themselves cannot add or delete elements. Element types are not required to be uniform, ordered, and repeatable
Create an empty tuple in either of the following ways:
atuple = ()btuple = tuple()
Null tuple is of little use. Therefore, tuple elements are initialized when a tuple is created.
atuple = (1, 2 , 2, 4, 'a', 'b')print(atuple)
Tuple with only one element should be like this
atuple = (1, )print(atuple)
Query Element
atuple = (1, 2 , 2, 4, 'a', 'b')print(atuple[0])print(atuple[1])
If you try to modify the element value, an error is returned.
atuple[0] = 0Traceback (most recent call last): File "c:\Users\myname\Documents\pythonporjects\test.py", line 8, in <module> atuple[0] = 0TypeError: 'tuple' object does not support item assignment
Get tuple Length
t = (1, 2, 3)size = len(t) #apiprintln(size)
There are several elements for getting a value.
t = (1, 2 , 2, 4, 'a', 'a','b')print(t.count(2)) # 2print(t.count('a')) # 2
To obtain the largest element, refer to the list chapter.
Tuple unpacking
t = (1 ,2)a , b = tprint(a) # 1print(b) # 2
Reference http://stackoverflow.com/questions/10867882/tuple-unpacking-in-for-loops
Set
Set is similar to a set in mathematics. elements are unordered and cannot be duplicated.
There is only one way to create an empty set
aset = set()
Traverse the set. The index does not exist because it is unordered.
for el in aset: print(el)
Traverse a set with a growth Length
Http://stackoverflow.com/questions/28584470/iterating-over-a-growing-set-in-python
Create an empty dictionary in either of the following ways:
adict = {}bdict = dict()
Traverse dictionary
Only Traverse key
for k in adict: print(k) # k
Traverse key and value
for key, value in adict.items(): print(key, value)
Reference http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python
Time and date Processing
#!/usr/bin/python# -*- coding:utf-8 -*-import timea = time.time()print("timestamp" , a)a = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print("now is", a)a = time.localtime()year = a.tm_yearprint("year is", year)
More references Python Date and Time | cainiao tutorial http://www.runoob.com/python/python-date-time.html
Reference
Http://www.runoob.com/python/python-tutorial.html
Http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000
Http://stackoverflow.com/questions/tagged/python
Https://www.dotnetperls.com/python