author : Han Xinzi @ShowMeAI
Tutorial address :http://www.showmeai.tech/tutorials/56
This paper addresses :http://www.showmeai.tech/article-detail/72
Statement : copyright , For reprint, please contact the platform and the author and indicate the source
Python There are many sequential data structures in ( Such as list or string ), have access to for Loop through .
grammar :
for The syntax of the loop is as follows :
for iterating_var in sequence: statements(s)
flow chart :
Code instance ( The code can be in On-line python3 Environmental Science Run in )
for letter in 'ShowMeAI': # First instance print(" The current letter : %s" % letter) fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # Second example print(' Current fruit : %s'% fruit) print(" complete !")
The output of the above code :
The current letter : S The current letter : h The current letter : o The current letter : w The current letter : M The current letter : e The current letter : A The current letter : I Current fruit : banana Current fruit : apple Current fruit : mango complete !
Another way to do loop traversal is through the index , The following example ( The code can be in On-line python3 Environmental Science Run in ):
fruits = [' Banana ', ' Apple ', ' grapes '] for index in range(len(fruits)): print(' Current fruit : %s' % fruits[index]) print(" complete !")
The output of the above code :
Current fruit : Banana Current fruit : Apple Current fruit : grapes complete !
In the above example, we use the built-in function len() and range(), function len() Returns the length of the list , That is, the number of elements . range Returns the number of a sequence .
stay python in ,for … else That means ,for There is no difference between the sentences in and the ordinary ones ,else The statement in will be executed normally in the loop ( namely for Not through break Jump out and interrupt ) In case of execution ,while … else Is the same .
for num in range(20,30): # iteration 10 To 20 Number between for i in range(2,num): # Iterate over the factors if num%i == 0: # Determine the first factor j=num/i # Calculate the second factor print ('%d be equal to %d * %d' % (num,i,j)) break # Jump out of current loop else: # Cyclic else part print ('%d It's a prime number ' % num)
The output of the above code :
20 be equal to 2 * 10 21 be equal to 3 * 7 22 be equal to 2 * 11 23 It's a prime number 24 be equal to 2 * 12 25 be equal to 5 * 5 26 be equal to 2 * 13 27 be equal to 3 * 9 28 be equal to 2 * 14 29 It's a prime number
Let's use what we learned for loop , Draw different patterns , Help you get familiar with for Use circularly
The code can be in On-line python3 Environmental Science Run in .
rows = int(input(' Enter the number of columns : ')) print(" Print a hollow equilateral triangle , Here, take out if-else Conditional judgment is solid ") for i in range(0, rows + 1):# Variable i Control the number of lines for j in range(0, rows - i):#(1,rows-i) print(" ", end='') j += 1 for k in range(0, 2 * i - 1):#(1,2*i) if k == 0 or k == 2 * i - 2 or i == rows: if i == rows: if k % 2 == 0:# Because the first number is from 0 At the beginning , So if you print even numbers *, Odd print spaces print("*", end='') else: print(" ", end='')# Notice the ", end='' ", Must not omit , Can play the role of not breaking lines else: print("*", end='') else: print(" ", end='') k += 1 print("\n") i += 1
The code can be in On-line python3 Environmental Science Run in .
rows = int(input(' Enter the number of columns : ')) print(" Print a diamond shape such as a hollow , Here, take out if-else Conditional judgment is solid ") rows = int(input(' Enter the number of columns : ')) for i in range(rows):# Variable i Control the number of lines for j in range(rows - i):#(1,rows-i) print(" ", end='') j += 1 for k in range(2 * i - 1):#(1,2*i) if k == 0 or k == 2 * i - 2: print("*", end='') else: print(" ", end='') k += 1 print("\n") i += 1 # The bottom half of the diamond for i in range(rows): for j in range(i):#(1,rows-i) print(" ", end='') j += 1 for k in range(2 * (rows - i) - 1):#(1,2*i) if k == 0 or k == 2 * (rows - i) - 2: print("*", end='') else: print(" ", end='') k += 1 print("\n") i += 1
Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition
https://www.bilibili.com/video/BV1yg411c7Nw
The code for this tutorial series can be found in ShowMeAI Corresponding github Download , Can be local python Environment is running , Babies who can visit foreign websites can also directly use google colab One click operation and interactive operation learning Oh !
This tutorial series covers Python The quick look-up table can be downloaded and obtained at the following address :