Python basics 7: python Basics

Source: Internet
Author: User

Python basics 7: python Basics
I. Data Type Supplement 1. str

#1. casefold # usage: S. casefold () # meaning: the lower () method is only ASCII encoded, that is, 'a-Z', effective for other languages (non-Chinese or English) can only use the casefold () method # usage: s = 'abbby' print (s. casefold () # abbex #2. expandtabs # usage: S. expandtabs (tabsoze = 8) # meaning: Convert the tab symbol ('\ t') in the string into spaces. The default number of spaces for the tab symbol (' \ t') is 8. # Start from scratch. the number to the first \ t is exactly 8 spaces. If not, fill in spaces. If there is \ t, then the number from the first \ t to the second \ t is still 8 spaces, and so on until the last \ t ends. # Usage S = "this is \ tstring example... wow !!! "Print (" original string: "+ S) # original string: this is string example... wow !!! Print ("replace \ t:" + S. expandtabs () # Replace \ t: this is string example ...... wow !!! Print ("replace \ t with 16 spaces:" + S. expandtabs (16) # use 16 spaces to replace the \ t Symbol: this is string example .... wow !!!
2. tuples

For tuples: if there is only one element and there is no comma, what data type is this element and what data type is this expression

Tu = (1) tu1 = (1,) print (tu, type (tu) #1 <class 'int'> print (tu1, type (tu1 )) # (1,) <class 'tuple'> tu = ('old boys') tu1 = ('old boys',) print (tu, type (tu )) # Old Boy <class 'str'> print (tu1, type (tu1) # ('Old Boy ',) <class 'tuple'> tu = ([1, 2, 3]) tu1 = ([1, 2],) print (tu, type (tu) # [1, 2, 3] <class 'LIST'> print (tu1, type (tu1) # ([1, 2, 3],) <class 'tuple'>
3. list

When looping a list, it is recommended that you do not delete the list (once deleted, the index will change), which is prone to errors. 55]

# Li = [, 55] # delete an element with an odd index. Li = [11,22, 33,44, 55] # del li [1: 2] # print (li) # [11, 33, 55] ll = [] for I in range (len (li): if I % 2 = 0: ll. append (li [I]) li = llprint (li) # [11, 33, 55]
li = [11,22,33,44,55]# for i in range(len(li)):#     print(li)  # [11, 22, 33, 44, 55]    [11, 22, 33, 44, 55]  [11, 33, 44, 55] [11, 33, 44, 55]  [11, 33, 44]#     print(i)  # 0  1 2 3 4#     if i % 2 == 1:#         del li[i]#     print(li)  # [11, 22, 33, 44, 55]  [11, 33, 44, 55]  [11, 33, 44, 55]  [11, 33, 44]  [11, 33, 44]#     print(i)  # 0  1  2  3 4# print(li)  # list assignment index out of range# del li[100]  # list assignment index out of range# for i in li:#     print(li)#     li.remove(i)#     print(li)# print(li)# for i in range(len(li)-1,-1,-1):#     if i % 2 == 1:#         del li[i]# print(li)
4. dict
# The Python Dictionary fromkeys () function is used to create a new Dictionary and use the elements in sequence seq as the Dictionary key. value is the initial value corresponding to all keys in the Dictionary # dict. fromkeys (seq [, value]) dic = dict. fromkeys ('abc', 'Alex ') print (dic) # {'C': 'Alex', 'A': 'Alex ',' B ': 'Alex '} dic = dict. fromkeys ([1, 2], []) print (dic) # {1: [], 2: [], 3: []} dic [1]. append ('shanghai') print (dic) # {1: ['shanghai'], 2: ['shanghai'], 3: ['shanghai']}
Dic = {'k1 ': 'value1', 'k2': 'value2', 'name': 'wusir'} # Keys containing k elements in the dictionary, delete the corresponding key-value pair. # The key-value pairs of the dictionary cannot be added or deleted in the circular dictionary. # Dictionary changed size during iteration # for I in dic: # if 'K' in I: # del dic [I] # count = 0 # for I in dic: # dic [I + str (count)] = dic [I] # count + = 1 # l1 = [] # for I in dic: # if 'K' in I: # l1.append (I) # for I in l1: # del dic [I] # print (dic)
Ii. set

Set: the data type is non-duplicate and unordered. Its elements can be hashed. It cannot be hashed, and it cannot be used as the dictionary key.

Purpose: 1. remove duplicates.

2. Test the data relationship.

# Deduplication li = [, 22, 33,] li = list (set (li) print (li) # [33, 11, 44, 22] set = {11,22, 33,11, 22,33, 44} print (set) # {33, 11, 44, 22} # Add set1 = {'Alex ', 'wusir ', 'ritiany', 'egon', 'barry'} set1.add ('SB ') print (set1) # {'SB', 'barry', 'egon', 'ritiany ', 'wusir ', 'Alex'} set1.update ('abc') set1.update ([1, 2]) # print (set1) # {'SB ', 1, 2, 3, 'barry', 'egon', 'ritiany', 'A', 'B', 'wusir ', 'Alex ', 'C'} # Delete set1.remove ('SB ') print (set1) # {'C', 1, 2, 'ritianc', 'wusir', 3, 'barry ', 'B', 'A', 'Alex ', 'egon'} set1.clear () print (set1) # set () del set1 # print (set1) # set1 = {, 5} set2 = {, 8} # intersection & intersectionprint (set1 & set2) # {4, 5} print (set1.intersection (set2) # {4, 5} # inverse intersection ^ symmetric_differenceprint (set1 ^ set2) # {1, 2, 3, 6, 7, 8} print (set. symmetric_difference (set2) # {1, 2, 3, 6, 7, 8} # difference set | unionprint (set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8} print (set1.union (set2) # {1, 2, 3, 4, 5, 6, 7, 8} set1 = {1, 2, 3} set2 = {1, 2, 4, 5, 6} print (set1.issubset (set2) # True subset print (set2.issuperset (set1 )) # True set2 is set1's superset set1 = {'barry', "wusir"} set2 = frozenset (set1) # Turns the set into a hashable, ordered set print (set2, type (set2) # frozenset ({'barry', 'wusir '}) <class 'frozenset'>
Iii. copy
L1 = [1, 2, 3] l2 = l1l2. append (111) # print (l1, l2) print (id (l1) #2682785139272 print (id (l2) #2682785139272 # For the value assignment operation, it points to the same memory address. Dictionary, list, and set are all the same. S = 1000s1 = sprint (id (s) #2682776128688 print (id (s1) #2682776128688
L1 = [1, 2, 3] l2 = l1l2. append (111) # print (l1, l2) print (id (l1) #2682785139272 print (id (l2) #2682785139272 # For the value assignment operation, it points to the same memory address. Dictionary, list, and set are all the same. S = 1000s1 = sprint (id (s) #2682776128688 print (id (s1) #2682776128688 # copy does not point to one, A memory space is opened in the memory. # For the light copy, the first layer creates a new memory address. # From the second layer, the memory address is directed to the same memory address, # therefore, the Layer 2 and deeper layers must be consistent. # L1 = [111, 3] # l2 = l1.copy () # l1.append () # print (id (l1), id (l2) l1 =, 3, [22, 33, 44], 4] l2 = l1.copy () # l1 [2]. append (666) print (l1) # [1, 2, [1, 2, 3, [22, 33, 44], 4] print (l2) # [1, 2, [1, 2, 3, [22, 33, 44], 4] print (id (l1) #1603100643016 print (id (l2 )) #1603100738056 # deep. copy # for deep copy, the two are completely independent. # change any element of any one (no matter how many layers), and the other will never change. import copyl1 = [1, 2, [1, 2, 3], 4] l2 = copy. deepcopy (l1) l1 [2]. append (666) print (l1, l2) # [1, 2, [1, 2, 3,666], 4] [1, 2, [1, 2, 3], 4] print (id (l1 [2]), id (l2 [2]) # print (id (l1 [2]), id (l2 [2]) # l1 = [1, 2, 3] l2 = l1l2. append (111) print (l1, l2) # [1, 2, 3,111] [1, 2, 3,111] l1 = [1, 2, 3, [22, 33] l2 = l1 [:] l1 [3]. append (666) print (l2) # [1, 2, 3, [22, 33,666] l1 = [666, 3,] l2 = l1 [:] l1.append) print (l2) # [1, 2, 3]
Iv. Encoding supplement
S = 'Alex 's1 = s. encode ('utf-8') # unicode ---> UTF-8 encoding s3 = s1.decode ('utf-8') # UTF-8 ---> unicode decoding print (s3) # s = 'Alex '# s1 = s. encode ('gbk') # unicode ---> gbk encoding # s3 = s1.decode ('gbk') # gbk ---> unicode decoding # print (s3) # gbk ---> UTF-8 # s = 'Alex '# s1 = s. encode ('gbk') # print (s1) # s2 = s1.decode ('gbk '). encode ('utf-8') # print (s2) s = 'old boys' s1 = s. encode ('gbk') print (s1) # B '\ xc0 \ xcf \ xc4 \ xd0 \ xba \ xa2'
s2 = s1.decode('gbk').encode('utf-8')  #b'\xe8\x80\x81\xe7\x94\xb7\xe5\xad\xa9'print(s2)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.