python 100day notes(2)

python 100day notes(2)

  1. str

    str2 = 'abc123456'
    
    print(str1.endswith('!'))  # True
    # 将字符串以指定的宽度居中并在两侧填充指定的字符
    print(str1.center(50, '*'))
    # 将字符串以指定的宽度靠右放置左侧填充指定的字符
    print(str1.rjust(50, ' '))
    
    
    # 检查字符串是否由数字构成
    print(str2.isdigit())  # False
    # 检查字符串是否以字母构成
    print(str2.isalpha())  # False
    # 检查字符串是否以数字和字母构成
    print(str2.isalnum())  # True
    str3 = '  [email protected] '
    print(str3)
    # 获得字符串修剪左右两侧空格的拷贝
    print(str3.strip())
  2. list

    def main():
        list1 = [1, 3, 5, 7, 100]
        list1.append(3)
        print(list1)
        # 删除元素 remove 会删除第一个
        list1.remove(3)
        # list1.remove(1234) 会报错
        if 1234 in list1:
            list1.remove(1234)
        del list1[0]
        print(list1)
        # 清空列表元素
        list1.clear()
        print(list1)
    
    
    if __name__ == '__main__':
        main()
    
    """
    output: 
    [1, 3, 5, 7, 100, 3]
    [5, 7, 100, 3]
    []
    """    
  3. tuple

    Here's a question worth exploring, we have a list of such data structure, why do we need such a tuple type it?

    Tuple elements can not be modified, in fact we are in the project, especially in multithreaded environments may prefer to use is not necessary that the same objects (objects on the one hand because the state can not be changed, so to avoid the resulting bugs, simply means that a constant than a variable target object easier to maintain; on the other hand because no one thread can modify the internal state of the same object, a constant objects are automatically thread-safe, this can save processing synchronization overhead. a constant object can be easily shared access). So the conclusion is this: If the elements do not need to add, delete, modify, they can consider using tuples, of course, if a method to return multiple values, use a tuple is a good choice.
    Tuple space created above takes time and is superior to the list. We can use the sys module getsizeof function to check the tuple same elements of memory and how much memory a list of each space, this is very easy to do. We can also use magic instruction in ipython% timeit to analyze the same time create content tuples and lists it takes.

Guess you like

Origin www.cnblogs.com/wangjiale1024/p/11330495.html