格式化的三种输出方式和基本运算符以及他们的优先级

格式化输出的三种方式

一、占位符

使用格式化输出的三种方式

name = 'wenbin'
height = 180
weight = 160

1.普通连接

x1,x2,x3 = name,height,weight
print("my name is "+x1+",my height is "+str(x2)+",my weight is "+str(x3))
print("my name is "+name+",my height is "+str(weight)+",my weight is "+str(height))

2.占位符(%s针对所有类型,不需要强制类型转换。%d仅仅针对数字类型)

print("my name is %s,my hight %s,my weight is %s "%(name,height,weight))

二、format格式化

print('my name is {},my weight is {},my hight is {}'.format(name,weight,height))
print('my name is {0},my weight is {1},my hight is {2}'.format(name,weight,height))

三、f-String格式化 (相比占位符的方式,python3版本增加了f-String格式化的方式,比较简单易懂,这是我目前使用最多的方式,推荐这种方式。)

print(f'my name is {name},my weight is {weight},my herght is {height}')

注:大写的F也适用

print(F'my name is {name},my weight is {weight},my herght is {height}')

也可以输出精度

print(f'my weight is {weight:4f}')

基本运算符

一算术运算符

img

二、比较运算符

img

三、赋值运算符

029-基本运算符-赋值运算符.jpg?x-oss-process=style/watermark

四、逻辑运算符

029-基本运算符-逻辑运算符.jpg?x-oss-process=style/watermark

# 从左到右的方式找到逻辑运算符,找到逻辑运算符的左边,左边成立,再去找到逻辑运算符的右边
print(3 > 3 and 1 > 2 or 2 > 1)  # False

五、身份运算符

029-基本运算符-身份运算符.jpg?x-oss-process=style/watermark

is和==的区别:is用于判断两个变量引用对象是否为同一个(是否在同一块内存空间中), ==用于判断引用变量的值是否相等。

六、位运算符

按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:

032-基本运算符-位运算符.jpg?x-oss-process=style/watermark

七、成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

032-基本运算符-成员运算符.jpg?x-oss-process=style/watermark

python运算符的优先级

python运算符的优先级相当于数学中的先算乘除再算加减,但是这很愚蠢,优先级高的你括号括起来就行了...

029-基本运算符-python运算符优先级.jpg?x-oss-process=style/watermark

posted @ 2019-08-01 20:32  旧时光清风  阅读(515)  评论(0编辑  收藏  举报