• Python学习笔记~


    View Code
      1 from 模块 import 函数
      2     >>> from math import sin
      3     >>> sin(1.5)
      4     0.9974949866040544
      5 
      6 直接import
      7     >>> import cmath
      8     >>> cmath.sqrt(-9)
      9     3j
     10 
     11 字符串表示,str和repr
     12     >>> print 100000L
     13     100000
     14     >>> print (10000L)
     15     10000
     16     >>> print ("1000L")
     17     1000L
     18     >>> print repr("Hello world!") repr:创建一个字符串,以合法的Python表达式的形式来表示值。
     19     'Hello world!'
     20     >>> print repr(10000L)
     21     10000L
     22     >>> print str("Hello") str:把值转换为合理形式的字符串,以便用户理解
     23     Hello
     24     >>> print str(1000L)
     25     1000
     26 (Plus 反引号`: used in 2.7 deprecated.)
     27 
     28 raw_input: 把所有的输入当做原始数据,放入字符串中
     29 
     30 长字符串: 三个引号 or \[回车]
     31 
     32 print r's\tring' 输入原始字符串。不能以'\'结尾(显而易见)
     33 
     34 print u"unicode string" 2.7以后不支持?
     35 
     36 序列:
     37 
     38 1 索引:
     39     >>> week = ['Mon', 'Tue'] + 3 * ['Wed']
     40     >>> week
     41     ['Mon', 'Tue', 'Wed', 'Wed', 'Wed']
     42     >>> week[1]
     43     'Tue'
     44 
     45 2 分片:
     46     >>> week[1:3]
     47     ['Tue', 'Wed']
     48     >>> week[1:2]
     49     ['Tue']
     50     >>> week[-3:]
     51     ['Wed', 'Wed', 'Wed']
     52 
     53 3 步长
     54     >>> num = [1,2,3,4,5,6,7,8,9]
     55     >>> num[1:7:2]
     56     [2, 4, 6]
     57     >>> num[7:1:-2]
     58     [8, 6, 4]
     59 
     60 空值 None
     61 
     62 布尔值 True False
     63 
     64 成员资格 in
     65     >>> array = [1,2,3]
     66     >>> 1 in array
     67     True
     68     >>> 4 in array
     69     False
     70 
     71 长度、最大值和最小值 len max min
     72 
     73 列表:
     74     list函数 返回列表
     75     >>> array=[1,2,3,4,5]
     76     >>> list(array)
     77     [1, 2, 3, 4, 5]
     78 
     79     del array[1] 删除元素
     80     分片赋值 and so on.
     81     
     82     列表方法:
     83         append(element)
     84         count(element)
     85         extend(lsit)
     86         index(element)
     87         isert(index, element)
     88         pop() & pop(index)
     89         remove(element)
     90         reverse()
     91         sort()
     92         cmp(element, element)
     93         
     94 
     95 元组:
     96     >>> (1, 2, 3)
     97     (1, 2, 3)
     98     >>> (1,)
     99     (1,)
    100     >>> (1)
    101     1
    102     
    103     tuple函数:返回元组
    104     >>> tuple([1, 2, 3])
    105     (1, 2, 3)
    106     >>> tuple('yes')
    107     ('y', 'e', 's')
    108     
    109 字符串
    110     格式化输出
    111     方法:
    112         find rfind index rindex count startwith endwith
    113         join split(element) splitlines
    114         lower islower capitalize swapcase title istitile upper isupper
    115         replace(a, b)
    116         strip() strip(element)
    117         
    118         >>> import string
    119         >>> table = string.maketrans('ex', 'ez')
    120         >>> len(table)
    121         256
    122         >>> table[97: 123]
    123         'abcdefghijklmnopqrstuvwzyz'
    124         >>> 'index fix fex'.translate(table)
    125         'indez fiz fez'
    126 
    127 字典:
    128     >>> stu = {'tom': '026', 'van': '027', 'von': '028', 'Liang': '029'}
    129     >>> stu
    130     {'von': '028', 'van': '027', 'Liang': '029', 'tom': '026'}
    131     >>> stu['von']
    132     '028'
    133     >>> stu = [('tom', '1'), ('jim', '2')]
    134     >>> stu
    135     [('tom', '1'), ('jim', '2')]
    136     >>> d = dict(stu)
    137     >>> d
    138     {'jim': '2', 'tom': '1'}
    139     >>> d['jim']
    140     '2'
    141     >>> d = dict(name = 'tom', age = '23')
    142     >>> d
    143     {'age': '23', 'name': 'tom'}
    144     
    145     键可以为任何不可变类型
    146     
    147     >>> x = [None] * 5
    148     >>> x
    149     [None, None, None, None, None]
    150     
    151     >>> x = {}
    152     >>> x
    153     {}
    154     >>> x['q'] = 123
    155     >>> x
    156     {'q': 123}
    157     
    158     字典的格式化字符串:
    159         >>> phone = {'tom': '110', 'jim': '120'}
    160         >>> 'tom\'s phone number is %(tom)s' % phone
    161         "tom's phone number is 110"
    162     
    163     字典方法
    164         clear
    165         copy 浅复制; deepcopy 深复制
    166         fromkeys([key1, key2]) 使用给定的键建立新的字典,键值为None.
    167         get(key, default value)
    168         has_key(key)
    169         items() 以列表方式返回item
    170         iteritems() 返回迭代器对象(高效)
    171         keys() 以列表方式返回键
    172         iterkeys() 以迭代器方式返回
    173         pop(key) 返回键的值并移除键值对
    174         popitem() 弹出随机项
    175         setdefault(key, default value) ≈ get + 在字典中不含有给定键的情况下设定相应的键值
    176         update(dict) 用一个字典项更新另外一个字典
    177         values()
    178         itervalues()
    179     
    180 print
    181     在3.0前为语句,3.0后为函数
    182     
    183 导入模块、方法
    184     import somemodule
    185     from somemodule import somefuction1, ...
    186     from somemodule import *
    187     import somemodule as alias
    188     from somemodule import somefuction as alias
    189 
    190 序列解包
    191     >>> x, y, z = 1, 2, 3
    192     >>> print x, y, z
    193     1 2 3
    194     >>> value = 1, 2, 3
    195     >>> value
    196     (1, 2, 3)
    197     >>> print value
    198     (1, 2, 3)
    199     >>> x, y = y, z
    200     >>> x, y, z
    201     (2, 3, 3)
    202     >>> d = {'tom': 1, 'jim': 2}
    203     >>> key, value = d.popitem()
    204     >>> key, value
    205     ('jim', 2)
    206     >>> key
    207     'jim'
    208     >>> value
    209     2
    210     
    211 链式赋值
    212     x = y = ...
    213     
    214 增量赋值
    215     >>> x = 't'
    216     >>> x += 'om'
    217     >>> x
    218     'tom'
    219     >>> x *= 2
    220     >>> x
    221     'tomtom'
    222     
    223 if语句
    224     if(clause):
    225         ...
    226         ...
    227     elif(clause):
    228         ...
    229         ...
    230     else:
    231         ...
    232         ...
    233 
    234 is
    235     同一性运算符
    236     
    237 in
    238     成员资格运算符
    239 
    240 and or not
    241     布尔运算符
    242     Special
    243         >>> name = raw_input("Name: ") or '<unknown>'
    244         Name: 
    245         >>> name
    246         '<unknown>'
    247         >>> name = raw_input("Name: ") or '<unknown>'
    248         Name: Tom
    249         >>> name
    250         'Tom'
    251     三元运算
    252         a if b else c
    253         
    254         >>> b = 1
    255         >>> a = 'true' if (b == 1) else 'false'
    256         >>> a
    257         'true'
    258         >>> b = 0
    259         >>> a = 'true' if (b == 1) else 'false'
    260         >>> a
    261         'false'
    262 
    263 assert 断言
    264     >>> a = 1
    265     >>> assert a == 2, 'a should equals 2'
    266 
    267     Traceback (most recent call last):
    268       File "<pyshell#13>", line 1, in <module>
    269         assert a == 2, 'a should equals 2'
    270     AssertionError: a should equals 2
    271     
    272 while语句
    273     >>> x = 1
    274     >>> while x < 10:
    275         print x
    276         x += 1
    277         
    278 range() 范围函数
    279     >>> range(10)
    280     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    281     >>> range(0, 10)
    282     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    283     
    284 for 语句
    285     nums = [1, 2, 3]
    286     for num in nums:
    287         print num
    288 
    289     for x in range(0, 3):
    290         print x
    291         
    292     遍历字典元素:
    293         >>> d = {'a': 'tom', 'b': 'jim'}
    294         >>> for key in d:
    295             print key, ":", d[key]
    296         a : tom
    297         b : jim
    298         >>> for key, value in d.items():
    299             print key, ":", value
    300         a : tom
    301         b : jim
    302 
    303     并行迭代:
    304         >>> name = ['tom', 'jim']
    305         >>> age = [18, 20]
    306         >>> for i in range(len(name)):
    307             print name[i], ":", age[i]
    308         tom : 18
    309         jim : 20
    310         >>> for n, a in zip(name, age):
    311             print n, ":", a
    312         tom : 18
    313         jim : 20
    314         
    315         zip函数可用于不等长的序列
    316         
    317     编号迭代:
    318         >>> s = ['xxx', 'yyy', 'xdresxxx']
    319         >>> for index, string in enumerate(s):
    320             if('xxx' in string):
    321                 s[index] = '@@@'
    322         >>> s
    323         ['@@@', 'yyy', '@@@']
    324         
    325 sorted、reversed、join函数
    326     
    327 break、continue语句
    328 
    329 pass语句
    330 
    331 del 删除名称 not332 
    333 exec 执行字符串的语句
    334     命名空间/作用域
    335         >>> from math import sqrt
    336         >>> scope = {}
    337         >>> exec 'sqrt = 1' in scope
    338         >>> sqrt(4)
    339         2.0
    340         >>> scope['sqrt']
    341         1
    342 
    343 eval 计算Python表达式
    344     >>> eval(raw_input("Expression: "))
    345     Expression: 2 + 3 - 4 *5
    346     -15
    347     >>> raw_input("Expression: ")
    348     Expression: 1 + 1
    349     '1 + 1'
    350     
    351     >>> scope = {}
    352     >>> exec 'x = 2' in scope
    353     >>> eval ('x * x', scope)
    354     4
    355 
    356 callable 判断函数是否可以调用(3.0不可用)
    357 
    358 创建函数
    359     >>> def hello(name):
    360             return 'Hello ' + name
    361     >>> hello('tom')
    362     'Hello tom'
    363 
    364 记录函数
    365     >>> def plus(a, b):
    366             'Return a + b'
    367             return a + b
    368     >>> plus.__doc__
    369     'Return a + b'
    370 
    371     >>> help(plus)
    372     Help on function plus in module __main__:
    373     plus(a, b)
    374         Return a + b
    375 
    376 关键字参数
    377     plus(a = xxx, b = yyy) 等于 plua(b == yyy, a = xxx)
    378 
    379     在函数给参数提供默认值:
    380     >>> def demo(greeting = 'Hello!'):
    381             print greeting
    382     >>> demo()
    383     Hello!
    384     >>> demo('你好')
    385     你好
    386 
    387 收集参数
    388     >>> def say_hello(greeting, *names, **extras):
    389         print greeting, names, extras
    390     >>> say_hello('Hi', 'tom', 'jim', 'lara', x = 'glad', y = 'to see you.')
    391     Hi ('tom', 'jim', 'lara') {'y': 'to see you.', 'x': 'glad'}
    392 
    393 逆转过程(分割)
    394     >>> def hi(hi, you):
    395             print hi, you
    396     >>> hello = ('Hello', 'Tom')
    397     >>> hi(*hello)
    398     Hello Tom
    399     
    400     >>> yeah = {'hi': 'Hello, ', 'you': 'Tom'}
    401     >>> hi(**yeah)
    402     Hello,  Tom
    403 
    404 作用域
    405     vars():返回变量字典
    406         >>> x = 1
    407         >>> scope = vars()
    408         >>> scope['x']
    409         1
    410         >>> scope['x'] += 1
    411         >>> scope['x']
    412         2
    413     globals():返回全局变量的字典
    414     locals():返回局部变量的字典
    415     global关键字:声明变量为全局变量
    416         >>> x
    417         3
    418         >>> def change_x():
    419                 global x
    420                 x += 1
    421                 return x
    422         >>> change_x()
    423         4
    424         >>>
    425     
    426     嵌套函数
    427         >>> def multiply(a):
    428                 def multiplyByFactor(b):
    429                         return a * b
    430                 return multiplyByFactor
    431         >>> num = multiply(2)
    432         >>> num
    433         <function multiplyByFactor at 0x0000000002B79C18>
    434         >>> num(3)
    435         6
    436         >>> num(4)
    437         8
    438         >>> multiply(4)(5)
    439         20
    440     
    441 类:
    442     调查继承:
    443         issubclass(subclass, baseclass)
    444         subclass.__bases__
    445         
    446     判断类的实例:
    447         isinstance(instance, class)
    448     
    449     对象所属类:
    450         object.__class__
    451         
    452     多继承:
    453         def class X (class1, class2)
    454     
    455     hasattr(object, '<attr name>')
    456     
    457     getattr(object, '<attr name>', <default value>)
    458 
    459     setattr(object, '<attr name>', <value>)
    460     
    461     object.__dict__ 查看对象内所有存储的值
  • 相关阅读:
    struts2.16 启动报错解决方案 at com.opensymphony.xwork2.util.FileManager$FileRevision.needsReloading(FileManager.java:209)
    多线程的概念
    JQuery Easy Ui 可装载组合框 ComboBox
    Spring 3.0 基于 Annotation 的依赖注入实现
    JQuery Easy Ui dataGrid 数据表格
    数据库中 对原有字段进行字符串追加处理
    1.Python知识点补充
    转跨站脚本攻击详解
    Wiz笔记
    ASP.NET角色管理的基本配置
  • 原文地址:https://www.cnblogs.com/DuSizhong/p/2521636.html
Copyright © 2020-2023  润新知