python conditional loop

  •  

  • python conditional loop

    Conditional loops in Python can be used to solve many problems, such as calculating the maximum value in a set of data, or getting an element from a list. Here is an example of a conditional loop. Python's conditional loop can be divided into two types, one is to use the for loop directly to calculate the maximum value, and the other is to use the for loop + if statement to calculate the maximum value. Both are the same in use, but the way of implementation is different. Directly use the for loop to calculate the maximum value, which is generally used in simple list comparison or sorting scenarios. For conditional loops, using for loop + if statement to calculate the maximum value can better reflect the advantages of conditional loops. Let's illustrate with a simple example:

    • 1. Get the list

      First of all, let's take a look at how Python obtains lists, as follows: We know that lists in Python are stored with tuples as the basic unit. A tuple is a one-dimensional array, usually stored in units of numbers. For example, in the above code, we combine the two strings "Hello World" and "How are you" into a tuple "Hello World", and store each element in the tuple at the beginning of the string. Then store the first element of the string at the beginning of the list and the second element at the end of the list via the loop variable. In this way, when we want to get an element from the list, we can read it through the loop variable. Then let’s look at Python’s conditional loop, as follows: First, let’s look at the implementation of the program: In the above code, we first define two variables "Hello World" and "How are you", and then assign them to 0 and 1, and assign a value of 1 to both variables. Then use the for loop to calculate the maximum value of each element. Finally, use the if statement to determine whether it is an element in the list. It should be noted here that when using the for loop to calculate the maximum value, if the for statement is used to obtain the elements in the list, then only one type of element can be obtained at a time. But if we want to get multiple elements of different types, then we can convert it to a list. Here we just need to convert it to a list: The above is the specific implementation of using conditional loops to obtain elements in the list. Let's talk about another way to use conditional loops: Here we first define a variable "total", and then use the for statement to get elements from the list. Finally, use the for statement to read the elements. However, unlike the above, we don't need to use the for statement to read the elements in the list.

    • 2. Compare the maximum value

      In the above example, we have used a for loop to compare the maximum value in two lists. Since we want to calculate the maximum value in these two lists, we can first determine which list has the maximum value, and then compare the two lists. But directly using the for loop to calculate the maximum value is very slow. If we use for loop + if statement, we can calculate the result very quickly. Let's look at an example of using for loop + if statement: In the above example, we first determine which list has the maximum value, and then use the if statement to calculate the maximum value. First determine which list has the maximum value, and then compare which list has the maximum value. The advantage of this is that it is very fast. But the downside of this is that it's a bit complicated. When using the for loop + if statement to calculate the maximum value, the value will be returned automatically after the maximum value is judged. However, when the loop is executed to the end, it may happen that the loop ends because a certain condition is not met. At this time, we need to judge again. Therefore, when using the for loop + if statement to calculate the maximum value, a conditional judgment statement should be added in the last judgment. But doing so will cause the conditional judgment statement to fail to execute when the result is returned after the loop ends. Because when using the if statement to calculate the maximum value, if we use the if () and else () conditions in the expression of the if statement, the judgment result will be converted to a Boolean value (False), and the for statement needs to be called in the middle Execute judgment. Although this can avoid the situation that the conditional judgment statement cannot be executed, it is very slow. In this process, we use a for loop to deal with the problems encountered in the loop execution. By comparing each element in this list with the element calculated in the previous cycle, compare and judge, and then perform the next cycle to calculate the maximum value. Doing this is very fast. When using the for+if statement to calculate the maximum value, because each loop needs to perform a comparison and judgment operation, it consumes more time. In order to avoid this situation, we can use conditional judgment statements instead of for loops for comparison and judgment operations. By comparing and judging, we can get a result: In the above example, we first judge whether there is a maximum value in the list before comparing and judging, thus avoiding the problems encountered in the loop execution process.

    • 3. Return the result

      The returned result can be the maximum value or the minimum value, depending on what you want. In the above code, if the maximum value is not calculated, an integer will be returned as the result. This is not the result we want. If we want to take out both the maximum and minimum values, we need to use a for loop. Because the for loop can only operate on values, it will report an error when it cannot be operated. Well, this is how to use the Python conditional loop. In the next article, we will see how to use conditional loops to count the elements in a list.

Common python conditional loop codes are as follows:

1. Use the if statement to determine the size of the number:
```
num = 10
if num > 0:
print("num is a positive number")
elif num == 0:
print("num is 0")
else:
print("num is a negative number ")
```
2. Use the while loop to calculate the sum of 1 to 10:
```
sum = 0
i = 1
while i <= 10:
sum += i
i += 1
print("The sum of 1 to 10 is: ", sum)
```
3. Use the for loop to traverse the list:
```
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)
```
4. Use the range function Generate a sequence of numbers and iterate through:
```
for i in range(1, 11):
print(i)
```
5.Use break and continue to control the loop:
```
i = 0
while i < 10:
i += 1
if i == 5:
continue
if i == 8:
break
print(i)
```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130655262