python learning 4-built-in function range(), loop structure, loop control statement, else statement, nested loop

1. The built-in function range() is used to generate an integer sequence. The return value is an iterator object.

Three ways to create range()

1. The first creation method has only one parameter (stop)

r=range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],默认从0开始,默认相差1步长
print(r) #range(0, 10)
print(list(r)) #用于查看range对象中的整数序列

2. The second creation method has two parameters (start, stop)

r=range(1,10)  #指定了起始值,从1开始,到10结束(不包含10),默认步长为1
print(list(r)) #[1, 2, 3, 4, 5, 6, 7, 8, 9]

3. The third creation method has three parameters (start, stop, step)

r=range(1,10,2)  #指定了起始值、结束值以及步长
print(list(r)) #[1, 3, 5, 7, 9]

Determine whether the specified integer exists in the sequence in, not in

print(10 in r) #False
print(10 not in r) #True

2. Loop structure while

a=0
sum=0
while a<5:
    sum+=a
    a+=1
print(sum)

Simple exercise in while loop (calculate the sum of even numbers between 1-100)

a=1
sum=0
while a<=100:
    if a%2==0:    #如果这里是a%2 算出来的结果就是奇数
        sum+=a
    a+=1
print(sum)

3. Loop structure for-in

The object traversed by for-in must be an iterable object for custom variable in iterable object: loop body

for i in range(10):
    print(i)

If you do not need a custom variable in the loop body, you can write the custom variable as "_"

for _ in range(5):
    print("python真棒!")

Use a for loop to calculate the sum of even numbers from 0 to 100

sum=0
for i in range(101):
    if i%2==0:
        sum+=i
print(sum)

for-in exercises (number of daffodils between 100-999)

for item in range(100,1000):
    ge=item%10
    shi=item//10%10
    bai=item//100
    if ge**3+shi**3+bai**3==item:  #(2**3是2的3次方的意思)
        print("是水仙花数:",item)

4. Process control statement break

Flow control statement break The break statement is used to end the loop structure, usually used together with the branch structure if
1. Use for as an example

for item in range(3):
    pwd=input("请输入您的密码:")
    if(pwd=='8888'):
        print("密码正确")
        break
    else:
        print("密码错误")

2. Use while as an example

a=0
while a<3:
    pwd=input("请输入您的密码:")
    if(pwd=='8888'):
        print("密码正确")
        break
    else:
        print("密码错误")
    a+=1

5. Process control statement continue

for item in range(1,51):
    if item%5!=0:
        continue
    print(item)

6. else statement

There are three combinations of else

  • if...else (execute else when the if conditional expression is not true)
  • for…else
  • while…else (both while and for execute else when break is not encountered)

1. The usage situation of for...else is that the loop ends normally and no break is encountered.

for item in range(3):
    pwd=input("请输入密码:")
    if (pwd == '8888'):
        print("密码正确")
        break
    else:
        print("密码错误")
else:
    print('对不起,三次密码均输入错误')

2. While…else is the same as for…else

a=0
while a<3:
    pwd=input("请输入您的密码:")
    if(pwd=='8888'):
        print("密码正确")
        break
    else:
        print("密码错误")
    a+=1
else:
    print('对不起,三次密码均输入错误')

7. Nested loops

1. Output a rectangle with three rows and four columns

for i in range(1,4):
    for j in range(1,5):
        print("*",end='\t')   #记得要写end='\t' 否则就自动换行了  如果想要挨着,那么end=''就好
    print() #换行

2. Output the multiplication table

for i in range(1,10):
    for j in range(1,i+1):
        print(j,'*',i,'=',i*j,end='\t')
    print()

3. Use continue and break in the second-level loop

for i in range(5):
    for j in range(1,11):
        if j%2==0:
            #break
            continue
        print(j,end='\t')
    print()

Guess you like

Origin blog.csdn.net/qq_43757976/article/details/130328512