Python部分知识点
生活随笔
收集整理的這篇文章主要介紹了
Python部分知识点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1format方法
format中 數字表示所占寬度 符號^表示居中顯示 \t表示添加制表符
format_title="{:^4}{:^12}\t{:^8}\t{:^10}\t{:^10}" print(format_title.format("ID","名字","英語成績","Python成績","C語言成績","總成績"))運行結果:
ID 名字 英語成績 Python成績 C語言成績
2判斷字符串是否為純字母串或者數字串
instr=input() if str.isdigit(instr):print('is digit') elif str.isalpha(instr):print('is alpha') else:print('i dont know')3去掉字符串中[]
strr='[1234556ssdd]' s_strr=strr.replace('[','').replace(']','') print(s_strr) #1234556ssdd4隨機產生數字 random.sample()產生的隨機數不重復 random.choice()產生的隨機數可以重復
rand_choice='' rand_choice_list=[] rand_sample='' rand_sample_list=[] import random a=[1,2,3,4,5,6,7,8,9] for j in range(10):for i in range(6):rand_choice=random.choice(a)rand_sample=random.sample(a,1)rand_choice_list.append(rand_choice)rand_sample_list.append(rand_sample) print(rand_sample_list) print(rand_choice_list)運行結果:
[[2], [9], [3], [6], [6], [7], [7], [3], [1], [8]]
[4, 6, 8, 8, 6, 3, 8, 4, 4, 6]
5字符映射 str.maketrans(intab, outtab) intab – 字符串中要替代的字符組成的字符串。outtab – 相應的映射字符的字符串。
將字符映射為數字
instr="0123456789" outstr="abcdefghij" transtab=str.maketrans(instr," ") transtab1=str.maketrans(instr,outstr) print(transtab) #{48: 97, 49: 98, 50: 99, 51: 100, 52: 101, 53: 102, 54: 103, 55: 104, 56: 105, 57: 106} str1="0123456789yy" print(str1.translate(transtab)) #abcdefghijyy6input默認輸入為字符串類型
7三重引號可以注釋 也可以定義多行字符串
8#利用try except語句捕獲打開文件異常
try:student_txt=open(filename,'a') #以追加模式打開文件 except Exception as e:student_txt=open(filename,'w') #文件不存在,創建文件并打開9列表排序 lamada
#1列表排序:使用lamada表達式進行排序 student_new=[{'id':1,'name':'無語1','english':100,'python':98},{'id':3,'name':'無語2','english':87,'python':96},{'id': 2, 'name': '無語3', 'english': 95, 'python': 100}]student_new.sort(key=lambda x:x['english'],reverse=True) print(student_new) # 輸出結果 # [{'id': 1, 'name': '無語1', 'english': 100, 'python': 98}, {'id': 2, 'name': '無語3', 'english': 95, 'python': 100}, {'id': 3, 'name': '無語2', 'english': 87, 'python': 96}]10零碎知識點
#1 判斷字符串是否為字母或者數字串 #2020-05-10instr=input() if str.isdigit(instr):print('is digit') elif str.isalpha(instr):print('is alpha') else:print('i dont know')#2 替換[] # 2020-05-10strr='[1234556ssdd]' s_strr=strr.replace('[','').replace(']','') print(s_strr) # 1234556ssdd # # # #3 隨機產生數字 random.sample()產生的隨機數不重復 random.choice()產生的隨機數可以重復 # rand_choice='' rand_choice_list=[] rand_sample='' rand_sample_list=[] import random a=[1,2,3,4,5,6,7,8,9] for j in range(10):for i in range(6):rand_choice=random.choice(a)rand_sample=random.sample(a,1)rand_choice_list.append(rand_choice)rand_sample_list.append(rand_sample) print(rand_sample_list) print(rand_choice_list) # [[3], [6], [4], [5], [4], [9], [4], [4], [9], [2]] # [8, 7, 2, 4, 3, 2, 2, 5, 9, 3] # # # #4 字符映射 str.maketrans(intab, outtab) intab -- 字符串中要替代的字符組成的字符串。outtab -- 相應的映射字符的字符串。 # # 將字符映射為數字 # instr="0123456789" outstr="abcdefghij" transtab=str.maketrans(instr," ") transtab1=str.maketrans(instr,outstr) print(transtab) # {48: 97, 49: 98, 50: 99, 51: 100, 52: 101, 53: 102, 54: 103, 55: 104, 56: 105, 57: 106} str1="0123456789yy" print(str1.translate(transtab)) # abcdefghijyy11可不列表和不可變列表
list=['12',3,4,5] print(id(list)) #id() 函數用于獲取對象的內存地址。 list[3]='review' print(id(list)) s_string='hello' print(id(s_string)) # s_string[0]='o' print(id(s_string)) tuple_test=(1,2,"sdsd",[3,4]) print(id(tuple_test)) tuple_test[3][1]=100 # tuple_test[2]='ssss' 不可修改 發生錯誤 # tuple_test[0]=2 不可修改 發生錯誤 print(tuple_test) print(id(tuple_test)) # 輸出結果 # 43120456 # 43120456 # 35615272 # 35615272 # 43095208 # (1, 2, 'sdsd', [3, 100]) # 43095208總結
以上是生活随笔為你收集整理的Python部分知识点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot security
- 下一篇: websocket python爬虫_p