Python-Day3

1. 编码问题(中文输出)

Python 2:

先将 utf-8 转为 unicode , 再编码为 gbk (自己敲代码)

#!/usr/bin/env python
# -*- coding:utf-8 -*-

temp = "测试"
#先解码,再编码
temp_unicode = temp.decode('utf-8')
temp_gbk = temp_unicode.encode('gbk')

print(temp_gbk)

  

Python 3.6

没有了 unicode ,无需自己考虑

#直接print()即可

temp = '测试'
print(temp)

  

对于Windows终端

  自动将 unicode 编成 gbk 格式,所以只需要解码为 unicode 即可,如下

# -*- coding:utf-8 -*-

temp = "测试"
temp_unicode = temp.decode('utf-8')

print(temp_unicode)

  

 2. PyCharm 使用Tips

  1. 注释 或 取消注释:选中代码 按 ctrl + / 

  2. 设置 在 File -> settings... 中

  pycharm 修改默认模版

  File -> settings -> Editor -> Code style -> File and Code templates -> Python Script

  3. ctrl + 鼠标左键 点击关键字,方法名 可以进入定义的位置

 

3. Python 小记

  1) 成员运算符(还有 算术,比较,逻辑,成员,身份 运算符)

    in      not in 

  2) 查看某个对象的类,方法等详细信息

    type()   dir()  help()    ctrl + 鼠标左键

val = 'alex'
print(type(val)) #查看类名
print(dir(val))  #查看全部方法名
help(type(val))  #超级详细

  3)  int 类

    bit_length()  二进制表示的最短位数

    n1+n2 --->  n1__add__(n2)

     str 类

li = ['hello', 'world', 'AHA']
s = "###".join(li)
print(s)

#运行结果为
hello###world###AHA

  

  4)  若源码中函数括号里是 function(self) 表示不用传递参数;若 function(self, asdf, asfd), 表示传递两个参数; 若 function(self, asdf, asfd=None), 表示可以传递一个参数,asfd参数取默认值,当然也可以指定所有参数。

posted @ 2017-03-14 15:22  云中王  阅读(144)  评论(0编辑  收藏  举报