Python 3 學習筆記之 資料型別

2021-09-24 08:52:18 字數 1802 閱讀 2173

型別

運算子

a = 'hello'

b = 'seniusen'

a + b # 字串拼接 'helloseniusen'

a * 2

# 重複輸出字串 'hellohello'

# 字串格式化輸出

print(repr(3).rjust(2), repr(16).rjust(3)) # 靠右對齊,ljust()、center() 靠左、居中對齊

print('12'.zfill(5)) # '000123',在數字的左邊填充 0

print('my name is %s, my lucky number is %d.' %('seniusen', 3))

print('my name is {}, my lucky number is {}.'.format('seniusen', 3))

# my name is seniusen, my lucky number is 3.

print('站點列表 , , 和 。'.format('google', 'runoob', other='taobao'))

# 站點列表 google, runoob, 和 taobao。

print('常量 pi 的值近似為:%5.3f。' % 3.1415926)

print('常量 pi 的值近似為:。'.format(3.1415926))

# 在 ':' 後傳入乙個整數, 可以保證該域至少有這麼多的寬度, .3 表示浮點數保留 3 位小數

print('常量 pi 的值近似為: 。'.format(3.1415926)) # 相當於 repr()

print('常量 pi 的值近似為: 。'.format(3.1415926)) # 相當於 str()

複製**

a = () # 新建乙個空元組

a = (2, ) # 新建乙個只有乙個元素的元組

a = (2) # 此時 a 為 int 型別

(1, 2, 3) + (4, 5, 6) # (1, 2, 3, 4, 5, 6)

(1, 2, 3) * 2

# (1, 2, 3, 1, 2, 3)

複製**

a = {} # 新建乙個空字典

>>> a =

>>> a.keys() # 字典的鍵

dict_keys(['name', 'age'])

>>> a.values() # 字典的值

dict_values(['seniusen', 21])

>>> a.items() # 字典的項

dict_items([('name', 'seniusen'), ('age', 21)])

>>> list(a.keys())

['name', 'age']

複製**

a = set() # 新建乙個空集合

>>> b = set('defgh')

>>> b

>>> a = set('abcde')

>>> a

>>> a - b # 只在 a 中不在 b 中的元素

>>> a & b # 既在 a 中又在 b 中的元素,交集

>>> a | b # 在 a 和 b 中的所有的元素,並集

>>> a ^ b # 只在 a 中或只在 b 中的元素

複製**

參考資料 菜鳥教程

Python 3 學習筆記之 資料型別

型別 運算子 a hello b seniusen a b 字串拼接 helloseniusen a 2 重複輸出字串 hellohello 字串格式化輸出 print repr 3 rjust 2 repr 16 rjust 3 靠右對齊,ljust center 靠左 居中對齊 print 12...

python3學習筆記之安裝

一 python安裝 1 安裝python之前需安裝所需依賴模組 1 yum y install zlib zlib devel 2 yum y install bzip2 bzip2 devel 3 yum y install ncurses ncurses devel 4 yum y insta...

Python3學習筆記

最近在起步學python,聚合一下這個過程中蒐集的資源和對一些基本知識做個小總結,語法基於python3,方便以後查詢。python官方文件 不錯的基礎課程 基本語法 演算法 建模 練習 以下是整理常用可能遺忘的基礎點 python3中的輸入是input 獲得使用者輸入的字串 a input ple...