Sample1 Ball 1+2+3...+100 and
Sum=0
For I in Range (1,101):
Sum+=i
Print sum
- Print if indented and sum+, the value of each sum+ will be printed
- If print is shelf with for, it will only print the value of the final sum+ after the for loop has finished executing
Sample2:for loop application and bubble sort:
LIST=[10,12,14,19,20,22]
For I in range (1,len (list)):
For j in Range (0,len (list)-1):
If LIST[J]>LIST[J+1]:
LIST[J],LIST[J+1]=LIST[J+1],LIST[J]
Print List
- Loop statement Sample2
list=["1", "2", "3", "4"
, "5", "6", "7"]
For I in list:
Print I
Note:
- Whether it is an if statement or a for statement, you need to add ":" When you finish writing your judgment or condition, or you will get an error.
- assignments, data type conversions, print, and so on are not required to add any punctuation
- The list data type can be wrapped directly, not affected
Lesson2:Python:for Statement Simple Application