One, the string (str) is important ...
How to create
- STR1 = "ABC"
- str2 = str ()
Common operations
- S.replace (' A ', ' B ') replaces the A in s with the B
- Str.capitalize () capitalizes the first letter in Str
- Str.upper () Uppercase
- Str.lower () lowercase
- S.swapcase () Uppercase and lowercase
- Str.startswith (' xx ') to determine whether to start with XX
- Str.endswith (' G ') to determine whether the end of G
- Str.isnumeric () determines whether a number
- List = Str.split ('. ') Use the list. Divided into lists, but must be in list.
- '. '. Join (list) with. Connect List to
- S.strip () Remove space
- Sstr1.index (SSTR2) or Sstr1.find (SSTR2) find
- Len (SSTR1) string length
- Split () split
- A[0:3] Slice, which refers to the middle element of >=0<3 in a
- SSTR1 = Sstr1[::-1] Reverses the SSTR1 string and assigns the value to SSTR1
< Span class= "string" > append: Escape character in string:
- < Span class= "string" >\\ slash
\ ' single quotes
\ " Double quotes
\a system horn
\b BACKSPACE
\n line break
\t horizontal tab
\v portrait tab
\r return character
\f page break
\o octal number represents the character
\x hexadecimal number represents the character &NBSP;&NBSP;
\000 Terminator, ignoring all characters after \000
Second, lists (list ordered, mutable) are important .....
How to create
listA = [‘a‘, ‘b‘, ‘c‘, 1, 2]
list(obj)
#把对象转换成列表,obj可以是元组,字典,字符串等
- Note: The list can support multiple layers of nesting.
- Note: If the string needs to contain double quotes, do not forget to escape, "\" "\"
Common operations
- List.append () Append member, member data
- List.pop () Delete Members, delete member I, note: can be individually given to a variable to be deleted
- List.count (x) calculates the number of occurrences of the parameter X in the list
- List.remove () Remove members from list, delete members directly I
- List.extend (l) Append another list to the list L
- List.reverse () Reverses the order of the members in the list
- List.index (x) Gets the position of the parameter X in the list
- List.sort () sort the members in the list
- List.insert () inserting data into the list insert (A, B) inserts data into the list
- The List.isinstance () function can be used to determine whether a particular identifier contains a particular type of data
Third, Ganso (tuple, ordered, non-modifiable) need to add common operations
How to create
tuple1 =()
tuple1 = tuple({1,2,3,4,5,‘6‘})
tuple1 = (1, 2, ‘3‘, 4, ‘5‘)
- Note: You cannot add or modify elements in a tuple after you define a tuple, but you can modify the elements of the following (except Ganso) for grandchildren, add
- Tuple1[-1] refers to the calculation of the last beginning of the Yongzu
Four, the dictionary (Dictionary, unordered, modifiable) is important ....
How to create
- Ict1 = {' name ' : ' Liuzhichao ', ' age ' : , ' sex ' : ' Male '}
- < Span class= "PLN" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" >ict1 = dict ()
< Span class= "PLN" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > common actions
- Dic.clear () Empty dictionary
- Dic.keys () Get a list of keys
- Dic.values () Get a list of values
- Dic.copy () Copy Dictionary
- Dic.pop (k) Delete key k
- Dic.get (k) Gets the value of the key K
- Dic.update () Update member, if member does not exist, equivalent to join
- Dic.items () Gets a list of keys and values
Four, set (unordered, cannot repeat)
How to create
set1 = {1, 2, 3, 4, 5}
- Set2 = set ()
Common operations
set2.add(10) #添加新元素 10,要保证set2中没有10 否则就添加一个10 或添加一个obj
set3 = frozenset(list1)
set3.add(10) 固定集合不能添加元素
s.issubset(t) #如果s是t的子集,返回True,否则返回False
s.
issuperset(t) #如果s是t的超集,返回True,否则返回False
s.
union(t) #返回一个新集合, 该集合是s和t的并集
s.intersection(t) #返回一个新集合, 该集合是s和t的交集
s.difference(t) #返回一个新集合, 该集合是s的成员, 但不是t的成员, 即返回s不同于t的元素
s.copy() #返回一个s的浅拷贝, 效率比工厂要好 #不明白
s.update(t) #用t中的元素 修改s,即s现在包含s或t的成员
s.difference_update(t) #s存在,t不存在,更新t
s.remove(obj) #从集合s中删除对象obj,如果obj不是集合s中的元素(obj not in s),将引发keyError错误
s.discard(obj) #如果obj是集合s中的元素,从集合s中删除对象obj
s.pop() #删除集合s中得任意一个对象,并返回它
s.clear() #删除集合s中的所有元素
- The intersection () method returns a new collection that contains all the elements that appear at the same time in the two collection.
- The Union () method returns a new collection containing elements that are not the same in two collections.
- The Symmetric_difference () method returns a new collection that contains all elements that appear only in one of the collections.
Python basic data results and usage (str,list,tuple,doct,set) Notes