第006讲:Python之常用操作符
2021/5/19 12:55:15
本文主要是介绍第006讲:Python之常用操作符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
第006讲:Python之常用操作符
测试题
0. Python 的 floor 除法现在使用 “//” 实现,那 3.0 // 2.0 您目测会显示什么内容呢?
>>> 3.0//2.0 1.0
1. a < b < c 事实上是等于?
# 等同于 a < b and b < c
2. 不使用 IDLE,你可以轻松说出 5 ** -2 的值吗?
# 0.04,1/25 # 幂运算操作符比其左侧的一元操作符优先级高,比其右侧的一元操作符优先级低。
3. 如何简单判断一个数是奇数还是偶数?
# 能被2整除的都是偶数 number = input('输入一个整数:') number_int = int(number) if number_int % 2 == 0: print(number + '是偶数') else: print(number + '是奇数')
4. 请用最快速度说出答案:not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
>>> (not 1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9) # 逻辑顺序 not > and > or >>> (not 1) or (0 or 4) or (6 or 9) # 之所以是0 是因为and逻辑如果两边都是真,则返回第二个值,如果两边有一个是假,则返回假,而1表示真,0表示假,大于0的任何数都是真 >>> (not 1) or (4 or 9) >>> (not 1) or 4 >>> 4
5. 还记得我们上节课那个求闰年的作业吗?如果还没有学到“求余”操作,还记得用什么方法可以“委曲求全”代替“%”的功能呢?
# 因为005讲写的程序没用到求于的运算符,所以这里用 % 运算符写一个 # 这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年。 years = input('请输入一个年份:') while not years.isdigit(): years = input('输入的不是整数,请重新输入:') years_int = int(years) if years_int % 400 == 0: print(years + '是闰年') else: if (years_int % 4 == 0) and (years_int % 100 != 0): print(years + '是闰年') else: print(years + '不是闰年')
动动手
0. 请写一个程序打印出 0~100 所有的奇数。
# while 方法 number = 0 while number < 101: if number%2 != 0 : print (number) number += 1 # 是就继续下一个 else: number += 1 # 不是就跳过
# for 方法 for number in range(0,100): if number % 2 != 0 : print(i)
1. 我们说过现在的 Python 可以计算很大很大的数据,但是......真正的大数据计算可是要靠刚刚的硬件滴,不妨写一个小代码,让你的计算机为之崩溃?
# 慎用 打开任务管理器 看好内存,不行就关掉 2**2**10000 '''我是虚拟机里运行,虚拟机内存6G,物理机剩余10G,允许内存交换,差点物理机内存都爆了 解释下,幂运算就是个无底洞,况且 上面的算法按照逻辑,是先算2**10000 '''
2. 爱因斯坦的难题
爱因斯坦曾出过这样一道有趣的数学题:有一个长阶梯,若每步上2阶,最后剩1阶;若每步上3阶,最后剩2阶;若每步上5阶,最后剩4阶;若每步上6阶,最后剩5阶;只有每步上7阶,最后刚好一阶也不剩。 (小甲鱼温馨提示:步子太大真的容易扯着蛋~~~) 题目:请编程求解该阶梯至少有多少阶?
# 理解为除以2余数1,以此类推 for number in range(0,1000): if number%2==1 and number%3==2 and number%5==4 and number%6==5 and number%7==0: #同时满足这些逻辑条件 print(number)
这篇关于第006讲:Python之常用操作符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python