Python tricks and tricks-continue to be updated...

Python Wonderful Skills#

Life is short, I use Python;

This is definitely not an empty talk in the programming world, especially for students who have used multiple languages for work. The longer you use Python, the more you have an idea of why I went there early. It’s okay. Whenever I use Python Not too late, this sharing mainly focuses on various classic usages of Python, interesting Easter eggs, etc., with only one purpose, so that everyone can feel the charm of Python;

This sharing plan is continuously updated, so everyone can follow me or bookmark this post. It will be updated from time to time. Everyone's black operations can also be given in the comment area. If it makes sense, I will update to the post. In, now let's start the topic;

Python and me##

The earliest use of Python was 16 years. At that time, my work was mainly Android development, responsible for the navigation part. At that time, road network data and navigation location data were often displayed when debugging functions. However, the debugging cost is relatively large. Operation, secondly, navigation requires a process, and it cannot be skipped well (simulated navigation is still different from actual navigation), so after the boss suggested to start learning Python, the original purpose was to draw road network data and visualize it through Python combined with Matplotlib. Since then, it’s out of control. Until now, basically any over-repetitive work will be basically completed through scripts. For example, the most recent one was to extract 3w of them from the data in a csv file from 5w files. Of course There are many ways to achieve it, but I have to say that Python 5 lines of code are really cool. This is the story between me and Python. Of course, after starting machine learning projects, I use Python more. These are not. Too much part of this article to share;

text##

First blood

I thought about the first point for a long time, because Python is really commendable, but which one is the most representative? It may not be the coolest, not necessarily the shortest, here I Selection list comprehension, shocked the first time I saw it, ultra-short code, cool appearance, powerful internal, it is really powerful, anyway, when I think of Python, the first thing in my mind is it. , You can also share the first feature of Python in your mind;

Code:

	#Calculate the probability of tossing 1,000 coins to get heads
	sum([int(round(random()))for x inrange(1000)])/1000.
	#According to the central limit theorem, this value will increasingly concentrate to the theoretical value 0 as the number of times increases..5 nearby

Take a look. It’s really cool. Although it’s not recommended in the project, but when it’s used in a script, it’s not your own decision. The script is cool, right? (PS: Note that the last 1000 is followed by a . Means decimal, otherwise the result will be 0, here I use Python2.7);

second round###

Requirement: There are multiple conditions, or bool type variables. The purpose is to determine whether all of them are true. As long as one of them is true, loops are likely to be used in other languages. But how does Python deal with it?

Code:

	x =[True, True, False]ifany(x):
	  print 'At least one is true'ifall(x):
	  print 'All true'ifany(x) and not all(x):
	  print 'At least one is true and one is false'

This time is not only refreshing, but also very readable, which is invincible. We know that the readability of the code written at work is basically the first thing. Who has changed the code that God can’t understand? I know, there is no place to talk about the pain in my heart, xD;

The third time###

In the loop, the general processing is to loop the subscript, indexing the element by the subscript, then if I loop to get the element, see how Python gets the subscript and the element at the same time;

Code:

	a =[8,23,45,12,78]for index, value inenumerate(a):print(index , value)

emumerate is very commonly used in Python, and very practical;

Fourth return###

This time I want to talk about a simple, but very characteristic of Python, exchange variables, when we are exchanging two variables (or more, the more this is, the more the advantages of this feature of Python can be reflected), we usually need temporary variables to do Transfer to see how Python does it;

Code:

	a=1
	b=2
	c=3
	a,b,c = b,c,a

This feature is very simple, and it is used a lot, but I think it represents the philosophy of Python, less is more, I really am a philosopher;

Fifth return###

The fifth round is the last round of the first Python sharing. I want to end with Python's easter eggs. I think this is also the difference between Python as a language. It is not serious and straightforward, but flexible and interesting;

At last##

I hope you can actively add that all kinds of brain-hole manipulation and spirituality have been engaged, and the above are just my family's words, so as to make a point.

Recommended Posts

Python tricks and tricks-continue to be updated...
Do python programs need to be compiled
How to use and and or in Python
Does Python code need to be indented
01. Introduction to Python
Python and Go
Python uses PIL to cut and stitch pictures
How to delete files and directories in python
How to read and write files with Python
Introduction to Python
Python reads the result and writes it to Excel
Centos 6.4 python 2.6 upgrade to 2.7
[python] python2 and python3 under ubuntu
Python deconstruction and packaging
Python3 configuration and entry.md
Python | An article to understand Python list, tuple and string operations