zoukankan      html  css  js  c++  java
  • 【python-基础】基础回顾

    python常见函数

    数学函数

    pow(a, b) 求a的b次幂

    abs(a) 求a的绝对值

    round(a) 求距离a最近的整数

    sqrt(a) 求开发

    input('please input your number: ') 终端输入函数

    raw_input('please input your number: ') 终端输入函数

    max() 返回序列中最大值

    min() 返回序列中最小值

    len() 返回序列的长度

    python序列 

    索引

    索引到第一个元素str[0]

    >>> str

    'hi,today is Sunday!'

    >>> str[0]

    'h'

    索引到最后一个元素str[-1]

    >>> str[-1]

    '!'

    索引到倒数第二个元素str[-2]

    >>> str[-2]

    'y'

     

    分片

    list[2,4]表示取出2<=index<4之间的元素

    >>> list=['a','b','c','1','2','3']

    >>> list[2:4]

    ['c', '1']

     

    步长

    默认步长值为1,可以在第三个参数中指定numbers[0:9:2]步长为2

    >>> numbers

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    >>> numbers[0:9:1]

    [0, 1, 2, 3, 4, 5, 6, 7, 8]

    >>> numbers[0:9:2]

    [0, 2, 4, 6, 8]

    >>> numbers[0:9:4]

     

    序列相加

    和序列相乘一样,得到了拼接效果

    >>> list_1=[0,2,4]

    >>> list_2=[1,3,5]

    >>> list_1 + list_2

    [0, 2, 4, 1, 3, 5]

     

    序列相乘

    和序列相加一样,得到了拼接效果

    >>> list_1

    [0, 2, 4]

    >>> list_1 * 3

    [0, 2, 4, 0, 2, 4, 0, 2, 4]

     

    删除元素

    >>> numbers

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    >>> del numbers[0]

    >>> numbers

    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    >>> 

    >>> del numbers[0:3]

    >>> numbers

    [4, 5, 6, 7, 8, 9]

     

    列表的方法

    count

    统计list中某个元素出现的个数 

    >>> n=['a', 'b', 'c', 'ac', 'bc', 'cd']

    >>> n

    ['a', 'b', 'c', 'ac', 'bc', 'cd']

    >>> n.count('a')

    1

    >>> n.count('ac')

    1

     

    append 

    在list末尾追加元素

    >>> n

    ['a', 'b', 'c', 'ac', 'bc', 'cd']

    >>> n.append('end')

    >>> n

    ['a', 'b', 'c', 'ac', 'bc', 'cd', 'end']

     

    extend

    在末尾追加另一个列表的多个元素 

    >>> n

    ['a', 'b', 'c', 'ac', 'bc', 'cd', 'end']

    >>> 

    >>> n1 = ['extra']

    >>> n.extend(n1)

    >>> n

    ['a', 'b', 'c', 'ac', 'bc', 'cd', 'end', 'extra']

     

    insert 

    在列表中插入元素,insert(i, node)表示在索引为i的位置,插入元素node

    >>> n

    ['a', 'b', 'c', 'ac', 'bc', 'cd', 'end', 'extra']

    >>> n.insert(0, 'first')

    >>> n

    ['first', 'a', 'b', 'c', 'ac', 'bc', 'cd', 'end', 'extra']

     

    pop

    移出最尾元素的值,此时列表发生变化

    >>> n

    ['first', 'a', 'b', 'c', 'ac', 'bc', 'cd', 'end', 'extra']

    >>> 

    >>> n.pop()

    'extra'

    >>> n

    ['first', 'a', 'b', 'c', 'ac', 'bc', 'cd', 'end']

     

    remove

    移除列表中某个元素

    >>> n

    ['first', 'a', 'b', 'c', 'ac', 'bc', 'cd', 'end']

    >>> n.remove('a')

    >>> n

    ['first', 'b', 'c', 'ac', 'bc', 'cd', 'end']

     

    reverse

    将列表中的元素按照索引顺序反向排列

    >>> n

    ['first', 'b', 'c', 'ac', 'bc', 'cd', 'end']

    >>> n.reverse()

    >>> n

    ['end', 'cd', 'bc', 'ac', 'c', 'b', 'first']

     

    sort

    将列表中的元素按照值的大小进行排列

    >>> numbers=[1, 5, 9, 2, 6, 8, 0]

    >>> numbers

    [1, 5, 9, 2, 6, 8, 0]

    >>> numbers.sort()

    >>> numbers

    [0, 1, 2, 5, 6, 8, 9]

     

    比较2个数值,com(a, b) 如果a>b则返回1, 如果a=b则返回0,如果a<b则返回-1。

    >>> cmp(100, 50)

    1

    >>> cmp(100, 100)

    0

    >>> cmp(100, 200)

    -1

     

    字符串

    字符串的格式化输出

    对于打印数据时,注意顺序:

    %表示格式化开始

    -表示靠左排列

    +并非表示靠右排列,而是显示正负号

    .前面是列宽度,列宽度前面如果还有内容,表示列不够的时候,用指定内容填充

    .后面是小数点显示几位,例如.3f表示小数点后显示3位

    举例如下:

    print "number is %-10f" % pi
    print "number is %10f" % pi
    print "number is %+10f" % pi
    print "number is %010f" % pi
    print "number is %.3f" % pi

     运行结果:

    number is 3.141593
    number is 3.141593
    number is +3.141593
    number is 003.141593
    number is 3.142

    对于打印各类型数据时:

    %d 打印十进制

    %o 打印八进制

    %x 打印十六进制

    %f 打印浮点型

    print "%s and %s are all in [a~z]" % ('a', 'b')
    print "price is %d" % 10
    print "pi is %f" % 3.1415926
    print "number 100 equals %d / %o / %x " % (100, 100, 100)

    字符串方法

    find

    找到指定字符,并返回最左端的索引,如果找不到返回-1

    >>> str='hello,my name is a'

    >>> str.find('no')

    -1

    >>> str.find('name')

    9

     

    join

    是split的逆方法

    >>> list

    ['a', 'b', 'c', 'd']

    >>> c='+'

    >>> c.join(list)

    'a+b+c+d'

    >>> d=''

    >>> d.join(list)

    'abcd'

     

    lower

    把字符串的大写转为小写

    >>> str

    'Hello, My Name Is John'

    >>> str.lower()

    'hello, my name is john'

    >>> str

    'Hello, My Name Is John'

     

    replace

    字符串.replace(源,目标)

    >>> str

    'Hello, My Name Is John'

    >>> str.replace('e', 'xxx')

    'Hxxxllo, My Namxxx Is John'

    split 

    将字符串按照指定的字符割裂成list元素

    >>> str

    'Hello, My Name Is John'

    >>> str.split(' ')

    ['Hello,', 'My', 'Name', 'Is', 'John']

    strip

    将字符串头尾的空格删掉

    >>> string

    '    hi,abc    '

    >>> string.strip()

    'hi,abc'

    当然strip也可以自由自定,删掉头尾的哪个字符串

    >>> string

    'hooooooh'

    >>> string.strip('h')

    'oooooo'

     

  • 相关阅读:
    日期时间格式或封装,已经互相转换,抽出来日后方便自己开发,之前用作在mpvue的框架开发小程序中
    微信小程序授权方法全能,当用户拒绝或者首次进来的,都可以弹起授权提示,主要是用wx.getSetting,还有wx.authorize,最后的wx.openSetting
    JavaScript封装自己的一个弹窗,是双按钮的,比较简单一些 ,其中引用了jQuery来写的方法,最后暴露出去,有更好的建议欢迎评论 。。。。
    最近学习mpvue框架开发微信小程序,把wepy框架的项目实现到mpvue中,知道其中的一些两者之间的区别
    JavaScript中历史搜索记录的实现,在h5页面,引用jQuery写法,使用localStorage存储
    vue中实现双向数据绑定原理,使用了Object.defineproperty()方法,方法简单
    好好学习vue中 写了一些demo 希望自己能提升多一点 vue中实现父子组件之间的通信 相比我的上一篇非父子组件会简单些
    Java中Scanner类在nextInt()后无法输入nextLine()的问题
    mybatis中#{}和${}的区别及order by的sql注入问题
    Intellij常用设置及快捷键
  • 原文地址:https://www.cnblogs.com/liurong07/p/13173236.html
Copyright © 2011-2022 走看看