def factors1(num,fac=[]):#因式分解
for i in range(2,int(num**0.5)+1):
if num%i == 0:
fac.append(i)
factors1(num/i,fac)
break;#break会使得, else之中只保留最后剩下的结果。
else :#这个else真的有必要?
fac.append(int(num))
Fac = []
Num = 2048
Ans = factors1(Num,Fac)
print(Fac)
#Answer:[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
#我对于此代码的时间复杂度表示怀疑,递归调用毫无意义
def factors2(num,fac=[]):#因式分解
for i in range(2,int(num**0.5)+1):
if num % i == 0:
for j in range(): # for 实现方式
if num % i == 0:
fac.append(i)
num = num / i;
else:
break
if num != 0:
fac.append(num)
Fac = []
Num = 2048
Ans = factors1(Num,Fac)
print(Fac)
#Answer:[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] , for的效率将更高
from time import *
from datetime import *
#输出时间
date = localtime()
year, month, day = date[:3]
print(year,month,day)
#range(start, stop, step) 起始点,终结点,步长
#range(5) 默认表示从0开始的五个元素,0 1 2 3 4
import random
import string
#循环
digits = {1,2,3,4}
for i in range(1,1000):
result = []
for i in digits:
for j in digits:
for k in digits:
result.append(i * 100 + j * 10 + k)
for i in range(len(result)): #len的时间复杂度为O(1)
print(result[i])
#python 局部变量查询的速度要快于全局变量的查询速度
#条件运算符
a = 10
b = 6 if a > 13 else 9
print(b)
flag = 3 or 4
print(flag)
flag = 3 | 4
print(flag)
#Answer:
#3
#7
flag = 5 and 7
print(flag)
flag = 5 & 7
print(flag)
flag = 5 and 0
print(flag)
#Answer
#7
#5
#0
flag = True
if flag == True:
print(3)
print(5)#在一行写入多个语句使用;分割
if flag == True:
print(4)
print(6);#效果同上
#exit(0)
Python 基本数据类型基础实战【函数】
最新推荐文章于 2022-11-04 18:48:15 发布