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

How to use while and for loops

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

Share

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

This article introduces the knowledge of "how to use while and for loops". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Overview

The loop statements in Python are for and while.

2. While cycle

1. Grammar:

While judgment condition:

Else:

2. Description:

When the conditions are met, the loop is performed. Jump out of the cycle when the conditions are no longer met

III. For statement

1. Grammar:

For in:

Else:

2. Description

An item used to traverse any sequence, such as a list or a string.

4. Range () function

1. If you need to traverse the sequence of numbers, you can use the built-in range () function. It generates a sequence of numbers.

For i in range (5):

Print (I)

0

one

two

three

four

2. You can also use range to specify the value of the range.

For i in range (5pr 9):

Print (I)

five

six

seven

eight

3. You can have range start with a specified number and specify a different increment (or even a negative number, which is sometimes called a step size)

For i in range (0,10,3):

Print (I)

0

three

six

nine

4. You can combine the range () and len () functions to traverse the index of a sequence

A = ['Google',' Baidu', 'Runoob',' Taobao', 'QQ']

For i in range (len (a)):

Print (I, a [I])

0 Google

1 Baidu

2 Runoob

3 Taobao

4 QQ

5. You can use the range () function to create a list

List (range (5))

[0, 1, 2, 3, 4]

5. Break and continue statements and else clauses in loops

1 、 break

Description

The break statement can jump out of the loop body of for and while. If you terminate from a for or while loop, any corresponding loop else blocks will not be executed.

Exampl

For letter in 'Runoob': # first instance

If letter = 'baked:

Break

Print ('current letter is:', letter)

2 、 continue

Description

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

Exampl

For letter in 'Runoob': # first instance

If letter = 'oasis: skip output when the letter is o

Continue

Print ('current letter:', letter)

3. A loop statement can have an else clause, which is executed when an exhaustive list (as a for loop) or when a condition changes to a false (a while loop) results in the termination of the loop, but not when the loop is terminated by break.

For n in range (2,10):

For x in range (2, n):

If n% x = = 0:

Print (n, 'equals', x,'*', nUnix)

Break

Else:

No element found in # loop

Print (n,'is prime')

VI. Pass sentence

Pass is an empty statement to maintain the integrity of the program structure.

Pass does nothing and is generally used as a placeholder statement

While True:

Pass # waiting for keyboard interruption (Ctrl+C)

Smallest class:

Class MyEmptyClass:

Pass

The following example executes a pass statement block when the letter is o:

For letter in 'Runoob':

If letter = = 'oasis:

Pass

Print ('execute pass block')

Print ('current letter:', letter)

Good

That's all for "how to use while and for loops". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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