Python-Day3

1. 編碼問題(中文輸出)

Python 2:

先將 utf-8 轉爲 unicode , 再編碼爲 gbk (本身敲代碼)python

#!/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... 中code

  pycharm 修改默認模版對象

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

  3. ctrl + 鼠標左鍵 點擊關鍵字,方法名 能夠進入定義的位置ip

 

3. Python 小記

  1) 成員運算符(還有 算術,比較,邏輯,成員,身份 運算符)utf-8

    in      not in unicode

  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參數取默認值,固然也能夠指定全部參數。

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息