3.运算符和表达式

本节实验中将学习运算符和表达式的基本用法。

一、实验目的

  • 关系/逻辑运算
  • 表达式
  • 类型转换

二、知识要点

1.算术运算
  • 算术运算符包括+(加),-(减),*(乘),/(除),%(取余),**(求幂),//(向下取整)

  • 例如:

1
2
3
4
5
6
7
8
9
a, b = 2, 9
print("a + b =", a + b)
print("a - b =", a - b)
print("a * b =", a * b)
print("b / a =", b / a)
print("b % a =", b % a)
print("b ** a =", b ** a)
print("b // a =", b // a)
print("-b // a =", -b // a)
  • 结果为:
1
2
3
4
5
6
7
8
a + b = 11
a - b = -7
a * b = 18
b / a = 4.5
b % a = 1
b ** a = 81
b // a = 4
-b // a = -5
  • 有关解释如下表:
操作 解释
求幂 b**a即求ba次方即92次方为81
取余 整除之后得到的余数,例如9/2=4···1则取余的结果即为1
向下取整 即选取比进行除法运算得到的数字小且最接近该数字的整数,代码中9/2=4.5因此选择4,而-9/2=-4.5因此选择-5
2.比较运算符
点击显/隐表格
运算符 描述 实例
== 等于 - 比较对象是否相等 (a == b) 返回 False。
!= 不等于 - 比较两个对象是否不相等 (a != b) 返回 true.
> 大于 - 返回x是否大于y (a > b) 返回 False。
< 小于 - 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。 (a < b) 返回 true。
>= 大于等于 - 返回x是否大于等于y。 (a >= b) 返回 False。
<= 小于等于 - 返回x是否小于等于y。 (a <= b) 返回 true。
3.赋值运算符
点击显/隐表格
运算符 描述 实例
= 简单的赋值运算符 c = a + b 将 a + b 的运算结果赋值为 c
+= 加法赋值运算符 c += a 等效于 c = c + a
-= 减法赋值运算符 c -= a 等效于 c = c - a
*= 乘法赋值运算符 c = a 等效于 c = c a
/= 除法赋值运算符 c /= a 等效于 c = c / a
%= 取模赋值运算符 c %= a 等效于 c = c % a
**= 幂赋值运算符 c **= a 等效于 c = c ** a
//= 取整除赋值运算符 c //= a 等效于 c = c // a
4.位运算符
点击显/隐表格
操作 解释
按位与运算符& 参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0
按位或运算符 只要对应的二个二进位有一个为1时,结果位就为1。
按位异或运算符^ 当两对应的二进位相异时,结果为1
按位取反运算符~ 对数据的每个二进制位取反,即把1变为0,把0变为1 。~x 类似于 -x-1
左移动运算符<< 运算数的各二进位全部左移若干位,由 << 右边的数字指定了移动的位数,高位丢弃,低位补0
右移动运算符>> >>左边的运算数的各二进位全部右移若干位,>> 右边的数字指定了移动的位数
  • 例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
a = 10         # 10 = 0000 1010
b = 80 # 80 = 0101 0000
c = a & b # 00 = 0000 0000
print("a&b value is :", c)
d = a | b # 90 = 0101 1010
print("a|b value is :", d)
e = a ^ b # 90 = 0101 1010
print("a^b value is :", e)
f = ~ a # -11 = 1000 1011
print("~a value is :", f)
g = a << 2 # 40 = 0010 1000
print("a<<2 value is :", g)
h = a >> 2 # 2 = 0000 0010
print("a>>2 value is :", h)
  • 结果为:
1
2
3
4
5
6
a&b value is : 0
a|b value is : 90
a^b value is : 90
~a value is : -11
a<<2 value is : 40
a>>2 value is : 2
5.逻辑运算符
逻辑表达式 描述
x and y 布尔”与” - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。
x or y 布尔”或” - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。
not x 布尔”非” - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。
  • 例如:
1
2
3
4
5
6
7
8
9
10
a = 10
b = 20
c = 0

print("a and b =", a and b)
print("c and b =", c and b)
print("a or b =", a or b)
print("c or b =", c or b)
print("not a =", not a)
print("not c =", not c)
  • 结果为:
1
2
3
4
5
6
a and b = 20
c and b = 0
a or b = 10
c or b = 20
not a = False
not c = True
6.成员运算符
运算符 描述 实例
in 如果在指定的序列中找到值返回 True,否则返回 False。 x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in 如果在指定的序列中没有找到值返回 True,否则返回 False。 x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。
  • 例如:
1
2
3
4
5
6
7
a = "hello world"
b = "llo"
c = "oll"
print("b in a is :", b in a)
print("b not in a is :", b not in a)
print("c in a is :", c in a)
print("c not in a is :", c not in a)
  • 结果为:
1
2
3
4
b in a is : True
b not in a is : False
c in a is : False
c not in a is : True
7.身份运算符
运算符 描述 实例
is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is not is not 是判断两个标识符是不是引用自不同对象 x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。
  • 例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 40, 50]
list3 = list1

print("Please judge the following proposition:")
print("list1 is list2 :", list2 is list1)
print("list3 is list1 :", list3 is list1)
print("list1 is not list2 :", list2 is not list1)
print("list3 is not list1 :", list3 is not list1)
print("list1 == list2 :", list2 == list1)
print("list3 == list1 :", list3 == list1)
print("list1 != list2 :", list2 != list1)
print("list3 != list1 :", list3 != list1)
  • 结果是:
1
2
3
4
5
6
7
8
9
Please judge the following proposition:
list1 is list2 : False
list3 is list1 : True
list1 is not list2 : True
list3 is not list1 : False
list1 == list2 : True
list3 == list1 : True
list1 != list2 : False
list3 != list1 : False

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

8.运算符优先级
点击显/隐表格
运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 ‘AND’
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= =** 赋值运算符
is 、is not 身份运算符
in 、not in 成员运算符
not and or 逻辑运算符

三、实验内容

1.书写表达式

通常我们书写表达式的时候,会在每一个运算符左右都放一个空格,这样使代码更可读,如:

1
a = 234 * (45 - 56 / 34)
2.类型转换
类型转换函数 转换路径
float(string) 字符串 -> 浮点值
int(string) 字符串 -> 整数值
str(integer) 整数值 -> 字符串
str(float) 浮点值 -> 字符串
  • 例如:
1
2
3
4
5
6
7
8
9
10
11
12
a = 1
print("type a is :", type(a))
a = float(a)
print("after float(a) type a is :", type(a))
b = 2.0
print("type b is :", type(b))
b = int(b)
print("after int(b) type b is :", type(b))
c = "1"
print("type c is :", type(c))
c = int(c)
print("after int(c) type c is :", type(c))
  • 结果为:
1
2
3
4
5
6
type a is : <class 'int'>
after float(a) type a is : <class 'float'>
type b is : <class 'float'>
after int(b) type b is : <class 'int'>
type c is : <class 'str'>
after int(c) type c is : <class 'int'>

type()函数:传入一个参数时,返回 object 的类型,返回值是一个 type 对象。

四、实验结果

1.evaluateequ.py

使用Python编辑一个程序用于计算如下表达式结果:

其中xn的值为整型且需要用户输入,输出结果保留四位小数。

  • 代码:
1
2
3
4
5
6
X = int(input("Please input an integer X :"))
N = int(input("Please input an integer N :"))
SUM = 0
for X in range(1, N + 1):
SUM += 1 / X
print("The SUM is :", "{:.4f}".format(SUM))
  • 结果:
1
2
3
Please input an integer X :1
Please input an integer N :10
The SUM is : 2.9290
  • 函数说明:

range()函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。例如:

1
2
range(stop)
range(start, stop[, step])

其中:

  • start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
  • stop:计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
  • step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
    拓展用法:
    list() 函数是对象迭代器,可以把range()返回的可迭代对象转为一个列表,返回的变量类型为列表。例如:
1
2
a = list(range(0, 30, 5))
print(a)

输出结果为:[0, 5, 10, 15, 20, 25]

2.quadraticequation.py

用户输入a,b,c后输出二次方根root1,root2

  • 代码:
1
2
3
4
5
6
7
8
9
10
import math
a = int(input("Please input a :"))
b = int(input("Please input b :"))
c = int(input("Please input c :"))
D = b * b - 4 * a * c
if D < 0:
print("ROOTS are imaginary")
else:
print("root1=", (-b + math.sqrt(D)) / (2 * a))
print("root2=", (-b - math.sqrt(D)) / (2 * a))
  • 结果:
1
2
3
4
5
6
7
8
9
10
Please input a :5
Please input b :-4
Please input c :0
root1= 0.8
root2= 0.0

Please input a :5
Please input b :4
Please input c :1
ROOTS are imaginary

Math模块常用函数:简书

3.salesmansalary.py

这个程序计算一位数码相机销售人员的工资。他的基本工资是 1500,每售出一台相机他可以得到 200 并且获得 2% 的抽成。程序要求输入相机数量及单价。

  • 代码:
1
2
3
count = int(input("Please input the number of cameras:"))
price = float(input("Please input the price of cameras:"))
print("The salary is:", 1500+200*count + 0.02*count*price)
  • 结果:
1
2
3
Please input the number of cameras:5
Please input the price of cameras:2000
The salary is: 2700.0
4.areaofacircle.py

输入圆的半径R,输出圆的面积

  • 代码
1
2
3
import math
R = float(input("Please input the radius:"))
print("The area of the circle is:", "{:.10f}".format(math.pi*R*R))
  • 结果
1
2
Please input the radius:2
The area of the circle is: 12.5663706144

下一篇:4.控制流if-else
上一篇:2.变量和数据类型
目 录:Python学习

本文标题:3.运算符和表达式

文章作者:小哲

发布时间:2020年02月29日 - 21:13

最后更新:2020年03月30日 - 11:20

原始链接: 点击复制原始链接

许可协议: 协议-转载请保留原文链接及作者