Python 杂记-1

来源:互联网 发布:mac hot tahiti 编辑:程序博客网 时间:2024/06/16 13:38

Python 杂记-1

  1. 地板除:”//”; 取余:”%”;幂:”**”;

  2. 在python交互界面中,最后输出值得变量名为“_”(相当于matlab里的ans);

  3. 字符串中“\n”表转行,通过在前面加r表示使用原字符串, print(r”C:\some\name”);

  4. “”” … “”“, ”’ … ”’,可以包括字符串中的多行,也可用来注释多行;

  5. 字符串不可改变,元组也不可以被改变;word=”Python”, 报错: word[0]=’J’;

  6. u”Hello world” 表示使用unicode编码;转换方法string.encode(“utf-8”),unicode(string,”utf-8”);

  7. raw_input()返回值为字符串;input会根据输入值自行转换;

  8. 循环语句也搭配else使用;

  9. result.append(a)比 result+[a]效率更高;

  10. 默认参数必须放在后面;

  11. def f(a, *b, **c): pass 其中*b当做元组, **c为字典;

def f(a, *b, **c):    print a    print b    print cf("L1", "L2", "L3", "L4", A="L5", B="L6")输出为"L1"("L2", "L3", "L4"){'A':"L5", 'B':"L6"}

12 . args 位参数列表List。如何unpacking args?
例:args=[1, 3] Error:range(args); range(*args)等价于range(1,3);
args={‘a’:1, ‘b’:2, ‘c’:3} f(*args)等价于f(1,2,3);

0 0