CS61A Python---Bool/if/循环
Boolean value : True / False
and
or
not
if 语句
if <condition>:
<statement>
<statement>
-------
if <condition>:
<statement>
elif <condition>:
<statement>
else :
<statement>
e.g
质数检验
def is_prime(n): """Return True iff N is prime. >>> is_prime(1) False >>> is_prime(2) True >>> is_prime(8) False >>> is_prime(21) False >>> is_prime(23) True """ return n > 1 and smallest_factor(n) == n def smallest_factor(n): """Returns the smallest value k>1 that evenly divides N.""" x = 2 while x * x <= n : if n % x == 0 : return x x += 1 return n pass def print_factors(n): x = 2 while n != 1 : if(n % x == 0) : n/=x print (x) else : x += 1 """Print the prime factors of N. >>> print_factors(180) 2 2 3 3 5 """ pass
while循环
while <condition>: <statement> <statement>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构