In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the types of loops in Python". In the operation of actual cases, many people will encounter such a dilemma, 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!
What is a cycle?
In programming, a loop means repeating the same set of calculations multiple times in the same order.
Think about what happens in real life. You are a field biologist who measures trees in the forest. You choose a tree, measure its diameter and height, write them down in your notebook, and estimate its total volume. Next, you choose another tree, measure its diameter and height, write them down in your notebook, and estimate its total volume. Then, you choose another tree, measure its diameter and height, write them down in your notebook, and estimate its total volume.
You will repeat the same process until all the trees in the example are used up. In programming jargon, you will traverse each tree and execute the same set of tasks in the same order.
Going back to programming, if you are given a list of integer values and asked to square each item, then add 5, and finally report the result-- this is an example of a loop.
What can we cycle through?
So what can we cycle through? Basically, any iterable data type can be manipulated using loops. Iterable objects in Python are sequences of values stored in different data formats, such as:
List (for example. [15] 10, 12, 13)
Tuples (for example, (10, 12, 13, 15))
Dictionary (e.g. {'Name': 'Alan', 'Age': 25})
String (for example, 'Data Science')
What are the types of loops?
There are two main kinds of loops: for loop and while loop. Among them, for loop is the most common one applied to data science problems. The key differences are:
The for loop iterates a limited number of times over each element in the iterable object
The while loop goes on until a condition is met
Traverse the list
Traversing a list is very simple. Give a list of values and ask to do something for each item. Suppose you have:
My_list = [1, 2, 3, 4]
Then you are asked to square each value in the list:
For each_value in my_list: print (each_value * each_value) Out: 1 4 9 16
Similarly, you can do more complex loops (such as' nested loops'). For example, give you two lists and ask:
(I) multiply the value of one list by another
(ii) append them to an empty list
(iii) print out a new list.
New_list = [] list1 = [2,3,4] list2 = [4,5,6] for i in list1: for j in list2: new_list.append (iStij) print (new_list) Out: [8,10,12,12,15,18,16,20,24] ergodic tuples
Traversing tuples can be a bit complex, depending on the structure of the tuples and the tasks to be accomplished.
Let's store some tuples in a list, each representing the name and age of a class of middle school students:
Students = [('Allie', 22), (' Monty', 18), ('Rebecca', 19)]
The task now is:
(I) extract all ages
(ii) store them in a list
(iii) calculate the average age
Ages = [] for iMagin j in students: ages.append (j) avg = sum (ages) / len (ages) print (avg) Out: 19.6666666666668
Each tuple here contains two entries (name and age). Even if you are not interested in the name, with I and j, you will specify these two items and ask that the item j (age) be appended to a new list. It is called "tuple unpacking".
Ergodic dictionary
A dictionary in Python is a collection of key-value pairs: each item in the dictionary has a key and an associated value. An example of a dictionary:
# fruit price dictionary fruit_prices = {"apple": 2.50, "orange": 4.99, "banana": 0.59}
You can loop through these dictionary elements and perform various operations. Here are some examples:
Extract all the key values in the dictionary:
For i in fruit_prices.keys (): print (I) Out: apple orange banana
Store all values in one list:
Values = [] for i in fruit_prices.values (): values.append (I) print (values) Out: [2.5,4.99,0.59] traverses the string
Let's consider the string-"Hello". Does it look like an iterable object? Actually, it is.
For i in 'Hello': print (I) Out: Hello
You can use for loops to unpack each character in a string and perform various operations on them.
Similarly, you can traverse every word in the sentence. But in this case, an extra step is needed to segment the sentence.
Sent = 'the sky is blue' # splitting the sentence into words sent_split = sent.split () # extract each word with a loop for i in sent_split: print (I) Out: the sky is blueWhile loop
Similar to the for loop, the while loop repeatedly executes a block of code-- as long as the condition is true. The loop is aborted only if the loop condition is false.
The general structure of a while loop is as follows:
I = 0while 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.
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.