从零开始的python学习(二)P23+P24

本文章记录观看B站python教程学习笔记和实践感悟,视频链接:【花了2万多买的Python教程全套,现在分享给大家,入门到精通(Python全栈开发教程)】 https://www.bilibili.com/video/BV1wD4y1o7AS/?p=6&share_source=copy_web&vd_source=404581381724503685cb98601d6706fb 

上节课学习了四种运算符(算数运算符、赋值运算符、比较运算符与逻辑运算符),本节课学习

 位运算符和运算符的优先级,以及本章总结和习题。

1.位运算符和运算符的优先级

位运算符:把数字看做二进制数来进行计算的,先把数字化成二进制数,再进行“位与”运算或者“位或”运算等。本知识点了解即可。

下为按 “位与”运算和按“位或”运算

前者转换后将两个数上下对齐,“位与”就是上下对齐取“与”,即必须都是1才能取1,否则取0;

“位或”就是上下取“或”,只有两个都是0才取0,否则取1.

下面是按“位异或”运算和按“位取反”运算

前者转换后两个数上下对齐,看看是否一样,一样就取0不一样就取1;

后者,原来为0就取1,原来是1就取0.

下面是四种运算的例子:

print('按位与运算',12&8)
print('按位或运算',4|8)
print('按位异或运算',31^22)
print('按位取反',~123)

结果如下:

 可以发现转化成二进制是电脑做的工作,实际上输入和输出都是十进制的数字。

下面是“左移位”运算符(<<)

它是将一个二进制数向左移动指定的位数,左边(高位端)溢出的位被丢弃,右边(低位端)的空位用0补充。下面是2的二进制向左移动两位的效果。

代码和结果如下:

print('左移位:',2<<2) #结果是8,这行代码表达的是将2的二进制向左移动2位
print('左移位:',2<<3) #结果是16,这行代码表达的是将2的二进制向左移动2位,相当于2*2*2*2

“右移位”运算符

“右移位”运算(>>)

是将一个二进制数向右移动指定的位数,右边(低位端)溢出的位被丢弃,左边(高位端)的空位端,如果最高位是0(正数)左侧空位填0,如果最高位是1(负数),左侧空位填1.

下面是8右移位两位得到2的过程:

代码如下:

print('右移位:',8>>2) #结果是2,这行代码是将8的二进制向右移动2位,相当于8//2,4//2
print('右移位:',-8>>2) #结果是-2,这行代码是将-8的二进制向右移动2位,相当于-8//2,-4//2

 最后回顾一下优先级,这里省略了括号,它一定优先于这个表格,表格内的从上到下优先级递减:

 2.本章总结

 本章学习了保留字和标识符,变量的语法结构,常用的数值处理函数和运算符

 十六进制的题目:

0xf2的计算方法是从右向左分别是16的0次方,16的1次方,16的2次方......这里0x表示16进制,所以应该计算成f乘16的1次方加上2乘16的0次方,f表示15,代入得到242.

表示四位数的题目:

有两种方法:

#法一
num=eval(input('请输入一个四位整数:'))
print('个位上的数:',num%10) #1234除以10取余数
print('十位上的数:',num//10%10) #1234除以10得到整数123再除以10取余数
print('百位上的数:',num//1000) #1234除以1000取整得到1

#法二
print('-'*40)  #40个字符串-
num=input('请输入一个四位的整数:') #此时,num是一个字符串
print('个位上的数:',num[3]) #对这个字符串进行索引,索引序号从左到右从0开始
print('十位上的数:',num[2])
print('百位上的数:',num[1])
print('千位上的数:',num[0])

 结果如下:

 下一个题目关于数值转换问题:

father_height=eval(input('请输入爸爸的身高:'))
mother_height=eval(input('请输入妈妈的身高:'))
son_height=(father_height+mother_height)*0.54
print('预测儿子的身高:',son_height)

结果如下:

为了保留两位 小数,稍作修改:

father_height=eval(input('请输入爸爸的身高:'))
mother_height=eval(input('请输入妈妈的身高:'))
son_height=(father_height+mother_height)*0.54
son_height=round(son_height,2)
print('预测儿子的身高:',son_height)

 结果如下:

本节完 

在上一题Point2D和Point3D类的基础上,新建一个TestPointV2类,在TestPointV2类的main()方法中添加如下语句。 Scanner sc = new Scanner(System.in); System.out.println("Please enter the coordinates of p23:"); double p23x = sc.nextDouble(); double p23y = sc.nextDouble(); Point2D p23 = new Point2D(p23x, p23y); System.out.println("Please enter the coordinates of p31:"); double p31x = sc.nextDouble(); double p31y = sc.nextDouble(); double p31z = sc.nextDouble(); Point3D p33 = new Point3D(p31x, p31y, p31z); System.out.println("Please enter the coordinates of p24:"); double p24x = sc.nextDouble(); double p24y = sc.nextDouble(); double p24z = sc.nextDouble(); sc.close(); // The reference of the parent class refers to the object of the subclass. Point2D p24 = new Point3D(p24x, p24y, p24z); System.out.println("Does " + p23 + " coincide with " + p33 + "? -- "+ p23.equals(p33)); System.out.println("Does " + p33 + " coincide with " + p23 + "? -- "+ p33.equals(p23)); System.out.println("Does " + p33 + " coincide with " + p24 + "? -- "+ p33.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p33 + "? -- "+ p24.equals(p33)); System.out.println("Does " + p23 + " coincide with " + p24 + "? -- "+ p23.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p23 + "? -- "+ p24.equals(p23)); 假设引用变量p23、p33和p24所指点对象的坐标依次为(0, 0)(0, 0, 5)(0, 0, 5)。从键盘输入这三个点的坐标,上述语句的运行结果如下: Please enter the coordinates of p23: 0 0 Please enter the coordinates of p31: 0 0 5 Please enter the coordinates of p24: 0 0 5 Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true 该结果显然不符合事实,请分析原因并改进Point2D类的代码,使得上述TestPointV2类的代码能够得到正确的运行结果。
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值