This article mainly describes the Python syntax related articles, how to write some Python syntax practices, these contents are found on some portal websites and technical forums, there may be many errors I did not pick out, thank you for your correction.
Next, let's talk about the problem I encountered. I wanted to learn about lambda usage (I haven't figured it out yet). I found an example on the Internet, as shown below (I have changed the print syntax.
- class People:
- age=0
- gender='male'
- def __init__(self, age, gender):
- self.age = age
- self.gender = gender
- def toString(self):
- return 'Age:'+str(self.age)+'\tGender:'+self.gender
-
- List=[People(21,'male'),People(20,'famale'),People(34,'male'),People(19,'famale')]
- print('Befor sort:')
- for p in List:
- print(p.toString())
- List.sort(lambda p1,p2:cmp(p1.age,p2.age))
- print('\nAfter ascending sort:')
- for p in List:
- print(p.toString())
- List.sort(lambda p1,p2:-cmp(p1.age,p2.age))
- print('\nAfter descending sort:')
- for p in List:
- print(p.toString())
Key point: the Python syntax specifies a function that contains a parameter. This function is used to extract the keywords used for comparison between each element (it seems that this usage is clearer than lambda). Based on this prompt, finally, I want to compare the size of each element in the List. Each element is a People class and the age member is used as the comparison keyword. Therefore, it is easy to think of assigning a method to the key, this method returns age in People, so the modified Code is as follows:
- class People:
- age=0
- gender='male'
- def __init__(self, age, gender):
- self.age = age
- self.gender = gender
- def toString(self):
- return 'Age:'+str(self.age)+'\tGender:'+self.gender
-
- List=[People(21,'male'),People(20,'famale'),People(34,'male'),People(19,'famale')]
- print('Befor sort:')
- for p in List:
- print(p.toString())
- List.sort(lambda p1,p2:cmp(p1.age,p2.age))
- print('\nAfter ascending sort:')
- for p in List:
- print(p.toString())
- List.sort(lambda p1,p2:-cmp(p1.age,p2.age))
- print('\nAfter descending sort:')
- for p in List:
- print(p.toString())
I roughly read A Byte of Python two days ago, which is A basic possibility. I have written some simple examples and have a basic understanding of Python syntax. However, there are still some details that are not very clear, some of which may be unknown, but they still need to be further studied. Today I went to Dive into python and found that writing a simple but powerful program in the book still takes some time.
Function Definition: The function definition does not need to define the returned data type, and does not need to specify the parameter type. In Python, you never need to specify the Data Type of anything. For example, char * ret = (x! = 0 )? "True": "False" the python format corresponding to this line of code is ret = (x and "True") or "False". In fact, parentheses can be removed ).
During runtime, the python virtual opportunity evaluates the Boolean expression on the right of the value assignment operator (note that this is not a ternary expression), and the return value is the last value analyzed. Why is "last analyzed" instead of "last" in the expression? Because a Boolean expression has a short circuit effect, such as a or B, if a is true, B will not be analyzed.
- Introduction to Python Variables
- Full parsing of Python Inheritance Issues
- A summary of how to learn Python
- Summary of the Python program learning process
- How can I write Python better and faster?
Well, now we almost understand the principles of this line of python code. If x is True, because the string "True" is also True, "True" is returned. Otherwise, x is false, so there is no need to check the short-circuit effect of the string "True ), directly return "False ". It is not hard to see that ternary operations can be expressed by using Boolean values in Python syntax. Then, there may be some minor issues. For example, char * ret = x? "" Or "VAL ".
Based on the previous example, we naturally think that we should write this in python, ret = x and "" or "VAL ". Wrong! No matter whether the Boolean value of x is true or false, ret always gets "VAL ". Strange? It is not surprising that the Boolean value of an empty string in python is false, so that x and "" are always false, so ret always gets "VAL.
There are two ways to solve this problem. The first one is my favorite one, that is, writing ret = not x and "VAL" or "". Second, please try ret = x and [""] or ["VAL"] and use ret [0] As the return value each time, this is because the value of [""] in Boolean is true.