Data Types and Methods

Keywords: Python encoding

1. None

2. Integer (int)

3. Boolean Types (bool)

4. String (str)

  1. upper/lower: letter case conversion

    • Upper: convert lower case to upper case

    • lower: uppercase to lowercase

v1 = 'alex'
v2 = v1.upper()
print(v2)          #'ALEX'

v3 = v2.lower()
print(v3)          #'alex'
  1. isdigit: Determines whether a string is a number
v1 = 'alex'
date1 = v1.isdigit()
print(date1)        #False

v2 = '666'
date2 = v2.isdigit()
print(date2)        #True
  1. strip/rstrip/lstrip: removing spaces
    • strip: removes spaces on the left and right sides
    • rstrip: Remove the space on the right
    • lstrip: Remove the space on the left
v = ' alex '
date1 = v1.strip()
print(date1)         #'alex'

date2 = v.rstrip()
print(date2)         #' alex'

date3 = v.lstrip()
print(date3)         #'alex '
  1. Replace: replace
v = 'Today I'm going to have fried rice, fried rice and fried rice'
date1 = v.replace('Fried rice','Roasted Duck')      #Replace all fried rice
print(date1)                         #'Today I'm eating Roast Duck Roast Duck'

date2 = v.replace('Fried rice','Roasted Duck',1)     #Replace only the first fried rice
print(date2)                           #'Today I'm going to have roast duck, fried rice and fried Rice'
  1. split/rsplit: cut
    • split: cut left to right
    • rsplit: cut right to left
v = 'Today I'm going to have fried rice, roast chicken, roast duck'
date1 = v.split(',')       #Cut all the stones from left to right
print(date1)                #['Today I'm having fried rice','roast chicken','roast duck']

date2 = v.split(',',1)     #Cut the first stop from left to right
print(date2)                #['I'm having fried rice today','roast chicken, roast duck']

date2 = v.rsplit(',',1)     #Cut the first stop from right to left
print(date2)                 #['Today I'm having fried rice, roast chicken','roast duck']
  1. startswith/endswith: determine if there is any beginning/end
    • startswith: Determine if something has started
    • endswith: determine if there is an end
v = 'alex'
date1 = v.startswith('al')
print(date1)         #True

date2 = v.endswith('ex')
print(date2)         #True
  1. format: string formatting
date = 'His name is{0},This year{1}'.format('Zhang San',22)
print(date)          #'His name is Zhang San, 22'
  1. encode: Convert unicode storage to other encoding storage
name = 'Zhang San'                    #After the interpreter reads into memory, store it in unicode
date1 = name.encode('utf-8')     #Convert unicode storage to utf-8 storage
date2 = name.encode('gbk')       #Convert unicode storage to gbk storage
  1. join: Loop each element and add connectors between them
v = 'alex'
date = "_".join(v)     #Loop through each element and add''between them
print(date)            #'a_l_e_x'

5. List

  1. Append: append
v = ['Zhang San','Li Si','King Five']
v.append(666)
print(v)               #['Zhang San','Li Si','Wang Wu', 666]
  1. Insert: insert
v = ['Zhang San','Li Si','King Five']
v.insert(1,666)
print(v)               #['Zhang San', 666,'Li Si','Wang Wu']

3.remove: Delete (specified element)

v = ['Zhang San','Li Si','King Five']
v.remove('Li Si')
print(v)               #['Zhang San','Wang Wu']
  1. pop: delete (element at index position)
v = ['Zhang San','Li Si','King Five']
v.pop(o)
print(v)               #['Li Si','Wang Wu']
  1. clear: delete everything
v = ['Zhang San','Li Si','King Five']
v.clear(o)
print(v)              #[]
  1. extend: circular join
v = ['Zhang San','Li Si','King Five']
v1 = [1,2,3]
v.extend(v1)          #Append element loop in v1 to v
print(v)              #['Zhang San','Li Si','Wang Wu', 1,2,3]
  1. Reverse: reverse
v = [1,3,6,4,2,5]
v.reverse()
print(v)             #[5,2,4,6,3,1]
  1. Sort: sort
v = [1,3,6,4,2,5]
v.sort(reverse = False)       #Sort from smallest to largest [default:.sort()]
print(v)                      #[1,2,3,4,5,6]

v.sort(reverse = True)        #Sort from large to small
print(v)                      #[6,5,4,3,2,1]

6. Tuple

7. Dictionary (dict)

  1. Keys: Get all keys in the dictionary
info = {'name':'Zhang San','age':22,'gender':'male'}
for i in info.keys():
    print(i)                 
"""
'name'
'age'
'gender'
"""
  1. Values: Get all the values in the dictionary
info = {'name':'Zhang San','age':22,'gender':'male'}
for i in info.values():
    print(i)  
"""
'Zhang San'
22
'male'
"""
  1. items: Get all key-value pairs in the dictionary
info = {'name':'Zhang San','age':22,'gender':'male'}
for a,b in info.items():
    print(a,b) 
"""
'name','Zhang San'
'age',22
'gender','male'
"""
  1. get: index value
info = {'name':'Zhang San','age':22,'gender':'male'}
v1 = info.get('name')
print(v1)                     #'Zhang San', equivalent to v1 = info['name']

v2 = info.get('hobby')
print(v2)                     #None

v3 = info.get('hobby',666)    #Output 666 if the'hobby'key does not exist
print(v3)                     #666
  1. pop: delete (element at index position)
info = {'name':'Zhang San','age':22,'gender':'male'}
v = info.pop('name')         #Delete'name':'Zhang San'key-value pair and assign'Zhang San' to v1
print(v)                     #'Zhang San'
print(info)                  #{'age':22,'gender':'man'}
  1. update: add (modify) in bulk
    • Add if not present, modify if present
info = {'name':'Zhang San'}
info.update({'age':22,'gender':'male','name':'Li Si'})       #Add'age'and'gender' and modify the value corresponding to'name'
print(info)                   # {'name':'Li Si','age':22,'gender':'man'}

8. Sets

  1. Add: add (no more add existing)
v = {1,2,3}
v.add(4)                 #Add 4
v.add(3)                 #3 is already there, don't add any more
print(v)                 #{1,2,3,4}   
  1. discard: delete
v = {1,2,3}
v.discard(2)             #Delete 2
print(v)                 #{1,3}
  1. update: add in bulk
v = {1,2,3}
v.update({4,5,6})        #Add 4,5,6
print(v)                 #{1,2,3,4,5,6}
  1. Intersection: intersection
v = {1,2,3}
date = v.intersection({1,3,4})      #Take the intersection of v and {1,3,4}
print(date)              #{1,3}
  1. union: union
v = {1,2,3}
date = v.union({1,3,4})             #Take the union of v and {1,3,4}
print(date)              #{1,2,3,4}
  1. Difference: difference set
v1 = {1,2,3}
v2 = {1,3,4}
date1 = v1.difference(v2)           #Take the difference between v1 and v2, which is not found in V2 in v1
print(date1)             #{2}

date2 = v2.difference(v1)           #Take the difference between v2 and v1, there is no V1 in v2
print(date2)             #{4}
  1. symmetric_difference: symmetric difference set
v = {1,2,3}
date = v.symmetric_difference({1,3,4})        #Take the symmetric difference set of v and {1,3,4}
print(date)              #{2,4}

Posted by rigbyae on Fri, 29 Nov 2019 21:39:04 -0800