zoukankan      html  css  js  c++  java
  • python第二天

    关于变量:python变量只要在内存里存在,就能使用。

    三元运算也叫(三目运算),表达式如下

    name = ‘hehe’ if name = "zhangsan" else "lisi"

    Python所有一切都是对象,是通过类创建的。对象包含类对象指针,找到类从而得到功能(方法)

    得到类型

    type(类型名)

    dir(类型名) 得到类型名,查看类中提供的所有功能

    help(类型名)查看全部功能用法

                

    带有两个下划线的为内置方法

    不带有下划线的为非内置方法

    非内置方法就一种:对象.非内置方法

    内置方法有一种或多种

    n1 = 1

    n2 = 2

    n1 + n2 输出3

    n1._add_(n2)

    int=3

    int("11",base=3)

    int('F',base=16)

    divmod  分页有用哦

    对象能切片因为有index方法

    repr和str区别

    字符串

    str1 = "alex"

    str1 = str("alex")

    class str(basestring):

        """

        str(object='') -> string

        

        Return a nice string representation of the object.

        If the argument is a string, the return value is the same object.

        """

        def capitalize(self):  

            """ 首字母变大写 """

            """

            S.capitalize() -> string

            

            Return a copy of the string S with only its first character

            capitalized.

            """

            return ""

        def center(self, width, fillchar=None):  

            """ 内容居中,width:总长度;fillchar:空白处填充内容,默认无 """

            """

            S.center(width[, fillchar]) -> string

            

            Return S centered in a string of length width. Padding is

            done using the specified fill character (default is a space)

            """

            return ""

        def count(self, sub, start=None, end=None):  

            """ 子序列个数 """

            """

            S.count(sub[, start[, end]]) -> int

            

            Return the number of non-overlapping occurrences of substring sub in

            string S[start:end].  Optional arguments start and end are interpreted

            as in slice notation.

            """

            return 0

    unicode===>UTF-8(编码)

    UTF-8===>unicode(解码)

    str.expandtabs(3):将tab转化为3个空格

    str.find(a) 查找第一个出现a的下游标

    format 需要传列表加*

              传字典需要加**

    name = " i m {0},age {1}"  name.format(11,22)

    li = [2222,3333]

    name = " i m {0},age {1}"  

    name.format(*li)

    name = " i m {ss},age {dd}"  name.format(dd=22,ss=11)

    dic = {'ss': 123,'dd': 456}

    name = " i m {ss},age {dd}"

    name.format(**dic)

    mport string

    intab = "aeiou"

            outtab = "12345"

            trantab = string.maketrans(intab, outtab)

            str = "this is string example....wow!!!"

            print str.translate(trantab, 'xm')



    index与find的区别

    index找不到报错

    find找不到返回-1

    isalpha是否是字母

    isdigit是否是数字

    lower将字符串改为小写

    upper将字符串改为大写

    swapcase大写变小写小写变大写

    replace('a1','b2') 把a1替换为b2

    列表

    count计数查找值出现在列表的次数

    extend 列表扩展

    pop 默认删除最后一个值

    pop(22)删除第一个为22的

    sort :进行排序哦  数字按照数值比较,字母按ascll比较 中文按照Unicode

    判断变量类型

    is list

    is dict

    is float

    ...

    字典

    dic,get('k2','OK')

    字典的key只能为不可改变的值,所以列表没戏了,但是元祖啊、时间啊都可以但是你最好别用

    。推荐用字符、数字、类的实例

    字典循环

    setdefaul(745,['呵呵'])t:存在返回值,不存在创建

    for k in dict :print V[K]

    潜copy只能copy第一层

    深copy得用copy模块

    set做成集合去重

    交集

    并集

    补集

    父级

    子集

  • 相关阅读:
    求转置矩阵问题
    喷水装置
    面试之谈
    迭代任意8种数据类型数组
    Session那点事
    随机验证码
    正则表达式:网页爬虫
    模拟上传服务端
    Properties集合的练习
    用递归算法对指定目录的深度遍历
  • 原文地址:https://www.cnblogs.com/lige-python/p/4943930.html
Copyright © 2011-2022 走看看