<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Python 基本語法與科學計算套件的使用： Python 基礎(一)
四堂課程大綱
第一堂-Python 基礎(一)：Python 簡介及環境建立、Python程式的編寫及執行、資料型態、基本輸入輸出、流程控制
第二堂-Python 基礎(二)：檔案讀寫、例外處理、函數、模組、物件導向
第三堂-Python科學計算套件(一)：Numpy、Matplotlib 
第四堂-Python科學計算套件(二)：Scipy、Astropy
Python 簡介
<a href="https
Step1:  基本輸出輸入
Step2:  強型別
轉型
Step3:  對變數的操作
Step4:  資料型態(List、Tuple、Dictionary)
簡介
1. Tuple
內容不可變
Step5:  tuple 是有順序但不可以變動
Step6:  list 是有順序且可以變動
Step7:  雙重list
Step8:  須注意指定問題
Step9:  判斷項目是否存在於list中 => in
Step10:  排序
Step11:  dictionary 是無順序且可以變動
Step12:  新增合併字典
Step13:  判斷key是否存在
Step14:  取得key, value
Step15:  Slicing
Step16:  流程控制：條件判斷式及迴圈
依據某些條件(條件判斷式)或多次執行某個程式區塊(迴圈)，來控制程式的執行流程
Python使用冒號及縮排來辨別程式區塊
if...else if...else 條件判斷式
Step17:  for 迴圈
Step18:  while 迴圈
Step19:  break 及 continue
<ASSISTANT_TASK:>
Python Code:
# ←此為Python的註解符號，在這之後的文字不會被當作程式碼執行
# Python不用宣告變數型態，在指定變數的值時即會動態決定其型態
n_solar_mass = 10                                           # 整數
MASS_SUN = 1.99 * 10 ** 30                        # 浮點數
z = complex(3., -1.)                                          # 複數                  
unit  = "kg"                                                         #字串
yes = True                                                         #布林值
no = False                                                         #布林值
type(n_solar_mass)
Explanation: Python 基本語法與科學計算套件的使用： Python 基礎(一)
四堂課程大綱
第一堂-Python 基礎(一)：Python 簡介及環境建立、Python程式的編寫及執行、資料型態、基本輸入輸出、流程控制
第二堂-Python 基礎(二)：檔案讀寫、例外處理、函數、模組、物件導向
第三堂-Python科學計算套件(一)：Numpy、Matplotlib 
第四堂-Python科學計算套件(二)：Scipy、Astropy
Python 簡介
<a href="https://www.python.org/" target="_blank">Python</a> 是一種<a href="https://en.wikipedia.org/wiki/Interpreted_language" target="_blank">直譯式的程式語言</a>
Python的擴充性：<a href="https://docs.python.org/3/library/" target="_blank">標準函式庫</a>+<a href="https://pypi.python.org/pypi" target="_blank">眾多的第三方套件</a>
<a href="http://www.astro.ncu.edu.tw/~yhsu/yhsu/%E5%BB%BA%E7%AB%8B%E7%94%A8%E6%96%BC%E7%A7%91%E5%AD%B8%E8%A8%88%E7%AE%97%E7%9A%84python%E9%96%8B%E7%99%BC%E7%92%B0%E5%A2%83/" target="_blank">建立用於科學計算的Python開發環境</a>
示範如何編輯及執行Python程式: Python/<a href="http://ipython.org/" target="_blank">IPython</a>互動式命令列介面、文字編輯器(<a href="https://atom.io/">Atom</a>、<a href="http://linux.vbird.org/linux_basic/0310vi.php" target="_blank">Vim</a>、<a href="http://www.sublimetext.com/3" target="_blank">Sublime Text 3</a>、<a href="https://notepad-plus-plus.org/" target="_blank">Notepad++</a>...)、<a href="https://wiki.python.org/moin/IntegratedDevelopmentEnvironments" target="_blank">整合開發環境</a>(Anaconda內建的<a href="http://pythonhosted.org/spyder/" target="_blank">Spyder</a>、<a href="https://www.enthought.com/products/canopy/" target="_blank">Enthought Canopy</a>...)、<a href="http://ipython.org/notebook.html" target="_blank">IPython Notebook </a>(可將程式碼、圖、筆記統整在一份文件中，並可儲存成其他格式分享給別人)
資料型態(整數、浮點數、複數、字串、布林值)及基本輸出輸入
Python的變數命名是會區分大小寫
動態定型
End of explanation
print("伴星的質量為：", n_solar_mass * MASS_SUN, unit)
perid = input("請輸入雙星軌道周期 (單位為秒) : ")
Explanation: 基本輸出輸入
End of explanation
MASS_SUN + unit
str(MASS_SUN) + unit
Explanation: 強型別
轉型
End of explanation
s = "Hello," + " " + "Python!"
#s.upper()
s.split()
Explanation: 對變數的操作
End of explanation
x = (1,2)
x
x[0] = 12  #無法更改內容
Explanation: 資料型態(List、Tuple、Dictionary)
簡介
1. Tuple
內容不可變
End of explanation
# 延續上面範例
constants = (3.14159, 1.99 * 10 ** 30 ,  6.67 * 10 ** -11)   # tuple用小括號
print(constants)
constants.append(-342)
x = (10,11)
y = list(x) #把tuple製作成串列
y[0] = 20
y
Explanation: tuple 是有順序但不可以變動
End of explanation
slist = []
slist = ['A','B','C','D','E']
slist
slist[0] = 'B'
slist
# 延續上面範例
radial_velocity = [140, 220, 314, 244, 'km/s']    # list用中括號
print(radial_velocity[0])                                        # 元素的index從零開始
radial_velocity.append(-342)
radial_velocity.insert(2, 592)
print(radial_velocity)
print(len(radial_velocity))
Explanation: list 是有順序且可以變動
End of explanation
dlist = [['A','B','C','D','E'],[1,2,3,4,5]]
dlist[1]
dlist[1][0]
Explanation: 雙重list
End of explanation
a = [1,2,3]
a
b = a
b
b[0] = 10
b
a # list a 中的數值也一起改變
# 如何解決？
c = a[:]
c
c[0] = 40
c
a
Explanation: 須注意指定問題
End of explanation
clist = ['A','B','C','D','E']
'B' in clist
'F' in clist
Explanation: 判斷項目是否存在於list中 => in
End of explanation
wlist = ['D','A','F','C','E','D']
sorted(wlist)
nlist = [4,23,1,3,2,98,3]
sorted(nlist)
Explanation: 排序
End of explanation
dic = {'Jack':84,'Ben':63,'Cathy':76, 'Bob': 83}
dic
dic['Jack']
dic = {'Jack':[84,23,34],'Ben':[63,12,74],'Cathy':[12,43,76],'Bob': [83,81,90]} # key 不能放list value可以
dic 
dic['Jack']
dic['Jack'] = [1,2,3]
dic
Explanation: dictionary 是無順序且可以變動
End of explanation
dic.update({'William':[23,43,84],'Eric':[93,31,32]})
dic
Explanation: 新增合併字典
End of explanation
'Ben' in dic
'William' in dic 
Explanation: 判斷key是否存在
End of explanation
dic.keys() #取得key not list
dic.values() #取得value not list
dic.items() #取得所有的key, value not list
# 延續上面範例
binaries = {'name':["GX 339-4","GRS 1915+105"], 'constants': constants}                   # dictionary用大括號
print(binaries['name'])
print(binaries['constants'])
binaries['mass'] = [ ]
print(binaries)
Explanation: 取得key, value
End of explanation
print(radial_velocity[2:5])                        # [i:j] 從i開始到j-1
print(radial_velocity[:-2:3])                     # [i:j:k] 從i開始每隔k個到j-1
Explanation: Slicing
End of explanation
if "GRS 1915+105" or "GX 339-4" in binaries['name']:
    print('They are microquasars!')
elif "XTE J1550-564" not in binaries['name']:
    print('XTE J1550-564 is not in the list!')
else:
    binaries['name'].append( input("請輸入下一個microquasars的名稱 : ") )
binaries['name']
Explanation: 流程控制：條件判斷式及迴圈
依據某些條件(條件判斷式)或多次執行某個程式區塊(迴圈)，來控制程式的執行流程
Python使用冒號及縮排來辨別程式區塊
if...else if...else 條件判斷式
End of explanation
# Basic example 1.
for i in range(10):
    print(i)
# Basic example 2.
arr = [1,3,5,7,9,2,4,6,8,10]
for i in arr:
    print(i)
microquasars = ["GRS 1915+105" , "GX 339-4"]
for m in microquasars:
    if m in binaries['name']:
        print(m, "is a microquasar!")
        
for i in range(2, 100, 20):
    print(i, i ** 2)
# Plot a light curve example
# (Read a LMC X-4 archival data collecting by RXTE/PCA)
import numpy as np
data = np.loadtxt('../files4examples/Tcol_10135-01-01-000_gx0')  # a list of X-ray photon arrival times
# elements of data
n = len(data)
binsize = 20.0                                     # unit: sec
n_col = (data[n-1]-data[0]) // binsize + 1
bintime = np.arange(0, n_col)*binsize + data[0] + 0.5*binsize
crate = np.arange(0, n_col)*0.0                    # an empty array
## for-loop case a.
#for i in range(int(n_col)):
#    for j in range(n):
#        if (data[j] >= bintime[i]-0.5*binsize) and (data[j] < bintime[i]+0.5*binsize):
#            crate[i] = crate[i]+ 1.0
#crate = crate/binsize
## The value used in the function of 'range' must be a integral number.
## Or typing 'range?' to check.
## for-loop case b.
#for i in bintime:
#    for j in data:
#        if (j >= i-0.5*binsize) and (j < i+0.5*binsize):
#            crate[np.where(bintime == i)] = crate[np.where(bintime == i)] + 1.0
#crate = crate/binsize
## for-loop case c.
for i,val in enumerate(bintime):
    nn = np.where((data >= val - 0.5*binsize) & (data < (val + 0.5*binsize)))
    crate[i] = len(nn[0])
crate = crate/binsize
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(bintime,crate)
#plt.show()
Explanation: for 迴圈
End of explanation
# Basic example 
x = 10
while x > 0:
    print (x)
    x = x - 1
input_value = "yes"
while input_value == 'yes' :
    input_value = input("請輸入下一個microquasars的名稱，結束請輸入no : ")
Explanation: while 迴圈
End of explanation
input_value == "yes"
while True :
    input_value = input("請輸入下一個microquasars的名稱，結束請輸入no : ")
    if input_value == "no":
        break
for i in range(2, 100, 20):
    if i == 2 or i == 42:
        continue
    print(i, i ** 2)
Explanation: break 及 continue
End of explanation
<END_TASK>