python學習-day9_內置函數

學習視頻:https://www.bilibili.com/video/BV1SE411N7Hi?p=57

基礎知識:https://guobaoyuan.gitee.io/new_book/Python/12-3%20%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0%E4%B8%80.html

以下內容僅供自己學習使用,侵刪

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
abs(-20)#絕對值

format(10,'b')#bin,把10進制格式化成2進制1010
print(format(10,'08b'))#00001010,將十進制轉化成二進制,不夠8位的用0補齊
print(format(10,'08o'))#00000012,將十進制轉化成八進制,不夠8位的用0補齊
print(format(10,'08x'))#0000000a,將十進制轉化成十六進制,不夠8位的用0補齊
print(format(2.156454,'.2f'))#保留2位小數
print(format("你好",">20"))#右對齊
print(format("你好","<20"))#左對齊
print(format("你好","^20"))#居中
#                   你好
# 你好
#          你好
#enumerate()#枚舉
lst=[12,4,56,8,7565]
for i in enumerate(lst):
    print(i)
# (0, 12)
# (1, 4)
# (2, 56)
# (3, 8)
# (4, 7565)

for i ,c in enumerate(lst):
    print(i,c)
# 0 12
# 1 4
# 2 56
# 3 8
# 4 7565

for i ,c in enumerate(lst,100):#默認起始位置爲0,設置數數的起始位置
    print(i,c)
# 100 12
# 101 4
# 102 56
# 103 8
# 104 7565

print(sum(lst))#7645,sum裏面必須爲list,計算總和


lst1=[1,2,3,4,45,5]
lst2=[1,1,1,1,1]
print(zip(lst1,lst2))#<zip object at 0x000001F32A96BF08>返回了一個對象
print(list(zip(lst1,lst2)))#[(1, 1), (2, 1), (3, 1), (4, 1), (45, 1)]
print(dict(zip(lst1,lst2)))#{1: 1, 2: 1, 3: 1, 4: 1, 45: 1}字典的創建方式
d=dict(a=1,b=2,c=3)#字典的創建方式
print(d)#{'a': 1, 'b': 2, 'c': 3}
lst1=[1,1,1,1,1]
lst2=[2,2,2,2]
lst3=[3,3,3,3]
lst4=[4,4,4,4]
print(list(zip(lst1,lst2,lst3,lst4)))#[(1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 4)]
print(dir(str))

print (1,"你好",sep="++",end="")#sep連接,end結束
print(2,"你好",sep="__")
#1++你好2__你好
print("大家好",file=open("day9_test","a",encoding="utf-8"))#將文字寫到day9_test文件中

print(list(reversed("你好啊")))#['啊', '好', '你']

lst=[1,2,35,6]
lst1=list(reversed(lst))#開闢新的空間,元數據不變
print(lst1)#[6, 35, 2, 1]
print(lst)#[1, 2, 35, 6]
#注意  list(reversed(lst))  與 lst.reverse() 的區別
lst.reverse()#在原地進行修改
print(lst)#[6, 35, 2, 1]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章