python - 循环

本文详细介绍了Python中的while和for循环,展示了如何使用它们进行基本操作,如计数、获取最大公约数和打印乘法表,同时涉及了条件语句和函数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


count = 0
while count < 10:
    print("hello, python")
    count += 1 #需要有使循环停止的条件,所以这里让count每进行一次都加1

runfile('C:/pycode/while.py', wdir='C:/pycode')
hello, python
hello, python
hello, python
hello, python
hello, python
hello, python
hello, python
hello, python
hello, python
hello, python
#获取最大公约数的脚本
x, y = eval(input("enter two numbers: ")) #获取两个数字,定义两个变量,按格式输入
gys = 1
try1 = 1
while try1 <= x and try1 <= y:  #尝试的范围不大于任何一个数字
    if x % try1 == 0 and y % try1 == 0:  #两个数字整除尝试的数字都为0
        gys = try1
    try1 += 1  #给一个停止的条件
print (gys)

runfile('C:/pycode/gys.py', wdir='C:/pycode')
enter two numbers: 128, 8000
64
sum = 0
for i in range(1, 11):  #for语句,循环的范围在in后,范围的值一个个放进变量i里运行
    sum += i
    
print(sum)

In [39]: a = [1, 2, 3] #范围可以是list 或者 tuple

In [40]: for i in a:
    ...:     print(i)
    ...:
1
2
3

In [41]: a = (1, 2, 3)

In [42]: for i in a:
    ...:     print(i)
    ...:
1
2
3
In [43]: for i in range(1,6):
    ...:     print(i)  #打印一次
    ...:     if i == 3:  #达到条件会跳出循环
    ...:         break
    ...:     else:
    ...:         print(i)  #不达到条件打印第两次
    ...:
1
1
2
2
3

for i in range(1, 6):
    print(i, end = ' ')
    if i == 3:
        continue  #跳出本次循环,4继续运行
    print(i)

runfile('C:/pycode/continue.py', wdir='C:/pycode')
1 1
2 2
3 4 4
5 5

import sys
for i in range(1, 6):
    print(i, end = ' ')
    if i == 3:
        sys.exit ()  #退出整个程序,break会跳出循环,还会执行循环外的代码
    print(i)

runfile('C:/pycode/continue.py', wdir='C:/pycode')
1 1
2 2
3 
for i in range(1,10):
    for j in (range(1, i+1)):
        print(i,"*",j,"=",i*j,end=' ')
    print()

    runfile('C:/pycode/chengfa.py', wdir='C:/pycode')
1 * 1 = 1 
2 * 1 = 2 2 * 2 = 4 
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81  
print('sushu as below:') #识另素数,
s = 1
while s < 100: 
    #print("now is work for",s)
    js = 0 #主要就是计数,能整除1-s这些元素的次数,素数<3,其他最多为2
    f = 1
    while f<=s: 
        if (s%f ==0):
            js +=1
        f +=1  
    if js <3:            
        print(s, end=',')
    s += 1
    
print()

for s in range(1,101):  #用for语句写一次
    js = 0
    f = 1
    for f in range(1,s+1):
        if (s%f==0):
            js+=1
    #print(js)
    if js<3:
        print(s,end=',')

runfile('C:/pycode/sushu.py', wdir='C:/pycode')
sushu as below:
1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值