Python journey - Data Types

1. Overview

Today to share with you is the data type, data type, degree of importance in Python is like as important than the Chinese pinyin us.

2. Data Types

1. Plastic

  • Plastic are like mathematics, 2, 3, is represented by int.

    a = 1 # 这就是一个整形
    print(type(a)) # 可以用type方法来检测数据类型

2. Boolean

  • A data type that is used to represent the Boolean value of true or false, with bool represented only True and False

3. String

  • Like a string of Chinese characters, as expressed by str, the string is to write the code of the most common, python memory string is in accordance with: unicode code storage. For strings are immutable

  • String There are many ways, this is mainly to say some commonly used methods

    1. Uppercase: upper

      v = 'ALEX'
      v1 = v.upper()
      print(v1)
      v2 = v.isupper() # 判断是否全部是大写
      print(v2)
    2. Lower case: lower

      v = 'alex'
      v1 = v.lower()
      print(v1)
      v2 = v.islower() # 判断是否全部是小写
      print(v2)
      
      
      ############ 了解即可
      v = 'ß'
      # 将字符串变小写(更牛逼)
      v1 = v.casefold() # 对于其他国家的语言也可以进行小写转化 
      print(v1) # ss
      v2 = v.lower()
      print(v2)
    3. Determine whether the numbers: isdecimal

      v = '1'
      # v = '二'
      # v = '②'
      v1 = v.isdigit()  # '1'-> True; '二'-> False; '②' --> True
      v2 = v.isdecimal() # '1'-> True; '二'-> False; '②' --> False
      v3 = v.isnumeric() # '1'-> True; '二'-> True; '②' --> True
      print(v1,v2,v3)
      # 以后推荐用 isdecimal 判断是否是 10进制的数。
      
      # ############## 应用 ##############
      
      v = ['alex','eric','tony']
      
      for i in v:
          print(i)
      
      num = input('请输入序号:')
      if num.isdecimal():
          num = int(num)
          print(v[num])
      else:
          print('你输入的不是数字')
    4. To Blank + \ t + \ n + a specified string: strip

      v1 = "alex "
      print(v1.strip())
      
      v2 = "alex\t"
      print(v2.strip())
      
      v3 = "alex\n"
      print(v3.strip())
      
      v1 = "alexa"
      print(v1.strip('al'))
    5. Replace: replace

      name = 'alex'
      new_name = name.replace('a','T')
      print(new_name)
    6. Beginning: starswith

      name = 'alex'
      new_name = name.startswith('a')
      print(new_name)
    7. End: endswith

      name = 'alex'
      new_name = name.endswith('x')
      print(new_name)
    8. encode: the string into binary

      name = '你好'
      new_name = name.encode('utf-8')
      print(new_name)
    9. format: String Format

      v1 = "我叫{0},年龄{1}".format('郭睿杰',23)
      print(v1)
      
    10. join: Join

      name = 'alex'
      new_name = "|".join(name)
      print(new_name)
    11. split: split

      name = 'alex'
      new_name = name.split("l")
      print(new_name)
      
    12. Other [optional]

4. List

  • Like a list of containers, which it can store many types of data, with list to represent. And inside the content can be modified

  • A list of commonly used methods:

    1. Add: append

      v1 = [1,2,3,4]
      v1.append(5)
      print(v1)
    2. Insert: insert

      v1 = [1,2,3,4]
      v1.insert(0,1)
      print(v1)
    3. Delete: remove

      v1 = [1,2,3,4]
      v2 = (5,6,7,8)
      v1.remove(1)
      print(v1)
    4. Delete: pop

      v1 = [1,2,3,4]
      v2 = (5,6,7,8)
      v1.pop(1)
      print(v1)
    5. Delete: clear

      v1 = [1,2,3,4]
      v2 = (5,6,7,8)
      v1.clear()
      print(v1)
    6. Adding another list (tuple) to the first element in the list: extend

      v1 = [1,2,3,4]
      v2 = (5,6,7,8)
      v1.extend(v2)
      print(v1)
    7. Reverse: reverse

      v1 = [1,2,3,4]
      v2 = (5,6,7,8)
      v1.reverse()
      print(v1)
    8. Sort: sort

      v1 = [1,2,3,4]
      v2 = (5,6,7,8)
      v1.sort(reverse=False)
      print(v1)

5-tuple

  • Is also a container, the content inside can not be amended. Expressed with tuple
  • General Procedure tuple, due to its nature can not be modified, it has some basic method is a method:
    1. Take length: len
    2. index
    3. slice
    4. Steps

6. Dictionary

  • The same type of container is a dictionary data type, represented by dict, it lists and tuples difference is that it has its own key-value pairs, i.e. keys and values, the same dictionary key is not allowed to modify , but the value can be modified

  • Dictionary of commonly used methods:

    1. Take all of the keys: keys

      v1 = {'k1':1,'k2':2}
      for i in v1.keys():
          print(i)
    2. Take all values: values

      v1 = {'k1':1,'k2':2}
      for i in v1.values():
          print(i)
    3. Take all of the key-value pairs: items

      v1 = {'k1':1,'k2':2}
      for a,b in v1.items():
          print(a,b)
    4. Value: get

      v1 = {'k1':1,'k2':2}
      x = v1.get('k1')
      print(x)
    5. Delete: pop

      v1 = {'k1':1,'k2':2}
      v1.pop('k1')
      print(v1)
    6. Update: update

      v1 = {'k1':1,'k2':2}
      v1.update({'k3':3})
      print(v1)
    7. Ordered dictionary

      from collections import OrderedDict
      
      info = OrderedDict()
      info['k1'] = 123
      info['k2'] = 456
      
      print(info.keys())
      print(info.values())
      print(info.items())

7. collection

  • As a set of data type container type, which is characterized not repeated. Is represented by set

  • Common methods for the collection

    1. Add: add

      v1 = {1,2,3,4}
      v1.add(666)
      print(v1)
      
    2. Delete: discard

      v1 = {1,2,3,4}
      v1.discard(1)
      print(v1)
      
    3. Bulk add: update

      v1 = {1,2,3,4}
      v1.update({7},{8},{9})
      print(v1)
    4. Take the intersection: intersection

      v1 = {1,2,3,4}
      v2 = {3,4,5,6}
      x = v1.intersection(v2)
      print(x)
    5. Take the union: union

      v1 = {1,2,3,4}
      v2 = {3,4,5,6}
      x = v1.union(v2)
      print(x)
    6. Set taking the difference: difference

      v1 = {1,2,3,4}
      v2 = {3,4,5,6}
      x = v1.difference(v2)
      print(x)

8.None

  • This data type is like surface means, means is empty.

9. summary

  • Today to share some of our commonly used data types, as well as some of their common method, welcome to discuss!

Guess you like

Origin www.cnblogs.com/guoruijie/p/11019887.html