亞洲大學基礎程式設計教材(AUP110-Fundamentals of Programming)¶
#Week1 Python 程式語言和IPO-model
Python 是一種腳本程式語言,意思是只要寫好一行的指令就可以執行。而Google Colab提供最簡單的方式來編寫和執行Python程式語言。在下面每一個程式區塊(code cell) 都可以單獨執行。而比較大的程式,程式可以跨區塊執行。
Python是易學、功能強大的程式語言。在本課程中,我們會介紹Python基本語法(變數、運算、流程控制和資料結構)等,讓同學在未來可以做為學習進階資料科學或深度學習等課程的基礎。
完成第一個程式很簡單,在所有程式語言,第一個程式叫Hello程式,Python 的Hello程式超簡單,就是下面一行:
print('Hello World')
#把Hello World程式貼在這個下面,然後按左側的執行鍵或(Ctrl+Enter鍵)....
print('Hello World')
Hello World
在上面程式,我們使用了一個函數print(),print會合併其輸入的參數,然後,輸出一行的文字。
下面我們有各種Hello World程式的變型。大家可以觀察其不同。
Topic 1(主題1)-Hello World程式¶
Step 1: 各種Hello World的寫法¶
print('Hello World!')
Hello World!
print('Hello '+'World!')
Hello World!
print("Hello World!")
Hello World!
print("Hello"); print("World!")
Hello
World!
print("Hello World!") #註解是不會執行的
Hello World!
Python的字串可以用單引號或雙引號
Topic 2(主題2)-IPO model¶
Step 1: 使用input()函數輸入變數的值¶
Python中使用input()函數讀入輸入的資料。
函數語法: input([prompt]) 參數prompt是提示訊息,跟輸入的值無關。
呼叫一次input()函數會輸入一個字串。
name = input('Please input your name:')
print('Hello, ', name)
print(type(name)) #列印變數的型別
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
/tmp/ipykernel_748/1873078738.py in <module>
----> 1 name = input('Please input your name:')
2 print('Hello, ', name)
3 print(type(name)) #列印變數的型別
~/.local/lib/python3.8/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
1001 """
1002 if not self._allow_stdin:
-> 1003 raise StdinNotImplementedError(
1004 "raw_input was called, but this frontend does not support input requests."
1005 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
Step 2: Assignment statement (指定敘述)¶
Assignment (賦值) 符號”=”
左邊是Variables (變數)
右邊是有型別(date type)的值 (value)
#Numbers
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = 'John' # A string
Step 3: Python 二個內建基本函數help()和 type()¶
help() 函數說明
type() 變數類別
help(type)
help(input)
type(counter)
type(miles)
type(name)
Step 4: 用print()函數輸出變數的值¶
help(print)
print(miles) #列印變數的值
print(type(miles)) #列印變數的型別
print(name) #列印變數的值
print(type(name)) #列印變數的型別
#Numbers
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = 'John' # A string
print(counter) #列印變數的值
print(type(counter)) #列印變數的型別
my = True
print(my) #列印變數的值
print(type(my)) #列印變數的型別
my = False
print(my) #列印變數的值
print(type(my)) #列印變數的型別
my = None
print(my) #列印變數的值
print(type(my)) #列印變數的型別