zoukankan      html  css  js  c++  java
  • python—day02_基本数据类型

    1,字符串

    字符串常用功能:
    • 移除空白
    • 分割
    • 长度
    • 索引
    • 切片

    1)移除空白

    """
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    """
    name = " gong fan "
    print("***%s***" % (name))
    print("***%s***" % type(name))
    new_name = name.strip()
    print("***%s***" % (new_name))
    C:Users11519AppDataLocalProgramsPythonPython35python.exe D:/PycharmPro/day01/hello.py
    *** gong fan ***
    ***<class 'str'>***
    ***gong fan***
    
    Process finished with exit code 0

     2)分割

    """
    S.split(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string. If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.
    """

    常用
    >>> u = "www.doiido.com.cn"  
      
    #使用默认分隔符  
    >>> print u.split()  
    ['www.doiido.com.cn']  
      
    #以"."为分隔符  
    >>> print u.split('.')  
    ['www', 'doiido', 'com', 'cn']  
      
    #分割0次  
    >>> print u.split('.',0)  
    ['www.doiido.com.cn']  
      
    #分割一次  
    >>> print u.split('.',1)  
    ['www', 'doiido.com.cn']  
      
    #分割两次  
    >>> print u.split('.',2)  
    ['www', 'doiido', 'com.cn']  
      
    #分割两次,并取序列为1的项  
    >>> print u.split('.',2)[1]  
    doiido  
      
    #分割最多次(实际与不加num参数相同)  
    >>> print u.split('.',-1)  
    ['www', 'doiido', 'com', 'cn']  
      
    #分割两次,并把分割后的三个部分保存到三个文件  
    >>> u1,u2,u3 = u.split('.',2)  
    >>> print u1  
    www  
    >>> print u2  
    doiido  
    >>> print u3  
    com.cn  

    3)长度

    比如a = 'abc'
    print len(a)

    4)索引

    info = 'abcd'
    start = info[0]##开头的第一个字母也就是a
    zhongjian = info[1:2]##中间的字母b
    end = info[-1]##结尾的第一个字母d

    5)切片

    >>> name = "gongfan"
    >>> str = name[0:4]
    >>> print str
    gong
    >>> 

     

    2,列表

    基本操作:

    • 索引
    • 切片
    • 追加
    • 删除
    • 长度
    • 循环
    • 包含

    1)追加

    """ L.append(object) -> None -- append object to end """

    >>> shop_list = ["apple", "water"]
    >>> shop_list.append("meet")
    >>> print shop_list
    ['apple', 'water', 'meet']
    >>> 

    2)删除

    li = [1,2,3,4,5,6]

    # 1.使用del删除对应下标的元素
    del li[2]
    # li = [1,2,4,5,6]

    # 2.使用.pop()删除最后一个元素
    li.pop()
    # li = [1,2,4,5]

    # 3.删除指定值的元素
    li.remove(4)
    # li = [1,2,5]

    3)循环

    fruits = ['banana', 'apple',  'mango']
    for fruit in fruits:        # 第二个实例
       print '当前水果 :', fruit

    4)包涵

    >>> l = ['aa','bcb', 'bcc']  
    >>> b = 'bbc'
    >>> b in l
    False
    >>> c = 'aa'
    >>> c in l
    True
    >>   

    3,元祖

    基本操作:
    • 索引
    • 切片
    • 循环
    • 长度
    • 包含

    4,字典

    • 索引
    • 新增
    • 删除
    • 键、值、键值对
    • 循环
    • 长度

    1)索引

    >>> mydict={'ada':'1111', 'Bill':'2222', 'Candy':'3333'}
    
    >>> mydict['Bill']
    
    '2222'
    
    >>> 

    2)新增

    >>> dict1['a']=1 #第一种  
    >>> dict1  
    {'a': 1}  
    #第二种:setdefault方法  
    >>> dict1.setdefault('b',2)  
    2  
    >>> dict1  
    {'a': 1, 'b': 2} 

    3)删除

    #删除指定键-值对  
    >>> dict1  
    {'a': 1, 'b': 2}  
    >>> del dict1['a'] #也可以用pop方法,dict1.pop('a')  
    >>> dict1  
    {'b': 2}  
    #清空字典  
    >>> dict1.clear()  
    >>> dict1 #字典变为空了  
    {}  
    #删除字典对象  
    >>> del dict1  
    >>> dict1  
    Traceback (most recent call last):  
    File "< interactive input>", line 1, in < module> 
    NameError: name 'dict1' is not defined 

    4)循环

    In [4]: for key, value in d.items():
       ...:     print key, 'corresponds to', value   
     

    5)长度

    len(dict)
  • 相关阅读:
    【Ubuntu】set参数介绍
    【英语学习】第14周翻译练习之日本教育
    【英语学习】第13周翻译练习之自然光照对工作的影响
    【英语学习】第12周翻译练习之出国交流心得
    【英语学习】第11周翻译练习之网上购物
    【英语学习】第9周翻译练习之大学专业
    【英语学习】第10周翻译练习之博物馆是否应当免费开放
    数据结构与算法之选择排序(C++)
    数据结构与算法之冒泡排序(C++)
    机械臂开发之DH模型
  • 原文地址:https://www.cnblogs.com/gongfan1992/p/7852495.html
Copyright © 2011-2022 走看看