实例一:

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),也可以直接变量+引号数值进行拼接

 

 

其次,float()函数也可以将整数和字符串转换为浮点类型。但同时,如果括号里面的数据是字符串类型,那这个数据一定得是数字形式。

转换为浮典数:

height = 183.5
weight = 79
age = '30'


print(float(height))
print(float(weight))
print(float(age))
结果:
height = 183.5
weight = 79
age = '30'


print(float(height))
print(float(weight))
print(float(age))

实例:

word = '3.8'
number = 1
sentence = '人工智障说:3.8+1等于'

print(sentence+str(int(float(word)+number)))

结果:

人工智障说:3.8+1等于4



要求:请运用所给变量,使用数据转换str()、int()、float()及数据拼接符号+,打印一句话: 脸黑怪我咯7张蓝票一个SSR都没有
其中,变量会在【书写代码】步骤里直接提供。

slogan = '脸黑怪我咯'
number = '7.8'
unit = '张'
sentence = '蓝票一个SSR都没有'
print(slogan+str(int(float(number)))+unit+sentence)

 

结果:

slogan = '脸黑怪我咯'
number = '7.8'
unit = '张'
sentence = '蓝票一个SSR都没有'
print(slogan+str(int(float(number)))+unit+sentence)