基本語法
型別
Number 數字 String 字串 boolean 布林 List 列表 Tuple 元組 Dictionary 字典 (Map)
列表
list_data = [1, 'string', 3.1, [5, 6, 7]]
print list_data #完整
print list_data[0] #第一個
print list_data[1:3] #第二個到第三個
print list_data[1:] #第二個到最後一個
#getIndex
list_data.index('string') #1
#新增
list_data.append('mouse') #新增在最後
list_data.insert(1,'mouse') #新增在1的位置
#刪除
list_data.remove('string')
#排序
list_data.sort()
字典
和 List 用 [] 來包資料不同 Dictionary 是用 {}
my_dic = {
'name': 'apple',
'country': 'taiwan',
'luckyNumber': 8
}
#取key
my_dic.keys()
#取value
my_dic.values()
讀取使用者的輸入內容
不管使用者輸入什麼,都是字串型別 input()
Number to String
str()
String to Integer
int()
String to float
float()
Boolean
Python 的 True False 都要大寫
&& => and || => or ! => not
print('1',True and True) #True
print('2',True and False) #False
print('3',True or False) #True
print('4',False or False) #False
print('5',not True) #False
print('6',not False) #True
要如何宣告 function
前面加 def 後面要加冒號 要縮排
def functionName() :
print('magic')
functionName()
迴圈
參考資料:(https://sites.google.com/site/zsgititit/home/python-cheng-shi-she-ji/python-de-hui-quan-jie-gou)
For
python 是利用縮排,去決定程式的區塊 所以只有指令區的會重複,指令區2的就只有一次
for i in range(次數):
指令區
指令區2
range(5) => 1 ~ 5
range(2,6) => 2~5
range(2,10,2) => 2, 4, 6, 8
while
i = 3
while i < 13:
指令區
i = i + 3
Last updated
Was this helpful?