Starting Python
With no IT background
Background 背景
I’m currently a master student in astronomy, and majored in physics in university. Before my junior year in university, I had no experience in programming. Right now, I’m using python to analyze my data from telescopes and entertain myself.
我目前是一名天文碩士生,以前在大學主修物理,在大三以前沒有寫程式的經驗。目前我使用python處理天文上的數據以及寫寫程式娛樂自己。
Equip yourself 安裝
Getting familiar with the coding environment is the most struggling part for beginners in my opinion.
熟悉程式環境是第一步,我認為踏出第一步,把設定設好,準備好寫程式是最困難的
What we need is:
- Python
Of course we need python to execute any .py file - Editor (Integrated Developer Environment)
Any place can let you type - Packages
Some written programs can save you a lot effort
There are several ways to achieve these. As most people recommend, Anaconda is one of the best way to make codes on your computer.
Anaconda itself consists of python, IDE(spyder and jupyter notebook) and common packages.
有幾個方式可以讓我們準備好所需三元素:Python, 編輯器, 以及模組,使用Anaconda幫助我們直接把整個環境完成,提供了jupyter以及spyder供我們做編譯,以及常用的模組
Another way is using Google Colab.
Google Colab offers its server resources to the public, and you can do your project on the page without installing anything.
另外一個方式是使用Google Colab,Google提供伺服器供大眾在上面寫程式語言,不限於python,不需要額外安裝軟體,但如果程式吃太多資源或者運行時間過長,可能會被系統停止運行
Start programming 開始撰寫
Congratulations! You’ve passed the most difficult step. Now we can start to code. In anaconda, you can either use jupyter or spyder to program.


Here in my opinion, the best way to learn is to do projects and learn from mistakes. Unless you want to be an IT technician or you have IT background, I’ll recommend you to make codes that entertain you the most.
以非IT背景學習者來說,我會認為以任務導向去學習,會學習最快,我們從完成任務的目標當中,從做的過程學。
The very first simple code:
print('Hello World')
It may seem too easy, but when you do loops, print function is important to let you know the progress or the bugs in your code.
print function看似簡單,但在繁複迴圈時,可以透露出當前程式運行到哪個階段,或者在除錯時,找出出問題的程式碼。


The first project
As I said, projects are the best way to learn coding. Here I present the projectEuler problems. It provides hundreds of math problem which can be done by simple codes.
這裡第一個例子我會使用projectEuler上的題目,Project Euler提供上百個數學問題,可以用簡單程式來幫助我們運算。
Problem 1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Before we go further, you’re suggested to think about how would you solve this problem? What’s your solution if you solve it by hand and by computer?
Solution
Obviously, we can solve it by a pen and a paper, but it would take you hours.
The multiples of 3 are 3,6,9… and the sum can be easily expressed as

Therefore, what we need is the number of multiples of 3 and 5 and 15.
#To have number of multiples of each number
a_3 = 999//3 #number of multiples of 3 under 1000
a_5 = 999//5 #number of multiples of 5 under 1000
a_15 = 999//15 #number of multiples of 15 under 1000#taking the sum of 3 and 5 and subtract the common sum which is multiples of 15final = 3*((1+a_3)*a_3/2)+5*((1+a_5)*a_5/2)-15*((1+a_15)*a_15/2)print(final)