zoukankan      html  css  js  c++  java
  • python基础

    1Number类型

    1类型定义

    Integer,Long integer,Boolean,Double-precision float,Complex number

    2操作符

    比特操作符

    ~A    按二进制取反操作

    A&B 并操作

    A|B  或操作

    A^B 异或操作

    A>>B 按比特位右移

    A<<B 按比特位左移

    算数操作符 :略

    3内置函数

    1通用函数

    cmp(A,B)比较AB大小,前者小返回-1,前者大返回1,两者相同返回0

    str(A)将数字转换为可显示的字符串

    type(A)返回参数的类型对象

    bool(A)将参数转换为布尔类型

    int(A)将参数转换为整数类型,以十进制表示

    long(A)将参数转换为长整形,以十进制表示

    float(A)将参数转换为浮点类型

    complex(A)将参数转换为复数类型

    2数值类型特定函数

    abs(A)取绝对值

    coerce(A,B)将AB转化为同一个类型,并生成一个元组

    divmod(A,B)除模操作:生成一个元组,形式为(A/B,A%B)

    pow(A,B)幂操作:A的B次方

    round(A) 返回参数的四舍五入结果

    hex(A) 将A转换为用十六进制的字符串

    oct(A) 将A转换为用八进制表示的字符串

    chr(A)将A转化为ASCII字符,要求0<=A<=255

    ord(A) chr(A)的反函数

    2String类型

    1基本使用

    python中字符串分两种:一种是普通字符串,另一种是Unicode字符串。用引号声明的字符串是普通字符串;而在因号之前加上字母u时,python会将其解释为Unicode字符串。

    2字符串格式化

    char = 'abcd'
    char = u'abcd'

    3内置函数

    详细参考:http://blog.luoyuanhang.com/2016/03/02/Python-%E5%AD%97%E7%AC%A6%E4%B8%B2-String-%E5%86%85%E5%BB%BA%E5%87%BD%E6%95%B0%E5%A4%A7%E5%85%A8-1/

    3Tuple类型

    tuple用圆括号表示,不同元素之间用逗号隔开,在初始化之后不能修改。

    4List类型

    内置函数

    append(obj)在列表尾部添加一个对象

    count(obj)计算对象在列表中出现的次数

    extend(seq)将序列seq的内容添加到列表中

    index(obj,i=0,j=len(list))计算对象obj在列表中的位置

    insert(index,obj)把对象插入到列表index指定的位置

    pop(index=-1)读取并删除index位置的对象,默认为最后一个对象

    remove(obj)从列表中删除对象obj

    reserve()获取反向列表

    list.sort(func=None,key=None,reservw=False)以指定方式排序列表中的成员

    5Dictionary类型

    1类型定义

    Dictionary是python中唯一表示映射关系的类,所以其有独特的定义和操作方式。

    2内置函数

    clear()清除字典中所有的键值对

    copy()复制字典的一个副本

    fromkeys(seq,val=None)用seq中的元素为键创建字典,所有键的值都设为val,val默认为None。

    get(key,default=None)读取字典中的键key,返回该键值;如果找不到返回default设置的值。

    has_key(key)判断key是否存在

     

    captalize() 函数

    功能

    将一个字符串的第一个字母大写

    用法

    str.captalize()

    参数

    返回值

    string

    示例代码

    1
    2
    3
    str = "hello world!"

    print "str.capitalize(): ", str.capitalize()

    运行结果

    1
    tr.capitalize():  Hello world!

    center(width, fillchar) 函数

    将字符串居中,居中后的长度为 width

    功能

    将字符串居中,居中后的长度为 width

    用法

    str.center(width[, fillchar])

    参数

    •  表示字符串总长度
    • fillchar: 使字符串居中所填充的字符,默认为空格

    返回值

    返回填充字符后的字符串

    示例代码

    1
    2
    3
    4
    str = "hello world!"

    print "str.center(20): ", str.center(20)
    print "str.center(20,'-'): ", str.center(20,'-')

    运行结果

    1
    2
    tr.center(20):      hello world!
    str.center(20,'-'): ----hello world!----

    count(str, start=0, end=len(string)) 函数

    功能

    返回该字符串中出现某字符串序列(或字符)的次数

    用法

    str.count(sub, start=0, end=len(string))

    参数

    • sub: 被查找的字符串序列
    • start: 开始查找的索引位置,默认为字符串开始
    • end: 结束查找的索引位置,默认为字符串结束

    返回值

    被查找的序列在字符串的查找位置中出现的次数

    示例代码

    1
    2
    3
    4
    5
    6
    7
    str = "hello world! hello world!"

    sub = "o"
    print "str.count(sub): ", str.count(sub)

    sub = "hello"
    print "str.count(sub, 5) ", str.count(sub, 5)

    运行结果

    1
    2
    str.count(sub):  4
    str.count(sub, 5) 1

    decode(encoding=’UTF-8’,errors=’strict’) 函数 & encode(encoding=’UTF-8’,errors=’strict’)

    功能

    使用特定编码将字符串解码(decode)/编码(encode)

    用法

    str.decode(encoding='UTF-8',errors='strict')

    str.encode(encoding='UTF-8',errors='strict')

    参数

    • encoding: 使用的编码格式
    • errors: 设置不同的错误处理方法,其他选项有 ignorereplacexmlcharrefreplacebackslashreplace

    返回值

    编码/解码后的字符串

    示例代码

    1
    2
    3
    4
    5
    str = "hello world!"

    str = str.encode('base64', 'strict')
    print "Encoded str: ", str
    print "Decoded str: ", str.decode('base64')

    运行结果

    1
    2
    3
    Encoded str:  aGVsbG8gd29ybGQh

    Decoded str: hello world!

    endswith(suffix, start=0, end=len(string)) 函数

    功能

    判断字符串是否是以某字符串结尾的

    用法

    str.endswith(suffix, start=0, end=len(string))

    参数

    • suffix: 被查找的字符串
    • start: 字符串查找的起始位置,默认为字符串起始位置
    • end: 字符串查找的结束位置,默认为字符串结束位置

    返回值

    如果字符串是以 suffix 结尾的返回 True, 否则返回 False

    示例代码

    1
    2
    3
    4
    5
    6
    7
    8
    str = "hello world!"

    suffix = "world!"
    print str.endswith(suffix)

    suffix = "llo"
    print str.endswith(suffix,0,4)
    print str.endswith(suffix,0,5)

    运行结果

    1
    2
    3
    True
    False
    True

    expandstabs(tabsize=8) 函数

    功能

    提供自定义 tab(/t) 长度的方法,默认为8

    用法

    str.expandtabs(tabsize=8)

    参数

    • tabsize: 表示自定义 tab 的长度

    返回值

    string

    示例代码

    1
    2
    3
    4
    5
    str = "hello	world!"

    print "Original str: " + str
    print "Defalut expanded tab: " + str.expandtabs();
    print "Double expanded tab: " + str.expandtabs(16)

    运行结果

    1
    2
    3
    Original str: hello	world!
    Defalut expanded tab: hello world!
    Double expanded tab: hello world!

    find(str, start=0, end=len(string)) 函数

    功能

    在字符串的某指定位置查找某字符串

    用法

    str.find(str, start=0, end=len(string))

    参数

    • str: 被查找的子字符串
    • start: 查找的起始位置,默认为字符串起始位置
    • end: 查找的结束位置,默认为字符串结束位置

    返回值

    如果查找到,返回该子字符串的索引;未查找到,返回-1

    示例代码

    1
    2
    3
    4
    5
    6
    str = "hello world!"

    str1 = "wo"

    print str.find(str1)
    print str.find(str1, 8)

    运行结果

    1
    2
    6
    -1

    index(str, start=0, end=len(string)) 函数

    功能

    功能上与 find() 相同,只是在未找到子字符串是抛出异常

    用法

    str.index(str, start=0, end=len(string))

    参数

    同 find()

    返回值

    如果查找到,返回该子字符串的索引;未查找到,抛出异常

    示例代码

    1
    2
    3
    4
    5
    6
    str = "hello world!"

    str1 = "wo"

    print str.index(str1)
    print str.index(str1, 8)

    运行结果

    1
    2
    3
    4
    5
    6
    Traceback (most recent call last):
    File "teststrmethods.py", line 6, in <module>
    print str.index(str1, 8)
    ValueError: substring not found

    isalnum() 函数

    功能

    判断该字符串是否只是字母数字组合

    用法

    str.isalnum()

    参数

    返回值

    如果该字符串是字母数字组合,返回 True,否则返回 False

    示例代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    str = "helloworld"
    print str.isalnum()

    str = "hello world"
    print str.isalnum()

    str = "hello123"
    print str.isalnum()

    str = "hello123!"
    print str.isalnum()

    运行结果

    1
    2
    3
    4
    True
    False
    True
    False

    isalpha() 函数

    功能

    判断该字符串是否是字母组合

    用法

    str.isalpha()

    参数

    返回值

    如果该字符串是字母组合,返回 True,否则返回 False

    示例代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    str = "helloworld"
    print str.isalpha()

    str = "hello world"
    print str.isalpha()

    str = "hello123"
    print str.isalpha()

    str = "hello123!"
    print str.isalpha()

    运行结果

    1
    2
    3
    4
    True
    False
    False
    False

    isdigit() 函数

    功能

    判断该字符串是否只包含数字

    用法

    str.isdigit()

    参数

    返回值

    如果该字符串只包含数字,则返回 True,否则返回 False

    示例代码

    1
    2
    3
    4
    5
    str = "hello123"
    print str.isdigit()

    str = "123456"
    print str.isdigit()

    运行结果

    1
    2
    False
    True

    islower() 函数

    功能

    判断该字符串中是否只是小写字母

    用法

    str.islower()

    参数

    返回值

    如果该字符串中只是小写字母,返回True,否则返回False

    示例代码

    1
    2
    3
    4
    5
    str = "hello wolrd!"
    print str.islower()

    str = "Hello Wolrd!"
    print str.islower()

    运行结果

    1
    2
    True
    False

    isspace() 函数

    功能

    判断该字符串是否只包含空格

    用法

    str.isspace()

    参数

    返回值

    如果该字符串只包含空格,返回True,否则返回False

    示例代码

    1
    2
    3
    4
    5
    str = "    "
    print str.isspace()

    str = "Hello Wolrd!"
    print str.isspace()

    运行结果

    1
    2
    True
    False

    istitle() 函数

    功能

    检查该字符串中的单词是否首字母都大写

    用法

    str.istitle()

    参数

    返回值

    如果该字符串中的单词首字母都大写了,返回True,否则返回False

    示例代码

    1
    2
    3
    4
    5
    str = "Hello world!"
    print str.istitle()

    str = "Hello Wolrd!"
    print str.istitle()

    运行结果

    1
    2
    False
    True

    isupper() 函数

    功能

    判断该字符串中的字母是否都是大写

    用法

    str.isupper()

    参数

    返回值

    如果该字符串中的字母都是大写,返回True,否则返回False

    示例代码

    1
    2
    3
    4
    5
    str = "Hello world!"
    print str.isupper()

    str = "HELLO WORLD!"
    print str.isupper()

    运行结果

    1
    2
    False
    True

    join(seq) 函数

    功能

    用该字符串连接某字符序列(seq)

    用法

    str.join(sequence)

    参数

    • sequence: 被连接的字符序列

    返回值

    返回连接之后的字符串

    示例代码

    1
    2
    3
    4
    str = "-"
    sequence = ("hello", "world", "everyone", "!")

    print str.join(sequence)

    运行结果

    1
    hello-world-everyone-!

    len(string) 函数

    功能

    得到该字符串的长度

    用法

    len(str)

    参数

    返回值

    返回该字符串长度

    示例代码

    1
    2
    3
    str = "Hello world!"

    print "the length of str: ", len(str)

    运行结果

    1
    the length of str:  12

    ljust(width, fillchar=’ ‘)函数

    功能

    在字符串的右边填充字符使得字符串达到指定长度

    用法

    str.ljust(width, fillchar=' ')

    参数

    •  填充后的目标长度
    • fillchar: 用于填充的字符,默认为空格

    返回值

    返回填充后的字符串

    示例代码

    1
    2
    3
    4
    str = "Hello world"

    print str.ljust(15)
    print str.ljust(15,'!')

    运行结果

    1
    2
    Hello world
    Hello world!!!!
  • 相关阅读:
    HTML链接/实施CSS的三种方法
    XML之Well-Formed文档规则
    【摘】SVN提交与版本冲突
    Web开发之404小结
    TCP 连接的要点
    [转] Epoll 相对Poll和Select的优点
    [转] 剖析 epoll ET/LT 触发方式的性能差异误解(定性分析)
    GDB调试技巧
    [转] 关于c++的头文件依赖
    [转] Linux中gcc,g++常用编译选项
  • 原文地址:https://www.cnblogs.com/leesen934/p/7787253.html
Copyright © 2011-2022 走看看