This article mainly introduces a list of 70 Python practice projects, which has certain reference value. Friends in need can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to understand it together.
For Python learners, being able to master concise and efficient programming skills in Python can not only improve the efficiency of the program, but more importantly, reflect the programmer's superb programming ability.
Today, the editor will share with you ten small Python cases. Each case has two solutions. The first method is relatively simple for novices, and the second method is for experienced experts to write fast code papers . Although the case is small, it contains Python programming skills. Let’s take a look.
1. Determine whether all the values in a list are less than a certain number
Faced with this problem, there are actually two common solutions:
Method 1: The most intuitive program is to determine whether the elements in the list are less than a certain value one by one. This method is the easiest to think of, but the program is very complicated.
Method 2: It uses two Python built-in functions + Python anonymous functions , which can be easily solved with one line of code.
2. Sort the strings in the list according to specific requirements
For sorting the strings in the list, friends who are familiar with sorting algorithms will think of using sorting algorithms to solve the problem. For example, method one is to use bubble sorting to solve the problem;
Method 2 can be solved by just one line of code using the built-in function sorted. Not only that, for specifying keywords for sorting, you can also set your own sorting function, such as the firstC function above, which sorts by the first letter of the string.
3. Sort the dictionary by key or value
Sort a dictionary by key or value
Method 1: You can use the sorted built-in function to sort and then convert it into dictionary form. This method will cause a waste of space resources during the conversion process.
Method 2: It directly uses the keys or values to sort, and then uses the sorted keys or values to construct the final dictionary, and the program is simple.
4. Convert the numbers in the list into strings
Among the above two methods, the editor personally thinks that they are both very good methods. Method 1 uses a list parsing scheme to generate a new list through loop iteration. Method 2 uses the map built-in function to convert the numbers in the list into strings.
5. Determine whether the elements in the list all belong to the same type
Method 1: The method used is to judge each element in the list one by one. If any element is not a string, False is output. When all loops are completed, if the index value is equal to the total length of the list, True will be output.
Method 2: Still use the map function to determine whether each element in the list satisfies the checkStr function . Use the all function to get the final result.
6. Reverse the list
Method 1: The method used is to create a new list object and add the elements in list6 to the new list from back to front. Method 2: There are two ways. The first way is to use the list slicing method to obtain the reversed list. The second way is to use the reverse function of the list , but the reverse function can only modify the original list and cannot create a new list.
7. Randomly select an element from an iterable object
To select random values in Python, you can use Python's built-in library random. In the function above, the choice function randomly selects a value from the list, the choices function selects k values with replacement, and sample selects k values without replacement. Select k values.
8. Create a dictionary using lists
Method 1: Use a list to create a dictionary, and use a for loop to create key-value pairs of items in the dictionary.
Method 2: Use the zip built-in function to create a zip object, and use the dict function to convert the zip object into a dictionary, all done in one line of code.
9. Filter out strings starting with vowels
Method 1: Perform judgment processing on each string element in list9. If the first letter of the string is the cause letter, add the string to the new list new_list9.
Method 2: Directly use list parsing or use the filter function to filter the elements in list9 that satisfy the anonymous function. Compared with the map function, the filter function can directly filter out the element values that meet the conditions.
10. Create a counting dictionary
Create a numerical statistics dictionary method 1: First create a dictionary, the keys of the dictionary contain all the elements that appear in list10, and then count the number of occurrences of the elements in the list one by one.
Method 2: Borrow the Counter class in the collections library, directly count the number of elements in list10, and then use the dict function to convert the Counter object into a dictionary object.
Summarize
Through the sharing of the above ten small cases, we can see that each case contains the wisdom of using Python's built-in functions to optimize the program. In the process of writing programs, everyone should also be good at digging and thinking about how to fully optimize the program. Use Python's existing functions to make your programs more beautiful. This can not only improve the aesthetics of the program, but also improve the operating efficiency of the program, especially for the processing of large amounts of data.
Python experience sharing
Learning Python well is good whether you are getting a job or doing a side job to make money, but you still need to have a learning plan to learn Python. Finally, we share a complete set of Python learning materials to give some help to those who want to learn Python!
Python learning route
Here we have sorted out the commonly used technical points of Python, and summarized the knowledge points in various fields. You can find corresponding learning resources based on the above knowledge points.
learning software
Python is a commonly used development software that will save everyone a lot of time.
Learning video
When learning programming, you must watch a lot of videos. Only by combining books and videos can you get twice the result with half the effort.
100 practice questions
Practical cases
Optical theory is useless. When learning programming, do not talk about it on paper. You must practice it and apply the knowledge you have learned into practice.
Finally, I wish you all to make progress every day! !
The above complete version of the complete set of Python learning materials has been uploaded to the CSDN official. If friends need it, they can directly scan the CSDN official certification QR code below on WeChat to get it for free [100% free guaranteed].