Python學習記錄-2016-12-15

來源:互聯網
上載者:User

標籤:北京天安門   document   names   記錄   count   

因為作業關係,跳轉聽了部分課程,現在記錄如下:

List列表部分:

列表的寫法:

names = ["zhangsan", "lisi", "zhaowu", "wangliu", "qianqi"]

列表的index:

列印從左側到第三個:

print(names[0:2])

列印從第三個到結束:

print(names[2:4])print(names[-3:])

列印從第二個到第三個:

print(names[1:2])

列表元素的追加:

names.append("sunba")

列表元素的插入:

names.append(1, "lishi")

下面是讀取檔案

開啟檔案:

f = open("document", "r", encoding="UTF-8")#r代表開啟,w代表覆蓋寫入或建立,a代表追加

寫入:

f.write("我愛北京天安門\n")

關閉檔案:

f.close()

檔案讀取為元素列表

f.readlines()

某一行

f.readline()

列印5行:

for i in range(5):    print(f.readline())

嘗試使用while迴圈寫:

count = 0while True:    print(f.readline())    count+=1    if count ==5:        break

第十行列印分割線:

for index , line in range enumerate(f.readlines()):    if index == 9:    print("--------------------")    continue    print(line.strip())#strip去掉所有空格和分行符號



Python學習記錄-2016-12-15

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.