python學習記錄2

來源:互聯網
上載者:User

標籤:code   min   相對   es2017   北京   apc   環境變數   res   nes   

一、兩個模組(sys和os)

 1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 import sys 5 print(sys.path)#列印環境變數 6 print(sys.argv[0])#當前檔案相對路徑,sys.argv是一個列表,第一個元素為程式本身的相對路徑,之後的為程式運行是的輸入參數 7  8 import os 9 #cmd_res= os.system("dir")#執行命令不儲存結果10 cmd_res = os.popen("dir").read()#儲存命令執行的結果並返回儲存地址11 print("-->",cmd_res)12 os.mkdir("new_dir")#建立一個目錄

sys和os兩個模組的簡易使用

import

現在目前的目錄下尋找模組,在環境變數中尋找模組

存放第三方模組的路徑  C:\Python36-32\Lib\site-packages

二、python中string與bytes之間的轉換

1 #!/usr/bin/env python2 # _*_ coding: UTF-8 _*_3 # Author:taoke4 str = "我愛北京天安門"5 str_endode = str.encode("utf-8")6 str_endode_decode = str_endode.decode("utf-8")7 print(str,type(str))8 print(str_endode,type(str_endode))9 print(str_endode_decode,type(str_endode_decode))
運行結果:我愛北京天安門 <class ‘str‘>b‘\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8‘ <class ‘bytes‘>我愛北京天安門 <class ‘str‘>

 

三、列表(List)

 1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 names = ["xiaoming","xiaohong","xiaohei","xiaoxiao"] 5  6 print(names) 7 print(names[0],names[2]) 8 print(names[1:3])#顧頭不顧尾,切片 9 10 names.append("xiaobingbing")11 print(names)12 names.insert(1,"renma")13 print(names)

 

 List中的淺copy和深copy

#!/usr/bin/env python# _*_ coding: UTF-8 _*_# Author:taokeimport copynames = ["xiaoming","xiaohong",["Jack","Toms"],"xiaohei","xiaoxiao"]names2 = names.copy()#淺copynames3 = copy.copy(names)#淺copynames4 = copy.deepcopy(names)#深copynames[2][0] = "JACK"print(names)print(names2)print(names3)print(names4)
運行結果:
[‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘Jack‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘]

 

 四、tuple(元組)

 不可以更改的列表,只能查。

五、string(字串方法)

  1. str.rjust:靠右對齊
  2. str.ljust:靠左對齊
  3. str.center:中間對齊
  4. str.zfill:預設的方式
  5. str.find:字串尋找,沒有返回-1
  6. str.index:尋找字串位置,沒有返回錯誤
  7. str.rfind:從右開始尋找
  8. str.rindex:同上
  9. str.count:統計字串出現的次數
  10. str.replace:字串替換
  11. str.strip:去除字串開頭末尾的空格
  12. str.lstrip:去除左邊空格
  13. str.rstrip:去除右邊空格
  14. str.expandtabs:把字串裡的table換成等長的空格
  15. str.lower:
  16. str.upper:
  17. str.swapcase:將字串字元大小寫反轉
  18. str.capitalize:字串首字元大寫
  19. str.title:字串中首字母大寫 
  20. str.split:字串拆分成列表
  21. str.splitlines:將字串中按行拆分放到列表中
  22. ‘-‘.join(strList):用‘-’將列表strList串連成字串
  23. str.startswith:測試字串是否是以指定字元開頭的
  24. str.endswith:測試字串是否是以指定字元結尾的
  25. str.isalum:判斷字串是否全是字元或數字並至少有一個字元
  26. str.isalpha:判斷字串是否全是字母
  27. str.isdigit:判斷字串是否全是數字
  28. str.isspace:判斷字串是否含有空格
  29. str.islower:判斷字串是否全是小寫
  30. str.isupper:判斷字串是否全是大寫
  31. str.istitle:判斷首字母是否是大寫
  32. import string
  33. string.atoi("123",base=10/8/16):轉換字串到int類型的數字
  34. string.atol:轉換字串到長整形數字
  35. string.atof:轉換字串到浮點型

 

python學習記錄2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.