range(10) returns a generator, often combined with a for loop
1. while loop
-python uses forced indentation to distinguish code blocks
- The standard is 4 spaces, tab and 4 spaces are not the same
while Judgment condition: (judgment condition boolean type expression)
loop body (can have multiple lines)
Avoid infinite loops --> regular code
n=1
while n<=10:
print(n)
n=n+1
Question 1: Find the sum of the even numbers from 1 to 100
n=1 sum=0 while n<=100: if n%2==0: sum+=n n=n+1 print(sum)The second question: ask the user to enter numbers, sum, until they enter 0 to exit
sum=0 while True: n=int(input('Please enter a number:')) if n==0: break sum+=n print(sum)
Question 3: 1-100
When a multiple of 3 and 5 is encountered, the current value is a multiple of 3 and 5
When a multiple of 5 is encountered, the current value is a multiple of 5
When a multiple of 3 is encountered, the current value is a multiple of 3
n=1 while n<=100: if (n%3==0)and(n%5==0): print(str(n),end='The current value is a multiple of 3 and 5') elif n%5==0: print(n, 'The current value is a multiple of 5') elif n%3==0: print(n, 'The current value is a multiple of 3') n=n+1 else print('End of loop') while...else executes the else block when the conditional statement is false
2. For iteration variable in list/dictionary/tuple takes values one by one
The range() function generates a sequence, excluding the trailing parameter
range(5): A parameter starts from 0 to 5 and does not contain 5 itself
range(5,15): The two parameters start from 5 to 15 and do not contain 15 themselves
range(5,55,5): The three parameters start from 5 to 55 and do not contain themselves, and the last parameter 5 is the step size
end=''do not wrap
1.for x in range(1,X):
loop body
2.list1 = [1,2,3,4] test_str = 'hello world'
for item in list1: for item in test_str:
print(item) print(item)
which is
for item in 'sequence': take out its elements from the sequence accordingly (a string is also a sequence)
print(item) traverse
3.nums=range(5)
for i in range(5,19,2):
print(i)
The first question: output a line of 9*
for i in range(9): print('*',end='')
Question 2: Output 9 lines of 9*
for i in range(9):#a few lines for i in range(9): # a line of several stars print('*',end=' ') print()
Question 3: Right Triangle
for i in range(1,6): for j in range(i): print('*',end='') print()
Question 4: Ninety-nine multiplication table
for i in range(1,10): for j in range(1,i+1): print(j,'*',i,'=',(j*i),end='\t') print()
Question 5: Rhombus
rows=int(input('Please enter the number of rows of diamonds')) s=rows//2+1 x=rows-s for i in range(s): for j in range(s-1,i,-1): print(' ',end='') for k in range(i*2+1): print('*',end='') print() for i in range(1,x+1): for j in range(i): print(' ',end='') for k in range((s-i)*2-1): print('*',end='') print()
3. Nested loops
break ends the loop where the outer code is executed --> outer loop
continue to return to the position where the loop condition is judged must end this loop in the loop body and continue the next loop. The code below continue is not executed
Question 1: Please input the score and count the number of people in each interval, re-enter if it exceeds 0-100, and correctly ask whether to continue
a=b=c=d=0 while True: num=int(input('Please enter the grade:')) if num>=90 and num<=100: a+=1 elif num>=80 and num<90: b+=1 elif num>=60 and num<80: c+=1 elif num>=0 and num<60: d+=1 else: print('Out of range, please re-enter:') continue print(num) str1=int(input('Continue? 1/0:')) print(str1) if str1==0: break print('>=90:',a) print('>=80:',b) print('>=60:',c) print('<60:',d)
Question 2: Count the grades of 3 students in each of the 3 classes: how many people are greater than 80
count=0 for i range(1,4): print('Please enter the grades of students in the ',i,' class') for j in range (1,4): score=int(input('Please enter the first',j,'the grade of the student')) if score<0: print('Enter a negative number to enter the next class') break if score<80: continue count+=1 print('Number of people greater than 80:', count)