Moment, Python than ever before are more popular, people are in practice every day and how powerful Python is easy to use.
I have been programming in Python for several years, but the last six months is full-time. These things listed below, that I started with Python when you want to clear:
- String Manipulation
- List comprehension
- Lambda functions and Map
- Else if elif and use condition judgment on one line
- zip () function
String Manipulation
Python is good at using similar mathematical operators + and * to operate the string
We can also be very convenient to negate operation of a string, just use the [:: --1] can be, but is not limited to this operation also string manipulation.
So for the list contains multiple strings of it, we can even do a Yoda-translator!
In the above code, we use .join () method, with spaces to reverse the list of elements of stitching up, and added an exclamation point.
List comprehension
Oh my god! Once I know that, my whole world changed (it may not have actually happened, but it was close). This is China's powerful, intuitive and readable way to quickly list of operations.
If we have such a function that takes a number of square add 5Python learning exchange group
Now if we want the function to a list of all the odd of them, if not understand list comprehensions, so you might write
But we have an easier way!
List derivation applies to [expression for item in list] condition, and if you want to apply some Boolean conditions, such as the above get an odd number of conditions: [expression for item in list if conditional], then it and the following wording is consistent
Cool, but we can do better, because we do not need the function "stupid_func"
Lambda and Map
Lambda
Lambda little strange, but I like the other elements introduced as long as you just go with it, you will find just how powerful and intuitive yes.
Lambda is actually a small anonymous function. Why anonymous it? This is because the small Lambda often used to perform simple operations, and these operations often need to use def my_function () function to define a formal
We were still the example above, for example, a number of squares and add 5. In the above code, we define a function def stupid_func (x), now let's use it to re-create Lambda
So why use such a strange syntax it? In fact, the usefulness of such an approach is reflected in, we do not define the actual function, you can achieve some simple operations. We continue to be a list of numbers, for example, if we want to sort the list below, one approach is to use the sorted ()
This does work, but if we want to follow the size of the square of the number of elements to sort, using Lambda is very convenient. key can be defined using a Lambda sorted () function is used for sorting
Map
Map is used to a function applied to each element of the sequence, such as a list. Suppose we have two lists to list the corresponding position of the element of the product, then how to do it, you can use Lambda and Map
The following code and compared with a combination of Lambda Map is too elegant a
Else if elif and use condition judgment on one line
Sometimes, you might write the following code
When you run this command, the system will prompt you () function from the input content input, assuming we enter 5, we will get Duck. But we can also write like this
It's pretty simple! Go read your old code, you will find that there are too many places can be this simple if else to replace the judge in this single line judge.
zip()
Remember the Map function section, we list two examples of parallel processing Well, using zip () will be easier
If we have two lists, one containing the name, a last name that contains, how it can be a good merge them, using zip ()!
Oh wow, there's wrong, my name is not Peter Jensen, then it can be adjusted as follows
Conclusion
I am here just a summary of a simple checklist, the purpose is to let you be able to understand the elegant Python can do a lot of things. If you have any different ideas, you can message Oh!