Python Basic concepts

Source: Internet
Author: User

2016.5.211. All objects:

Objects in Python, object types are assigned similar to C language pointers

2. Number types
    • Decimal

      将相应的函数改名使用:from decimal import Decimal as D    eg:D(‘0.3‘)/D(3)统计运行时间:python -mtimeit -s "from decimal import Decimal as D"        D(‘1.2‘)+D(‘3.4‘)        由此可以发现decimal非常地耗费时间
    • Common libraries:

      dir(math)  help(math)  #z:上一页  w:下一页   q:退出math:    import math    math.pi    math.sprt(80)    math.log10(2**1000)    math.pow(x,y)    math.factorial(x)   #连乘random:    import random    random.random() #0~1之间的随机数    random.choice([1,2,3,4])    random.randint(a,b)         random.uniform(a,b)    random.gauss(mu,lamda)numpy:    产生数组和矩阵,正太分布的随机数    
3. String

Slices, index

Common Methods Collection:

+ * s.upper() s.find(‘f‘) s.replace(‘python‘,‘java‘)  print "%s like %s" %(‘we‘,‘python‘)

Import Re:regular Expression

^ start$ end. except+ 1-inf* 0-inf? 0-1[] or{} repeat[^] not| or\w  [a-zA-Z0-9_],  (not)\W\d  [0-9],  (not)\D\s  [ \t\n\r\f\v],   (not)\S\b  word boundary\B  not \b\A  input start, ^\Z  input end, $*  0~inf greedy+  1~inf greedy*?  0~inf non-greedy+?  1~inf non-greedy()  group #用于区分域名之类的有帮助(?P<name>pattern)等价(re.X:去掉中间的空格,换行符和注释等,还有其他的编译选项):a = re.compile(r"""\d +  # 整数部分                   \.    # 小数点                   \d *  # 小数部分                """, re.X)b = re.compile(r"\d+\.\d*")
4. Date and Time
    • Datetime:
    • Time

      This part is not too detailed for the time being, then it can be used directly after use.

2016.5.225. List

Slices, indexes, references

Common operations:

6. Tuples

Immutable list: cannot be modified in situ

Common operations: Convert a tuple count Count + *

7. Dictionaries

Key-value: Relative between keyword and value

Common operations: Keys values get update del clear nesting

特点:没有顺序的数据结构,类似于c中的散列表,适合插入查询操作key为不可变对象
8. Documents

Common operations:

open(path,‘r‘)  #r:r w aread readline write close   中文支持:import codecsf=codecs.open(filename,mode,encoding)文件操作:import osos.path.exists(filename)os.rename(old,new)

Shelve Library:

import shelveD = Shelve.open(file)   #读到字典中

Pickle/cpickle #cPickle更底层所以更快

import cPicklef = open(file,mode)cPickle.dump(obj,f) #先入先出obj = cPickle.load(f)
9. Statement expressions
赋值输入input:raw_input():当作自负串赋给后边的变量            input():运行输入的内容3.x 输出:print([obj,...][,sep=‘‘][,end=‘\n‘][,file=sys.std.stdout])列表解析:    可以去掉列表中的重复元素    可以简洁地用语句表达嵌套循环等,并且相应的时间会减少异常处理:    raise:自己定义errortype的提示信息    assert condition,message:条件触发,触发后打印信息(except中打印),并终止程序    try/except:有异常时执行except    try/finally:无论如何执行 finally
10. Functions that work efficiently
函数语法定义:    def func_name(arguments):        statement        return x,y    x,y = func_name(para)变量作用域:    local    encloding function locals    global(module)    build-in(python)函数作参数:    把函数作为参数的函数叫做 高阶函数自带高阶函数:    filter,map,reduce
2016.5.23 Functional Programming
lambda    用法:lower = (lambda x,y:x if x<y else y) 返回一个函数,简化程序的编写    calc_dict={        ‘+‘:lambda a,b:a+b,        ‘*‘:lambda a,b:a*b,        ‘-‘:lambda a,b:a-b,    }    用法:calc_dict[‘*‘](12,3)    将函数当作数据来使用
Callback Callback
把上面的函数式编程的方式中的 lambda 改为 callback 函数,很符合编程标准了就!
Closures and decorators
闭包:绑定外部变量的函数:    外部变量在内部函数中可以以列表的方式进行访问和改变    当我们采用两层函数的时候是为了更方便地传递参数装饰器:    无嵌套,函数作为返回值    @一个函数....return:在导入的时候就会被运行    里面有一定的学问,以后用到的时候慢慢去理解
2016.5.24

Recursive

Generator and yield
迭代器:避免重复计算,只有当需要用到的时候才计算出来。惰性计算    速度简直了,当是大数据的时候,速度可以提升100甚至以上倍数    用 send 方式传送数据

Python Basic concepts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.