四则运算
Python里的整数、浮点数和数学意义的整数、小数是一样的,它最重要的作用是与运算符号结合,进行数学计算。
但是Python世界的算数运算符,和我们平时在纸上写的运算符号有相同点,也有不同点。

Python世界的运算优先级,和我们平时的计算优先级是一样的。

1 2 |
print(499*561+10620-365) print((5025-525)/100+18*17) |
1 2 3 |
#运行结果: print(499*561+10620-365) print((5025-525)/100+18*17) |
字符串的拼接
1 2 3 4 5 6 7 8 9 10 11 12 |
hero = '我' organization = '云计算' identity = '工程师' action = '是' ID = 'PlacerGold' print(hero+action+organization+identity+ID) print(organization+identity+ID+action+hero) # 输出结果 我是云计算工程师PlacerGold 云计算工程师PlacerGold是我 |
利用字符串拼接符号【+】,将需要拼接的变量连在一起就行了。

错误代码:
1 2 3 4 5 6 7 8 |
who = '我的' action = '是' destination = '镜像世界' number = 1023 code = '通行密码' print(who+destination+code+action+number) |
梦想是美好的,但现实是残酷的,我们立马被报错【TypeError:can only concatenate str (not “int”) to str】(类型错误:只能将字符串与字符串拼接)无尽地呻吟。
当你无法识别数据类型时,可以用type()函数。
数据类型的查询——type()函数
只需把查询的内容放在括号里就行。

1 2 3 4 5 6 7 8 9 10 11 12 13 |
who = '我的' action = '是' destination = '镜像世界' number = 153 code = '通行密码' type(who) type(action) type(destination) type(number) type(code) #只查询,不打印,输出无显示 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
who = '我的' action = '是' destination = '镜像世界' number = 153 code = '通行密码' print(type(who)) print(type(action)) print(type(destination)) print(type(number)) print(type(code)) #运用print()函数,将查询函数的结果打印出来 #输出结果: <class 'str'> <class 'str'> <class 'str'> <class 'int'> <class 'str'> |