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)
  • 相关阅读:
    破周三,前不着村后不着店的,只好学pandas了,你该这么学,No.9
    周一不睡觉,也要把pandas groupy 肝完,你该这么学,No.8
    大周末的不休息,继续学习pandas吧,pandas你该这么学,No.7
    链接
    音乐
    术语
    新闻
    我的文章分类
    我的代码规则
    Jenkins 访问特别慢,且不消耗服务器资源
  • 原文地址:https://www.cnblogs.com/gongfan1992/p/7852495.html
Copyright © 2011-2022 走看看