一、占位符

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

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}')

基本运算符

一算术运算符

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

二、比较运算符

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

三、赋值运算符

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

四、逻辑运算符

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

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

五、身份运算符

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

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

六、位运算符

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

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

七、成员运算符

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

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

python运算符的优先级

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

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

相关文章:

  • 2021-10-28
  • 2021-10-30
  • 2022-02-16
  • 2021-11-18
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2021-09-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2021-06-24
  • 2021-05-28
  • 2022-12-23
相关资源
相似解决方案