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 are the loop statements that meet the Python condition

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail what are the loop sentences that meet the Python conditions, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Python conditional statement

A Python conditional statement is a block of code that determines execution by the execution result of one or more statements. In Python programming, the if statement is used to control the execution of the program. Python does not support the use of switch statements, so when there are multiple conditions, you can only use elif for programming. The basic form of the if statement is:

If (conditional expression): conditional statement elif (other conditions): conditional statement else: conditional statement

Example:

A = 1if type (a) = = int: # determine whether an is plastic print ('is plastic') # if an is plastic, execute the conditional statement elif type = = float: # determine whether an is floating-point print ('floating-point') # if an is floating-point Execute the conditional statement else: # any type of print except shaping floating point ('') # execute the conditional statement # output result: for shaping 1.1 pass statement

The if statement cannot be empty. You don't want to execute anything in the statement block after the colon. You can use the pass statement to avoid errors. Example:

A = 0if a = 0: passelse: print ('hello') print (' end') # output result: end2, Python for loop statement

The for loop is used to iterate through items of any sequence, such as strings or lists. The for loop judges one condition at a time. Example of dictionary loop traversal:

Person = {"name": "mj", "age": 31, "hobby": "dance"} # get all the key value values in the dictionary for iMagne v in person.items (): print (I) print (v)

# output result:

Name

Mj

Age

thirty-one

Hobby

Dance

2.1 for nested Loop

A nested loop is a loop within a loop, and the inner loop is executed every time the outer loop is iterated. Example:

# use for to print the multiplication table for i in range (1): for j in range (1): print ("{} * {} = {}" .format), end='') print () ""

# output result:

1-1-1

1 / 2 / 2 / 2 / 2 / 4

1 "3" 3 "3" 3 "3" 6 3 "3" 9

1 "4" 4 2 "4" 8 3 "4" 12 4 "4" 16

1 "5" 5 "5" 10 3 "5" 15 4 "5" 20 5 "5" 25

1 "6" 6 "6" 12 3 "6" 18 4 "6" 24 5 "6" 30 6 "6" 36

1 "7" 7 "7" 14 3 "7" 21 4 "7" 28 5 "35 6" 7 "42 7" 7 "49

1 "8" 8 "8" 16 3 "8" 24 4 "8" 32 5 "8" 40 6 "8" 48 7 "8" 56 8 "8" 64

1'9'9 2'9 18 3'9 27 4'36 5'9 45 6'54 7'63 8'72 9'9 81

"

3. Python while loop statement

The while loop execution statement can be a single statement or a block of statements, and we can execute a set of statements as long as the condition is true. If the conditional judgment statement is always true, the loop will execute indefinitely, for example:

While (1): print (missing)

Output result:

Miss

Miss

Miss

...

"

3.1 while loop nesting

Use the while statement to print the ninety-nine multiplication table example:

I = 1while (I

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

Development

Wechat

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

12
Report