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 Python loop method?

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

Share

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

In this article, the editor introduces in detail "what is the method of Python cycle" with detailed content, clear steps and proper handling of details. I hope this article "what is the method of Python cycle" can help you solve your doubts.

1. Loop # 1.for...in Loop Iterate each element in list or tuple in turn studentNames = ["Willard", "ChenJD", "ChenBao", "LinWenYu"] for studentName in studentNames: print (studentName) print ("- -") # calculate the sum of 1-10 and sumOfNums = 0for num in. 9d10]: sumOfNums = sumOfNums + numprint ("the sum of 1-10 is:", sumOfNums) print ("- -") # use the range () function to generate integer sequences The range () function is left closed and right open # calculates the cumulative sum of 1-1000 sumOfNums = 0for num in range (1001): sumOfNums + = numprint ("the cumulative sum of 1-1000 is:", sumOfNums)

# result output:

Willard

ChenJD

ChenBao

LinWenYu

The sum of 1-10 is: 55

The sum of 1-1000 is: 500500

# 2.while loop, as long as the condition is satisfied, continue to loop, exit the loop when the condition is not satisfied # calculate the odd number and sumOfNums = 0n = 99while n > 0: sumOfNums + = n = n-2print ("the sum of odd numbers within 100 is:", sumOfNums)

# result output:

The sum of odd numbers within 100 is: 2500

# 3. Exit the loop n = 1while n in advance using the breakstatement

< 20: if n >

10: break print (the value of "n is:", n) n + = 1print ("The End.")

The value of n is: 1

The value of n is: 2

The value of n is: 3

The value of n is: 4

The value of n is: 5

The value of n is: 6

The value of n is: 7

The value of n is: 8

The value of n is: 9

The value of n is: 10

The End.

# 4.continue statement, skip the current loop and start the next loop n = 10while n < 20: n + = 1 if n = = 15: continue print ("n value is:", n) print ("The End.")

# result output:

The value of n is: 11

The value of n is: 12

The value of n is: 13

The value of n is: 14

The value of n is: 16

The value of n is: 17

The value of n is: 18

The value of n is: 19

The value of n is: 20

The End.

# 5. Log in to the instance totalFrequency = 3inputFrequency = 0userName = input ("Please enter your account:") password = input ("Please enter your password:") while inputFrequency < totalFrequency: if ((userName = = "Willard") and (password = = "JD584520"): print ("account password is correct and login is successful!") Break else: print ("account or password entered incorrectly, login failed!") If (totalFrequency-inputFrequency-1) = = 0: break print ("Please log in again! you still have% d chances to enter your account password!" % (totalFrequency-inputFrequency-1) print ("- -") inputFrequency + = 1 userName = input ("Please re-enter your account:") password = input ("Please re-enter your password:")

# result output:

# output 1:

Please enter your account number: Willard

Please enter your password: JD584520

The account password is correct and the login is successful!

-

# output 2:

Please enter your account number: Willard

Please enter your password: jd584520

The account or password was entered incorrectly and the login failed!

Please log in again! You still have 2 chances to enter your account password!

-

Please re-enter your account number: Willard

Please re-enter your password: JD584520

The account password is correct and the login is successful!

-

# output 3:

Please enter your account number: willard

Please enter your password: JD584520

The account or password was entered incorrectly and the login failed!

Please log in again! You still have 2 chances to enter your account password!

-

Please re-enter your account number: Willard

Please re-enter your password: jd584520

The account or password was entered incorrectly and the login failed!

Please log in again! You still have a chance to enter your account password!

-

Please re-enter your account number: willard

Please re-enter your password: jd584520

The account or password was entered incorrectly and the login failed!

two。 Dictionary # dict: dictionary, stored using key-value pairs (key-value) # example: studentScore = {"Willard": {"Math": 100,98, "Chinese": 98, "Eng": 90}, "ChenJD": {"Math": 99, "Chinese": 100, "Eng": 98}, "ChenBao": {"Math": 100, "Chinese": 99, "Eng": 96}} print ("Willard score is:", studentScore ["Willard"]) print ("ChenJD score is:" StudentScore ["ChenJD"]) print ("ChenBao's result is:", studentScore ["ChenBao"]) print ("-") # modify the value of the element print ("Willard's original result is:", studentScore ["Willard"]) studentScore ["Willard"] = {"Math": 100, "Chinese": 100 "Eng": 100} print ("the score of the revised Willard is:", studentScore ["Willard"]) print ("-") # to determine whether key exists # 1. Determine whether print exists in key by in ("judging whether there is a ChenXiaoBao' key.\ n") if "ChenXiaoBao" in studentScore: print ("ChenXiaoBao' this key.") else: print ("does not exist 'ChenXiaoBao' this key.") print ("-") # 2. Through the get () method, if key does not exist, None is returned, or the specified valueprint (studentScore.get ("Willard")) print (studentScore.get ("Willard"),-1) print ("-") # deletes a key Using pop (key) print ("dictionary before deletion:\ n", studentScore, "\ n") studentScore.pop ("ChenBao") print ("dictionary after deletion:", studentScore) # Tips:# dictionary key must be immutable objects, such as strings, integers, etc., list is mutable

# result output:

Willard's score is: {'Math': 100,' Chinese': 98,' Eng': 90}

ChenJD's score is: {'Math': 99,' Chinese': 100, 'Eng': 98}

ChenBao's score is: {'Math': 100,' Chinese': 99,' Eng': 96}

-

Willard's original grades are: {'Math': 100,' Chinese': 98,' Eng': 90}

The score of the revised Willard is: {'Math': 100,' Chinese': 100,' Eng': 100}

-

Determine whether the key 'ChenXiaoBao'' exists.

There is no such key as' ChenXiaoBao'.

-

{'Math': 100,' Chinese': 100, 'Eng': 100}

{'Math': 100,' Chinese': 100, 'Eng': 100}-1

-

Delete the dictionary before:

{'Willard': {' Math': 100, 'Chinese': 100,' Eng': 100}, 'ChenJD': {' Math': 99, 'Chinese': 100,' Eng': 98}, 'ChenBao': {' Math': 100, 'Chinese': 99,' Eng': 96}}

Deleted dictionaries: {'Willard': {' Math': 100, 'Chinese': 100,' Eng': 100}, 'ChenJD': {' Math': 99, 'Chinese': 100,' Eng': 98}}

3. Collection # Collection: set; is a collection of key, but does not store value, and the key cannot be repeated and is unique; # 1. Create a set and provide a list as the input set setEg = set ([1 setEg 2) print ("contents of the set setEg:", setEg) print ("- -") # 2. The element uniqueness of a set setEg2 = set ([1 setEg2 1) 2 2]) print ("content of set setEg2:", setEg2) print ("- -") # 3. Add the element setEg = set ([1Power2Power3]) print ("contents of the collection before adding elements:", setEg) setEg.add (5) print ("contents of the collection after adding elements:", setEg) print ("- -") # 4. Delete the element setEg = set ([1Power2Power3]) print ("the contents of the collection before deleting elements:", setEg) setEg.remove (1) print ("contents of the collection after deleting elements:", setEg) print ("- -") # 5. Intersecting and merging setOne = set ([1Jing 2Jing 3Jing 4jue 5]) setTwo = set ([1Jing 2Jing 4Jing 6]) print ("contents of setOne collections:", setOne) print ("contents of setTwo collections:", setTwo) print ("intersection of setOne and setTwo:", (setOne & setTwo)) print ("Union of setOne and setTwo:", (setOne | setTwo))

# result output:

Contents of the collection setEg: {1,2,3}

-

Content of collection setEg2: {1, 2, 3, 4, 5}

-

The contents of the collection before adding elements: {1, 2, 3}

Contents of the collection after adding elements: {1, 2, 3, 5}

-

The contents of the collection before deleting the element: {1,2,3}

The contents of the collection after deleting the element: {2,3}

-

Contents of setOne collection: {1, 2, 3, 4, 5}

Contents of setTwo collection: {1, 2, 4, 6}

Intersection of setOne and setTwo: {1,2,4}

Union of setOne and setTwo: {1, 2, 3, 4, 5, 6}

After reading this, the article "what is the method of Python cycle" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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