程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python based circular statements

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .


One 、 background

Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce while Circulation and for How to use circular statements .


Two 、 Practice

When solving practical problems , We often encounter different operation conditions , Or often encounter the need to repeat the same or similar operations .Python Judgment and loop statements are provided to solve these problems .

1、for Loop statement

Python Medium for Loops are often used to traverse lists 、 Tuples 、 Elements in sequences such as strings and dictionaries . for The basic format of the syntax structure of circular statements :

for Variable in Sequence :
Sentence block

example 1,for Loop through a string .

for x in "python":
print(x)

Program running effect .

for Circular statements are often associated with range() Functions together ,range() The function is Python Built in functions for , You can create a list of integers .range() The syntax of the function is : (1) Count from start Start , The default is 0 Start . (2) Count to stop end , But does not include stop. (3) step , The default is 1.

range([start,]stop[,step])

example 2:

range(5) Equivalent to range(0,5) range(0,5) yes [0,1,2,3,4] range(0,5) Equivalent to range(0,5,1)

example 3: use for Statement seeking S=1+2+3+…+100 Value .

S = 0 # Create variables S, The assignment is 0
for i in range(1, 101): # Loop variable i from 1 Loop to 100
S += i # Sum up , Put the results in S in
print("S=1+2+3+…+100=", S) # Output S Value

Program running effect .


2、while Loop statement

while The basic format of a loop statement is as follows :

while Judge the condition :
Sentence block # The loop body

Tips :while The loop statement is “ First judge , After execution ”. If the conditions are not met when the cycle is just entered , Then the loop body does not execute . The other thing to note is that , There must be a sentence to modify the judgment conditions , Make it false , Otherwise, there will be “ Dead cycle ”.

example 1: Programming , seek S=1+2+3+…+100 Value .

i = 1 # Create variables i, The assignment is 1
S = 0 # Create variables S, The assignment is 0
while i <= 100: # loop , When i>100 End of time
S += i # Sum up , Put the results in S in
i += 1 # Variable i Add 1
print("S=1+2+3+…+100=", S) # Output S Value

Program running effect , Be careful . (1) Variable initialization description should be complete 、 accuracy . (2) There should be a statement in the loop body that makes the loop tend to end .

3、 A nested loop

The loop body of a loop statement contains another complete loop structure , It's called loop nesting . (1) The circulation embedded in the circulation is called internal circulation . (2) A cycle embedded with an inner cycle is called an outer cycle . (3) Loops can also be nested in embedded loops , This is called multiple cycles . Two kinds of loop statements while Statement and for Statements can be nested with each other , Free combination . The outer circulation body may contain one or more inner circulation structures . Be careful : Each cycle must contain , No cross is allowed between them .

example 1: Write a program , Output the following multiplication table .

for x in range(1, 10): # Loop variable x from 1 Loop to 9
for y in range(1, x + 1): # Loop variable y from 1 Loop to x+1
print(y, "*", x, "=", x * y, "", end="") # Output multiplication expression
print("") # Output empty string , The function is to wrap

Program running effect .


Four 、 Reference resources

1、Python Programming case tutorial


5、 ... and 、 summary

The above is about Python Of while Circulation and for How to use circular statements . How to use , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved