In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the branch structure and loop structure in Python". In daily operation, I believe that many people have doubts about what the branch structure and loop structure in Python are. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the branch structure and loop structure in Python". Next, please follow the editor to study!
For-in cycle
If you know exactly how many times the loop is executed, we recommend using the for-in loop, such as calculating the sum of 1 to 100, that is.
"
Using for Loop to realize the Sum of 1 to 100
Version: 0.1
Author: Luo Hao
"
Total = 0
For x in range (1,101):
Total + = x
Print (total)
It is important to note that range (1,101) in the above code can be used to construct a range from 1 to 100. when we put such a range into a for-in loop, we can fetch integers from 1 to 100 in turn through the previous loop variable x. Of course, the use of range is very flexible, and here is an example:
Range (101): can be used to generate integers in the range of 0 to 100, but it is important to note that it is less than 101.
Range (1,101): can be used to produce integers in the range of 1 to 100, which is equivalent to an open interval followed by a closed interval.
Range (1,101,2): can be used to generate odd numbers from 1 to 100, where 2 is the step size, that is, the value incremented each time.
Range (100,0,-2): can be used to produce even numbers from 100 to 1, where-2 is the step size, the value of each decreasing number.
Knowing this, we can use the following code to achieve the sum of even numbers between 1 and 100.
"
Using for Loop to realize the Sum of even numbers between 1 and 100
Version: 0.1
Author: Luo Hao
"
Total = 0
For x in range (2,101,2):
Total + = x
Print (total)
While cycle
If you want to construct a loop structure that does not know the specific number of loops, we recommend using while loops. The while loop controls the loop through an expression that produces or converts a Bool value. The expression's value of True continues the loop; the expression's value of False ends the loop.
Let's take a look at how to use the while loop through a "guessing" Mini Game. The rules of the number guessing game are: the computer gives a random number between 1 and 100, the player enters the number he guesses, and the computer gives the corresponding prompt information (bigger, smaller, or guessed right). If the player guesses the number, the computer prompts the user to guess a total of how many times, the game is over, otherwise the game continues.
"
Game of guessing numbers
Version: 0.1
Author: Luo Hao
"
Import random
Answer = random.randint (1,100)
Counter = 0
While True:
Counter + = 1
Number = int (input ('Please enter:')
If number
< answer: print('大一点') elif number >Answer:
Print ('smaller')
Else:
Print ('Congratulations on your guess!')
Break
Print (you guessed a total of {counter} times')
Break and continue
The above code uses while True to construct a loop with constant conditions, which means that the loop will not end without special treatment, which is often referred to as a "dead loop." In order to exit the loop structure when the user guesses a number, we use the break keyword, which ends the loop early. It is important to note that break can only terminate the loop it is in, which needs to be noted when using a nested loop structure, which we will talk about in the following example. In addition to break, another keyword is continue, which can be used to abandon the subsequent code of this loop and allow the loop to move on to the next round.
Nested loop structure
Like branch structures, loop structures can be nested, that is, loop structures can also be constructed in the loop. The following example demonstrates how to output a multiplication table (ninety-nine table) through a nested loop.
"
Print multiplication table
Version: 0.1
Author: Luo Hao
"
For i in range (1,10):
For j in range (1, I + 1):
Print (f'{I} * {j} = {I * j}', end='\ t')
Print ()
Obviously, in the above code, the outer loop is used to control the output of a total of nine lines, while the inner loop is used to control how many columns are output for each row. The output in the inner loop is all the columns in the ninety-nine table row, so when the inner loop is complete, there is a print () to achieve the effect of newline output.
Example of a loop: enter a positive integer to determine whether it is a prime or not.
Tip: a prime is an integer greater than 1 that is divisible only by 1 and itself.
"
Enter a positive integer to determine whether it is a prime or not
Version: 0.1
Author: Luo Hao
"
Num = int (input ('Please enter a positive integer:')
End = int (num * * 0.5)
Is_prime = True
For x in range (2, end + 1):
If num% x = = 0:
Is_prime = False
Break
If is_prime and num! = 1:
Print (f'{num} is prime')
Else:
Print (f'{num} is not a prime')
Example 2: enter two positive integers and calculate their maximum common divisor and least common multiple.
Hint: the greatest common divisor of two numbers is the largest of the common divisors of two numbers; the least common multiple of two numbers is the smallest number that can be divisible by two numbers at the same time.
"
Enter two positive integers to calculate their maximum common divisor and least common multiple
Version: 0.1
Author: Luo Hao
"
X = int (input ('x ='))
Y = int (input ('y ='))
If x > y:
The values of two variables can be exchanged in this way in x, y = y, x # Python
For factor in range (x, 0,-1):
If x% factor = 0 and y% factor = = 0:
Print (the greatest common divisor of f'{x} and {y} is {factor}')
Print (the least common multiple of f'{x} and {y} is {x * y / / factor}')
Break
At this point, the study of "what is the branch structure and loop structure in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.