Each novice programmer must know Python skills

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

>>> my_string = "Hi Medium..!"
>>> print(my_string * 2)
Hi Medium..!Hi Medium..!
>>> print(my_string + " I love Python" * 2)
Hi Medium..! I love Python I love Python
复制代码

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.

>>> print(my_string[::-1])
!..muideM iH
>>> my_list = [1,2,3,4,5]
>>> print(my_list[::-1])
[5, 4, 3, 2, 1]
复制代码

So for the list contains multiple strings of it, we can even do a Yoda-translator!

>>> word_list = ["awesome", "is", "this"]
>>> print(' '.join(word_list[::-1]) + '!')
this is awesome!
复制代码

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

>>> def stupid_func(x):219539519欢迎加入
>>>     return x**2 + 5
复制代码

Now if we want the function to a list of all the odd of them, if not understand list comprehensions, so you might write

>>> my_list = [1, 2, 3, 4, 5]
>>> new_list = []
>>> for x in my_list:
>>>     if x % 2 != 0:
>>>         new_list.append(stupid_func(x))
>>> print(new_list)
[6, 14, 30]
复制代码

But we have an easier way!

>>> my_list = [1, 2, 3, 4, 5]
>>> print([stupid_func(x) for x in my_list if x % 2 != 0])
[6, 14, 30]
复制代码

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

>>> for item in list:
>>>     if conditional:
>>>         expression
复制代码

Cool, but we can do better, because we do not need the function "stupid_func"

>>> print([x ** 2 + 5 for x in my_list if x % 2 != 0])
[6, 14, 30]
复制代码

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

>>> stupid_func = (lambda x : x ** 2 + 5)
>>> print([stupid_func(1), stupid_func(3), stupid_func(5)])
[6, 14, 30]
复制代码

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 ()

>>> my_list = [2, 1, 0, -1, -2]
>>> print(sorted(my_list))
[-2, -1, 0, 1, 2]
复制代码

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

>>> print(sorted(my_list, key = lambda x : x ** 2))
[0, -1, 1, -2, 2]
复制代码

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

>>> print(list(map(lambda x, y : x * y, [1, 2, 3], [4, 5, 6])))
[4, 10, 18]
复制代码

The following code and compared with a combination of Lambda Map is too elegant a

>>> x, y = [1, 2, 3], [4, 5, 6]
>>> z = []
>>> for i in range(len(x)):
>>>     z.append(x[i] * y[i])
>>> print(z)
[4, 10, 18]
复制代码

Else if elif and use condition judgment on one line

 

Sometimes, you might write the following code

>>> x = int(input())
>>> if x >= 10:
>>>     print("Horse")
>>> elif 1 < x < 10:
>>>     print("Duck")
>>> else:
>>>     print("Baguette")
复制代码

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

print("Horse" if x >= 10 else "Duck" if 1 < x < 10 else "Baguette")
复制代码

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 ()!

>>> first_names = ["Peter", "Christian", "Klaus"]
>>> last_names = ["Jensen", "Smith", "Nistrup"]
>>> print([' '.join(x) for x in zip(first_names, last_names)])
['Peter Jensen', 'Christian Smith', 'Klaus Nistrup']
复制代码

Oh wow, there's wrong, my name is not Peter Jensen, then it can be adjusted as follows

>>> print([' '.join(x) for x in zip(first_names, last_names[::-1])])
['Peter Nistrup', 'Christian Smith', 'Klaus Jensen']

复制代码

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!

Guess you like

Origin www.cnblogs.com/Pythonjiaochen/p/11640496.html