What exactly can you do with Python? Here are Python's 3 main applications.
你究竟能用Python做什麼?這是Python的3個主要應用程式。
If you're thinking of learning Python --- or if you recently started learning it --- you may be asking yourself:
如果您正在考慮學習Python ---或者如果您最近開始學習它 - 您可能會問自己:
"What exactly can I use Python for?"
Well that's a tricky question to answer, because there are so many applications for Python.
嗯,這是一個棘手的問題,因為Python有很多應用程式。
But over time, I have observed that there are 3 main popular applications for Python:
但隨著時間的推移,我發現Python有三個主要的流行應用程式:
- Web Development
- Data Science --- including machine learning, data analysis, and data visualization
- Scripting
Let's talk about each of them in turn.
讓我們依次談談它們中的每一個。
Web Development
Web frameworks that are based on Python like
基於Python的Web框架 Django and
and Flask have recently become very popular for web development.
最近變得非常流行的Web開發。
These web frameworks help you create server-side code (backend code) in Python. That's the code that runs on your server, as opposed to on users' devices and browsers (front-end code). If you're not familiar with the difference between backend code and front-end code, please see my footnote below.
這些Web框架可幫助您在Python中建立伺服器端程式碼(後端程式碼)。這是在您的伺服器上執行的程式碼,而不是使用者的裝置和瀏覽器(前端程式碼)。如果您不熟悉後端程式碼和前端程式碼之間的區別,請參閱下面的腳註。
But wait, why do I need a web framework?
That's because a web framework makes it easier to build common backend logic. This includes mapping different URLs to chunks of Python code, dealing with databases, and generating HTML files users see on their browsers.
這是因為Web框架使構建通用後端邏輯變得更容易。這包括將不同的URL對映到Python程式碼塊,處理資料庫以及生成使用者在其瀏覽器上看到的HTML檔案。
Which Python web framework should I use?
Django and Flask are two of the most popular Python web frameworks. I'd recommend using one of them if you're just getting started.
Django和Flask是兩個最流行的Python Web框架。如果你剛入門,我建議你使用其中一個。
What's the difference between Django and Flask?
There's an
There's an excellent article about this topic by Gareth Dwyer, so let me quote it here:
關於Gareth Dwyer的這個話題,讓我在這裡引用它:
<begin quote>
Main contrasts:
Main contrasts:
- Flask provides simplicity, flexibility and fine-grained control. It is unopinionated (it lets you decide how you want to implement things).
- Django provides an all-inclusive experience: you get an admin panel, database interfaces, an ORM [object-relational mapping], and directory structure for your apps and projects out of the box.
You should probably choose:
你應該選擇:
- Flask, if you're focused on the experience and learning opportunities, or if you want more control about which components to use (such as what databases you want to use and how you want to interact with them).
- Django, if you're focused on the final product. Especially if you're working on a straight-forward application such as a news site, an e-store, or blog, and you want there to always be a single, obvious way of doing things.
</end quote>
In other words, If you're a beginner, Flask is probably a better choice because it has fewer components to deal with. Also, Flask is a better choice if you want more customization.
換句話說,如果你是初學者,Flask可能是一個更好的選擇,因為它有更少的元件需要處理。此外,如果您想要更多自定義,Flask是更好的選擇。
On the other hand, if you're looking to build something straight-forward, Django will probably let you get there faster.
另一方面,如果你想要直接構建一些東西,Django可能會讓你更快地到達那裡。
Now, if you're looking to learn Django, I recommend the book called Django for Beginners. You can find it
現在,如果你想學習Django,我推薦名為Django for Beginners的書。你可以找到它here.
.
You can also find the free sample chapters of that book
您還可以找到該書的免費樣本章節here.
.
Okay, let's go to the next topic!
好的,我們來看下一個主題吧!
Data Science --- including machine learning, data analysis, and data visualization
First of all, let's review what machine learning is .
I think the best way to explain what machine learning is would be to give you a simple example.
我認為解釋機器學習的最佳方法是給你一個簡單的例子。
Let's say you want to develop a program that automatically detects what's in a picture.
假設您想要開發一個程式來自動檢測圖片中的內容。
So, given this picture below (Picture 1), you want your program to recognize that it's a dog.
因此,如下圖(圖1),您希望程式識別出它是一隻狗。
Given this other one below (Picture 2), you want your program to recognize that it's a table.
鑑於下面的另一個(圖2),您希望程式識別它是一個桌子。
You might say, well, I can just write some code to do that. For example, maybe if there are a lot of light brown pixels in the picture, then we can say that it's a dog.
你可能會說,好吧,我可以寫一些程式碼來做到這一點。例如,如果圖片中有很多淺棕色畫素,那麼我們可以說它是一隻狗。
Or maybe, you can figure out how to detect edges in a picture. Then, you might say, if there are many straight edges, then it's a table.
或者,您可以弄清楚如何檢測圖片中的邊緣。然後,你可能會說,如果有很多直邊,那麼它就是一張桌子。
However, this kind of approach gets tricky pretty quickly. What if there's a white dog in the picture with no brown hair? What if the picture shows only the round parts of the table?
但是,這種方法很快變得棘手。如果照片中有一隻沒有棕色頭髮的白狗怎麼辦?如果圖片只顯示錶格的圓形部分怎麼辦?
This is where machine learning comes in.
Machine learning typically implements an algorithm that automatically detects a pattern in the given input.
機器學習通常實現一種自動檢測給定輸入中的模式的演算法。
You can give, say, 1,000 pictures of a dog and 1,000 pictures of a table to a machine learning algorithm. Then, it will learn the difference between a dog and a table. When you give it a new picture of either a dog or a table, it will be able to recognize which one it is.
你可以給機器學習演算法給1000張狗的照片和1000張桌子的照片。然後,它將學習狗和桌子之間的區別。當你給它一張狗或桌子的新圖片時,它將能夠識別它是哪一個。
I think this is somewhat similar to how a baby learns new things. How does a baby learn that one thing looks like a dog and another a table? Probably from a bunch of examples.
我認為這有點類似於嬰兒學習新事物的方式。寶寶怎麼知道一件事看起來像狗,另一件看起來像一張桌子?可能來自一堆例子。
You probably don't explicitly tell a baby, "If something is furry and has light brown hair, then it's probably a dog."
你可能沒有明確地告訴嬰兒,"如果有毛茸茸的東西,有淺棕色的頭髮,那麼它可能是一隻狗。"
You would probably just say, "That's a dog. This is also a dog. And this one is a table. That one is also a table."
你可能會說,"那隻狗是狗。這也是一隻狗。這只是一張桌子。那張桌子也是一張桌子。"
Machine learning algorithms work much the same way.
機器學習演算法的工作方式大致相同。
You can apply the same idea to:
您可以將相同的想法應用於:
- recommendation systems (think YouTube, Amazon, and Netflix)
- face recognition
- voice recognition
among other applications.
等應用程式。
Popular machine learning algorithms you might have heard about include:
您可能聽說過的流行機器學習演算法包括:
- Neural networks
- Deep learning
- Support vector machines
- Random forest
You can use any of the above algorithms to solve the picture-labeling problem I explained earlier.
您可以使用上述任何演算法來解決我之前解釋過的圖片標註問題。
Python for machine learning
There are popular machine learning libraries and frameworks for Python.
有流行的機器學習庫和Python框架。
Two of the most popular ones are
兩個最受歡迎的是 scikit-learn and
and TensorFlow .
.
- scikit-learn comes with some of the more popular machine learning algorithms built-in. I mentioned some of them above.
- TensorFlow is more of a low-level library that allows you to build custom machine learning algorithms.
If you're just getting started with a machine learning project, I would recommend that you first start with scikit-learn. If you start running into efficiency issues, then I would start looking into TensorFlow.
如果您剛開始使用機器學習專案,我建議您先從scikit-learn開始。如果你開始遇到效率問題,那麼我會開始研究TensorFlow。
How should I learn machine learning?
To learn machine learning fundamentals, I would recommend either
要學習機器學習基礎知識,我建議Stanford's or
or Caltech's machine learning course.
機器學習課程。
Please note that you need basic knowledge of calculus and linear algebra to understand some of the materials in those courses.
請注意,您需要微積分和線性代數的基本知識才能理解這些課程中的一些材料。
Then, I would practice what you've learned from one of those courses with
然後,我會練習你從其中一門課程中學到的東西Kaggle. It's a website where people compete to build the best machine learning algorithm for a given problem. They have nice tutorials for beginners, too.
。這是一個人們競爭為特定問題構建最佳機器學習演算法的網站。他們也為初學者提供了很好的教程。
What about data analysis and data visualization?
To help you understand what these might look like, let me give you a simple example here.
為了幫助您瞭解這些可能是什麼樣子,讓我在這裡給您一個簡單的例子。
Let's say you're working for a company that sells some products online.
假設您正在為一家線上銷售某些產品的公司工作。
Then, as a data analyst, you might draw a bar graph like this.
然後,作為資料分析師,您可以繪製這樣的條形圖。
Bar Chart 1 --- generated with Python
條形圖1 ---用Python生成
From this graph, we can tell that men bought over 400 units of this product and women bought about 350 units of this product this particular Sunday.
從這張圖中,我們可以看出,男性購買了超過400個單位的產品,女性在這個特定的星期天購買了約350個單位的產品。
As a data analyst, you might come up with a few possible explanations for this difference.
作為資料分析師,您可能會對這種差異提出一些可能的解釋。
One obvious possible explanation is that this product is more popular with men than with women. Another possible explanation might be that the sample size is too small and this difference was caused just by chance. And yet another possible explanation might be that men tend to buy this product more only on Sunday for some reason.
一個明顯可能的解釋是,這種產品比男性更受男性歡迎。另一種可能的解釋可能是樣本量太小而且這種差異只是偶然造成的。而另一種可能的解釋可能是,男性傾向於僅在週日因某種原因購買該產品。
To understand which of these explanations is correct, you might draw another graph like this one.
要了解哪些解釋是正確的,您可能會繪製另一個這樣的圖表。
Line Chart 1 --- generated with Python
折線圖1 ---用Python生成
Instead of showing the data for Sunday only, we're looking at the data for a full week. As you can see, from this graph, we can see that this difference is pretty consistent over different days.
我們不是僅顯示星期日的資料,而是檢視整整一週的資料。如您所見,從這張圖中,我們可以看到這種差異在不同的日子裡非常一致。
From this little analysis, you might conclude that the most convincing explanation for this difference is that this product is simply more popular with men than with women.
從這個小小的分析中,你可以得出結論,對這種差異最有說服力的解釋是,這種產品更容易受到男性的歡迎,而不是女性。
On the other hand, what if you see a graph like this one instead?
另一方面,如果你看到像這樣的圖表怎麼辦?
Line Chart 2 --- also generated with Python
折線圖2 ---也是用Python生成的
Then, what explains the difference on Sunday?
那麼,是什麼解釋了週日的差異?
You might say, perhaps men tend to buy more of this product only on Sunday for some reason. Or, perhaps it was just a coincidence that men bought more of it on Sunday.
你可能會說,也許男人往往只是因為某種原因在週日購買更多的這種產品。或者,也許只是巧合,週日男人買了更多。
So, this is a simplified example of what data analysis might look like in the real world.
因此,這是資料分析在現實世界中可能看起來像的簡化示例。
The data analysis work I did when I was working at Google and Microsoft was very similar to this example --- only more complex. I actually used Python at Google for this kind of analysis, while I used JavaScript at Microsoft.
我在谷歌和微軟工作時所做的資料分析工作與這個例子非常相似 - 只是更復雜。我實際上在谷歌使用Python進行這種分析,而我在微軟使用JavaScript。
I used SQL at both of those companies to pull data from our databases. Then, I would use either Python and Matplotlib (at Google) or JavaScript and D3.js (at Microsoft) to visualize and analyze this data.
我在這兩家公司使用SQL來從我們的資料庫中提取資料。然後,我會使用Python和Matplotlib(在谷歌)或JavaScript和D3.js(在微軟)來視覺化和分析這些資料。
Data analysis / visualization with Python
One of the most popular libraries for data visualization is
最受歡迎的資料視覺化庫之一是Matplotlib.
.
It's a good library to get started with because:
這是一個很好的庫,因為:
- It's easy to get started with
- Some other libraries such as seaborn is based on it. So, learning Matplotlib will help you learn these other libraries later on.
How should I learn data analysis / visualization with Python?
You should first learn the fundamentals of data analysis and visualization. When I looked for good resources for this online, I couldn't find any. So, I ended up making a YouTube video on this topic:
您應該首先了解資料分析和視覺化的基礎知識。當我在網上尋找好的資源時,我找不到任何東西。所以,我最終制作了關於這個主題的YouTube視訊:
Intro to Data Analysis / Visualization with Python and Matplotlib
使用Python和Matplotlib介紹資料分析/視覺化
I also ended up making a
我也最終做了一個full course on this topic on Pluralsight, which you can take for free by signing up to their 10-day free trial.
,您可以免費註冊10天免費試用版。
I'd recommend both of them.
我推薦他們兩個。
After learning the fundamentals of data analysis and visualization, learning fundamentals of statistics from websites like Coursera and Khan Academy will be helpful, as well.
在學習了資料分析和視覺化的基礎知識之後,從Coursera和Khan Academy等網站學習統計資料的基礎知識也會有所幫助。
Scripting
What is scripting?
Scripting usually refers to writing small programs that are designed to automate simple tasks.
指令碼通常是指編寫旨在自動執行簡單任務的小程式。
So, let me give you an example from my personal experience here.
那麼,讓我舉一個我個人經歷的例子。
I used to work at a small startup in Japan where we had an email support system. It was a system for us to respond to questions customers sent us via email.
我曾經在日本的一家小型創業公司工作,我們有一個電子郵件支援系統。這是一個系統,讓我們回答客戶通過電子郵件傳送給我們的問題。
When I was working there, I had the task of counting the numbers of emails containing certain keywords so we could analyze the emails we received.
當我在那裡工作時,我的任務是計算包含某些關鍵字的電子郵件的數量,以便我們分析收到的電子郵件。
We could have done it manually, but instead, I wrote a simple program / simple script to automate this task.
我們可以手動完成它,但是,我編寫了一個簡單的程式/簡單指令碼來自動執行此任務。
Actually, we used Ruby for this back then, but Python is also a good language for this kind of task. Python is suited for this type of task mainly because it has relatively simple syntax and is easy to write. It's also quick to write something small with it and test it.
實際上,我們當時使用Ruby,但Python也是這類任務的好語言。 Python適合這種型別的任務,主要是因為它具有相對簡單的語法並且易於編寫。用它寫一些小東西並測試它也很快。
What about embedded applications?
I'm not an expert on embedded applications, but I know that Python works with Rasberry Pi. It seems like a popular application among hardware hobbyists.
我不是嵌入式應用程式方面的專家,但我知道Python可以與Rasberry Pi一起使用。它似乎是硬體愛好者中的一種流行應用。
What about gaming?
You could use the library called PyGame to develop games, but it's not the most popular gaming engine out there. You could use it to build a hobby project, but I personally wouldn't choose it if you're serious about game development.
您可以使用名為PyGame的庫來開發遊戲,但它並不是最流行的遊戲引擎。你可以用它來建立一個愛好專案,但如果你認真對待遊戲開發,我個人不會選擇它。
Rather, I would recommend getting started with Unity with C#, which is one of the most popular gaming engines. It allows you to build a game for many platforms, including Mac, Windows, iOS, and Android.
相反,我建議開始使用Unity與C#,這是最受歡迎的遊戲引擎之一。它允許您為許多平臺構建遊戲,包括Mac,Windows,iOS和Android。
What about desktop applications?
You could make one with Python using Tkinter, but it doesn't seem like the most popular choice either.
您可以使用Tkinter製作一個Python,但它似乎也不是最受歡迎的選擇。
Instead, it seems like languages like
相反,它似乎像語言Java, C#, and C++ are more popular for this.
對此更受歡迎。
Recently, some companies have started using JavaScript to create Desktop applications, too.
最近,一些公司也開始使用JavaScript來建立桌面應用程式。
For example, Slack's desktop app was built with something called Electron. It allows you to build desktop applications with JavaScript.
。它允許您使用JavaScript構建桌面應用程式。
Personally, if I was building a desktop application, I would go with a JavaScript option. It allows you to reuse some of the code from a web version if you have it.
就個人而言,如果我正在構建一個桌面應用程式,我會使用JavaScript選項。它允許您重用Web版本中的一些程式碼(如果有的話)。
However, I'm not an expert on desktop applications either, so please let me know in a comment if you disagree or agree with me on this.
但是,我也不是桌面應用程式的專家,所以如果您不同意或同意我的意見,請在評論中告訴我。
Python 3 or Python 2?
I would recommend Python 3 since it's more modern and it's a more popular option at this point.
我會推薦Python 3,因為它更現代,而且在這一點上它是一個更受歡迎的選項。
Footnote: A note about back-end code vs front-end code (just in case you are not familiar with the terms):
Let's say you want to make something like Instagram.
假設您想製作像Instagram這樣的東西。
Then, you'd need to create front-end code for each type of device you want to support.
然後,您需要為要支援的每種型別的裝置建立前端程式碼。
You might use, for example:
您可以使用,例如:
- Swift for iOS
- Java for Android
- JavaScript for web browsers
Each set of code will run on each type of device / browser. This will be the set of code that determines what the layout of the app will be like, what the buttons should look like when you click them, etc.
每組程式碼都將在每種型別的裝置/瀏覽器上執行。這將是一組程式碼,用於確定應用程式的佈局如何,單擊按鈕時的外觀等等。
However, you will still need the ability to store users' info and photos. You will want to store them on your server and not just on your users' devices so each user's followers can view his/her photos.
但是,您仍然需要能夠儲存使用者的資訊和照片。您需要將它們儲存在伺服器上,而不僅僅儲存在使用者的裝置上,以便每個使用者的關注者都可以檢視他/她的照片。
This is where the backend code / server-side code comes in. You'll need to write some backend code to do things like:
這是後端程式碼/伺服器端程式碼的用武之地。您需要編寫一些後端程式碼來執行以下操作:
- Keep track of who's following who
- Compress photos so they don't take up so much storage space
- Recommend photos and new accounts to each user in the discovery feature
So, this is the difference between backend code and front-end code.
因此,這是後端程式碼和前端程式碼之間的區別。
By the way, Python is not the only good choice for writing backend / server-side code. There are many other popular choices, including Node.js, which is based on JavaScript.
順便說一下,Python並不是編寫後端/伺服器端程式碼的唯一好選擇。還有許多其他流行的選擇,包括Node.js,它基於JavaScript。
Anyway, thanks a lot for reading my article!
公眾號:銀河系1號
聯絡郵箱:public@space-explore.com
(未經同意,請勿轉載)