上課第四天

匿名函數:api

a=int(input('輸入一個數:>>'))
(lambda x:print('自加 = ',x+x))(a)

---------------------------------------------------------------------------------------------數組

  • 字符中,小寫變大寫:

a.capitalize()       #只能改變首字母app

  • 從字符串中找對應字母

a.find('找的字母')函數

  • 是否爲開頭

a.startswith()blog

  • 左右填充
 a.center()
  • 切片
str2 = ' a b c 1 2 3 4 5 6 '
            0 1 2 3 4 5 6 7 8
            9 8 7 6 5 4 3 2 1       倒數沒有0

# 字符串切片(從指定的開始索引到指定的結束索引)
print(str2[2:5]) # c12
print(str2[2:]) # c123456            #從第二個開始,所有輸出
print(str2[2::2]) # c246            # :至關於字符串的所有,從第二個開始,隔一個輸出一個
print(str2[::2]) # ac246              #從開始,隔一個輸出一個
print(str2[::-1]) # 654321cba                  #倒的輸出
print(str2[-3:-1]) # 45                      #數組前閉後開,取到-3和-2索引

  • 刪除兩邊的空格

a.strip()ip

  • 去掉字符中所有空格

a.replace(" ",'')             #  "  "被替換爲''rem

  • 連續輸出5次字符

list=['Hello']*5字符串

print(list)input

  • 在數組後面追加

a.append(追加的數字)

在索引以前加

a.insert(索引,所添加的)

  • 刪除

a=[1,1,12,3,4]

a.remove(12)

輸出:[1,1,3,4]

  • 去重
def Qc():          #去重
a=[1,1,2,2,3]
b=[]
for i in a:
if i not in b:
b.append(i)
print(b)
Qc()
  • 進度條
import time
def Jdt():
for i in range(1,101):
time.sleep(1)
print('%s%d%%\r' %('#'*i,i),end="",flush=True)
Jdt()
  • 字典:讓文件遍歷輸出(讀取文件)
path='F:\\Classes_.txt'             #絕對地址
open_=open(path,mode='r',encoding='utf')            #r爲只讀模式
lines=open_.readlines()
dict_={}
print('文件內容:',lines)
for str_ in lines:
str_1=str_.strip('\n')                   #去除\n
print(str_1.split(' '))
key,value=str_1.split(' ')
# print(key,value)
dict_[key]=value

num=input('>>')
value=dict_[num]
print(value)
部分

----------------------------------------------------------------------------------------------------------------------------

明天見吧

相關文章
相關標籤/搜索