zoukankan      html  css  js  c++  java
  • python基础-对象

     

    1. 对象:一组数据和操作数据方法的集合

    >>> class Person(object):
    ...     def __init__(self,name):
    ...         self.name=name
    ...     def get_name(self):
    ...         return self.name
    ...
    >>> p=Person("Jordan")
    >>> p.name
    'Jordan'
    >>> p.get_name()
    'Jordan'
    >>> dir(p)
    ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribut
    e__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_e
    x__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_
    _weakref__', 'get_name', 'name']

     

    2. 保留字

    代码换行:

    >>> print "aaa

    ...     yyy"

    输出结果:aaa     yyy

     

    括号内的代码可随意换行:

    >>> sum([1,2,3])

    6

    >>> sum([1,2

    ...     ,3,4])

    10

     

    3. 缩进:四个空格

    4. 注释:一行用#;多行用’’’

    5. 编码规范:避免劣质代码;适当添加注释

    拓展->文档字符串:

    >>> def sum(a,b):
    ...     '''此函数用于两个数字的加法'''
    ...     return a+b
    ...
    >>> sum.__doc__
    'xb4xcbxbaxafxcaxfdxd3xc3xd3xdaxc1xbdxb8xf6xcaxfdxd7xd6xb5xc
    4xbcxd3xb7xa8'
    >>> print sum.__doc__
    此函数用于两个数字的加法
    >>> help(sum)
    Help on function sum in module __main__:

    sum(a, b)
        此函数用于两个数字的加法

    6. 编写函数的原则:

    7. 编码:各进制之间要会手工转换

    二进制:逢二进一   1:1  2:10  3:11  4:100  5:101

    二进制转成十进制:

    1111: 1111=1*2的3次方+1*2的2次方+1*2的1次方+1*2的0次方

    1000: 1*2的3次方+0*2的2次方+0*2的1次方+0*2的0次方

     

    八进制:逢八进一   1:1  2:2   …… 10:  12

     

    十六进制:逢十六进一    130: 16*8=128    82

     a=10  b=11  c=12  d=13   e=14   f=15

     

    其他进制函数转换为十进制:

    >>> int('1100',base=2)
    12
    >>> int('17',base=8)
    15
    >>> 1*8的一次方+7*8的0次方

    >>> int('ff',base=16)
    255
    >>> 15*16的一次方+15*16的0次方

    转码函数:

    >>> int('10101',base=2)   十进制转二进制

    21

    >>> int('17',base=8)  十进制转八进制

    15

    >>> int('10',base=16)  十进制转十六进制

    16

    >>> bin(10)   把十进制转成二进制

    '0b1010'

    >>> oct(10)   把十进制转成八进制

    '012'

    >>> hex(255)  把十进制转成十六进制

    '0xff'

     

    8. 原码、反码和补码

    反码:解决负数加法的问题

    补码:解决﹣0的问题

    9. 运算符和表达式

    乘方:x**y 

     eg:>>> 8**2

    64

    >>> pow(2,4)

    16

    开方:

    >>> import math

    >>> math.sqrt(4)

    2.0

    取整:

    >>> 1/3

    0

    除法:

    >>> 1.0/3

    0.3333333333333333

    向下取整:小数位全部舍去(“地板除”)

    >>> 5//2.0

    2.0

    >>> 5//2.9

    1.0

    四舍五入:

    >>> round(5.123412,2)

    5.12

    >>> round((5/3),1)

    1.0

    取余:用于判断一个数能否被整除。

    >>> 5%2

    1

    同时获取商和余数:

    >>> divmod(10,3)

    (3, 1)

    >>> divmod(10.9,7)

    (1.0, 3.9000000000000004)

     

    Python无法精确保存计算时: 扩大计算再返回去

    >>> divmod(10.2,3)

    (3.0, 1.1999999999999993)

    >>> divmod(102,3)

    (34, 0)

     

    10. 切片复习

    切片是开区间

    步长:

    >>> s="abcdefghi"

    >>> s[0:8:2]

    'aceg'

    >>> s[:-1:2]

    'aceg'

    #倒着取

    >>> s[-1:0:-2]

    'igec'

    >>> s[-1:0:-1]

    'ihgfedcb'

    >>> s[1:-3:2]

    'bdf'

     

    练习1:三个单词,把这三个单词的最后一个字母拼接到一起

    方法1:

    >>> q1=raw_input()

    abc

    >>> q2=raw_input()

    fgh

    >>> q3=raw_input()

    xxx

    >>> q1[-2:]

    'bc'

    >>> q1[-1:]

    'c'

    >>> q1[-1]+q2[-1]+q3[-1]

    'chx'

    方法2:

    #coding=utf-8

     

    a="abc defghu bxys"

    word_list=a.split()

    print word_list

    result=""

    for word in word_list:

        result+=word[-1]

     

    print result

     

    方法3:
    #coding=utf-8

    result=""
    for i in range(3):
        word=raw_input("请输入一个单词:".decode("utf-8").encode("gbk"))
        result+=word[-1]

    print result

     

     

    练习2:输入无限个单词,拼接输出最后一个字母

    >>> str1=''

    >>> str2=''

    >>> while True:

    ...     str1=raw_input("请输入:")

    ...     str2=str2+str1[-1:]

    ...     if str1==".":

    ...         break

    ...     print str2

    ...

    请输入:www

    w

    请输入:eee

    we

    请输入:qqq

    weq

    请输入:11

    weq1

    请输入:

     

    方法2:

    #coding=utf-8

     

    result=""

    while 1:

    word=raw_input("请输入一个单词:".decode("utf-8").encode("gbk"))  

    #用input打印中文必须用到

        if word==".":break

        result+=word[-1]

     

    print result

     

    11.打印二进制、八进制、十进制

    >>> a=bin(8)

    >>> a[2:]

    '1000'

     

    Zfill补零:切片结果前面没取到的位置自动补充为0

    >>> bin(8)[2:].zfill(len(bin(8)))

    '001000'

    >>> "12".zfill(4)

    '0012'

     

    12. 位与、位或、按位异或、按位翻转

    与:

    >>> 1&0

    0

    >>> 0&0

    0

    >>> 0&1

    0

    或:

    >>> 1|0

    1

    >>> 1|1

    1

    >>> 0|1

    1

    >>> 0|0

    0

    异或:相同为零,不同为一

    >>> 0^1

    1

    >>> 1^0

    1

    >>> 0^0

    0

    >>> 1^1

    0

     

    取反:

    >>> ~1

    -2

     

    练习:

    >>> a=1;b=2

    >>> bin(a)

    '0b1'

    >>> bin(b)

    '0b10'

    >>> a&b

    0

    >>> a|b

    3

    >>> a^b

    3

    >>>

    原理:

    01
    10
    00==——a&b=0
    11=a|b=3
    11=a^b=3

    13.比较关系运算符

    >>> a=1

    >>> b=2

    >>> a==b

    False

    >>> a!=b

    True

    >>> a<>b

    True

    >>> a>b

    False

    >>> a<b

    True

    >>> a>=b

    False

    >>> a<=b

    True

     

    14. 运算符优先级

    记不住的话用小括号括起来。

    >> 右移一位表示除2,左移一位表示乘2->针对二进制

    >>> 4>>2

    1

    >>> 4>>1

    2

    >>> 4<<2

    16

    >>> 4<<1

    8

    二进制:100(4)-右移1位->10(2)-右移1位>1(1)

     

    15. is  is not

    >>> a=2

    >>> a=1000

    >>> b=a

    >>> a is b

    True

    >>> a=1000

    >>> b=1000

    >>> a is b

    False

    原理:同一个数值并不全部指向同一块内存,256之前的是同一个,超过256的不是。

    16. in  not in

    >>> "a" in "abc"

    True

    >>> "w" not in "www"

    False

    17.operator包的应用

    可以直接传参进行运算。

    import  operator

    print  operator.add(1,1)
    print  operator.sub(2,1)
    print  operator.mul(2,3)
    print  operator.div(6,2)
    print  operator.contains("ab","a")
    print  operator.pow(2,3)
    print  operator.ge(1,1)
    print  operator.ge(2,1)
    print  operator.le(1,2)
    print  operator.eq(1,1)

    18. 标准输入、标准输出和错误输出

    输出:

    >>> import sys

    >>> sys.stdout.write("hello ")

    hello

    >>> print "hello"

    hello

    >>> print ("hello")

    Hello

    错误输出:

    >>> sys.stderr.write("error")

    error>>>

    >>> raw_input("input:")

    input:error

    'error'

    输入:

    >>> a=sys.stdin.readline()

    hello

    >>> a

    'hello '

     

    19. 将标准输出改为文件输出

    >>> import sys

    >>> print 'divein!'

    saveout=sys.stdout

    fsock=open('out.log','w')

    sys.stdout=fsock

    print'This message will belogged instead of displayed'

    sys.stdout=saveout

    fsock.close()

     

    divein!

    没写 路径,默认在当前程序的路径下。

     

    20. 重定向到错误输出

    #encoding=utf-8
    import sys
    print >> sys.stderr, 'Fatal error: invalid input!'

     

    21.表达式

    #coding=utf-8
    length = 5
    breadth = 2
    area = length * breadth
    print u'面积是:', area
    print u'周长是:', 2 * (length + breadth)

    22. 单行表达式

    flag = True
    if flag: print 'Yes' 

     

    练习:code word 网站

    23. 数学函数

    >>> import math

    >>> math.fabs(-1)

    1.0

    >>> math.ceil(1.1)

    2.0

    >>> math.floor(2.1)

    2.0

    >>> math.log(10)

    2.302585092994046

    >>> dir(math)

    ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan',

     'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'er

    fc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gam

    ma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'mod

    f', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

    >>> math.log10(10)

    1.0

     

    24. 三角函数

    25.随机函数

     随机1-10的整数

    >>> import random

    >>> random.randint(1,10)

    5

    >>> random.randint(1,10)

    4

    >>> random.randint(1,10)

    10

     

    随机一个列表

    >>> random.choice([1,2,3,4,5])

    4

    >>> random.choice([1,2,3,4,5])

    2

    >>> random.choice([1,2,3,4,5])

    2

     

    打乱列表顺序:

    >>> a=[1,2,34,5]

    >>> random.shuffle(a)

    >>> a

    [34, 2, 1, 5]

     

    26.五种基本数据类型间的转换

     

     

    >>> int('4e00',base=16)

    19968

    >>> print unichr(19968)

    汉字编码:http://www.qqxiuzi.cn/zh/hanzi-unicode-bianma.php

     

    27.退出程序(强制退出)

    import  sys
    print '1'
    sys.exit()
    print  '2'

     

    28. 浮点数计算之坑

    Eg:浮点数计算有错误

    >>> 10-9.9

    0.09999999999999964

     

    29. 使用pdb调试程序

     

  • 相关阅读:
    MySQL select语句中where条件的提取过程
    MySQL特性:ICP,Index Condition Pushdown
    MySQL特性:BKA,Batched Key Access,批量索引访问
    MySQL特性:MRR,Multi-Range Read,多范围读
    show engine innodb status 输出结果解读
    IPv6的一点使用小计
    MySQL 通过ibd恢复数据
    explain 小结
    clickhouse的多路径存储策略
    cenos6.5升级glibc2.18
  • 原文地址:https://www.cnblogs.com/qingqing-919/p/8620313.html
Copyright © 2011-2022 走看看