1. 程式人生 > >python 類型的使用

python 類型的使用

李白 ash http 結果 fir char 字符 int har

實例一:

print(499*561+10620-365)
print((5025-525)/100+18*17)

結果:

print(499*561+10620-365)
print((5025-525)/100+18*17)

技術分享圖片

實例二:浮點數

print(0.55+0.3)

結果:

bash:81$ python ~/classroom/apps-1-id-5c3d88f18939b4000100e7d9/81/main.py
0.8500000000000001

技術分享圖片 技術分享圖片

實例三:內容拼接

hero1 = ‘亞瑟‘
hero2 = ‘李白‘
action = ‘秒掉‘
gain = ‘獲得‘
achieve = ‘First Blood‘
print(hero1+action+hero2+gain+achieve)
print(hero2+action+hero1+gain+achieve)

結果:

亞瑟秒掉李白獲得First Blood
李白秒掉亞瑟獲得First Blood

實例四:顯示內容的類型

hero = ‘亞瑟‘
enemy = ‘敵方‘
action = ‘秒殺‘
gain = ‘獲得‘
number = 5
achieve = ‘Penta Kill‘

print(type(hero))
print(type(enemy))
print(type(action))
print(type(gain))
print(type(number))
print(type(achieve))

結果:

<class ‘str‘>
<class ‘str‘>
<class ‘str‘>
<class ‘str‘>
<class ‘int‘>
<class ‘str‘>

實例五:數據類型轉換方法

技術分享圖片

hero = ‘亞瑟‘
enemy = ‘敵方‘
action = ‘秒殺‘
gain = ‘獲得‘
number = 5
achieve = ‘Penta Kill‘

print(hero+action+str(number)+enemy+gain+achieve)

將字符串與數值拼接,使用str()

結果:

亞瑟秒殺5敵方獲得Penta Kill

print(slogan+‘10000‘+unit+character+place+action),也可以直接變量+引號數值進行拼接

python 類型的使用