Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Python conditional judgment and loop statement

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the difference between Python conditional judgment and loop statement". In daily operation, I believe many people have doubts about what is the difference between Python conditional judgment and loop statement. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the question of "what is the difference between Python conditional judgment and loop statement"! Next, please follow the small series to learn together!

(1) If-elif-else

The if statement is the basis of the judgement statement. The basic form of an if statement is as follows:

if case1: block1elif case2: block2else: block3

If the compound statement is a single statement, you can write the preceding statements on the same line. Its basic form is as follows:

if case1:block1elif case2:block2elif case3:block3else:block4

Note: This principle also applies to for-loop and while-loop statements.

(2)if nested

In nested if statements, the if…elif…else structure can be placed inside the main if…elif…else structure. The basic structure is as follows:

if case1:if case1.1: block1 elif case1.2: block2 else: block3elif case2: block4else: block5(3) conditional expression

If the judgment condition structure is simple, you can also use conditional expressions, that is, ternary operators. The basic form is as follows:

#If condition is true, return X, otherwise YX if condition else Y

Here is a simple example of a ternary operator:

#Output the larger of a,b a,b = 6,8c = a if a>b else bprint("the larger of a,b is: ", c)

Run Results:

A,B, C, C, D, E, E,

The while statement is also the base statement in a loop statement. The basic form of the while statement is as follows:

while condition: repeat_block

The block repeat_block in the while loop will loop until condition is false.

After the while and for statements, there can also be an else statement. When condition is false, jump out of the loop and execute the block in else. The basic form of the while-else statement is as follows:

while condition: repeat_blockelse: once_block

Note: If the loop is terminated by a break statement, the else statement block is not entered.

(2)for loop

The for statement is born for iteration and is the base statement in a loop statement. The basic form of for is as follows:

for iter_var in iterable: repeat_block

Each loop, the iterator variable (iter_var) is set to the current element of the iterable, provided to the repeat_block statement block. Where iterable can be a sequence, iterator, or other object that supports iteration.

Here are three examples of different iterations:

Iterate directly over elements in nameList.

nameList = ["Zhang San", "Li Si", "Wang Ermazi"]for name in nameList:print(name)

Run Results:

Zhang San Li Si Wang Er Ma Zi

2. Iterate through the subscript of nameList element, and use len when generating range object.

nameList = ["Zhang San", "Li Si", "Wang Ermazi"]for i in range(0, len(nameList)):print(nameList[i])

Run Results:

Zhang San Li Si Wang Er Ma Zi

3. Use enumerate to generate iterators and complete the access to list subscripts and element values in a for loop.

nameList = ["Zhang San", "Li Si", "Wang Ermazi"]for i, name in enumerate(nameList):print(i, name)

Run Results:

0 Zhang 3 1 Li 4 2 Wang 2 Ma Zi 3, break and continue the difference

Break and continue are often used in while and for loops to interrupt programs, but there are essential differences:

The break statement jumps directly out of the body of the current for and while loop. If you terminate from a for or while loop, any corresponding loop else block will not execute.

The continue statement is used to skip over the remaining statements in the current loop block and then proceed to the next loop.

The following figure takes the while loop as an example to show the different execution orders of the program when using break and continue:

Thus, using break in the loop body will jump out of the loop directly, as shown in the red line in the figure; using contiune will end the current loop and continue to execute the next loop, as shown in the green line in the figure.

At this point, the study of "What is the difference between Python conditional judgment and loop statement" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report