賴昱錡
希望你以後不會被code爆打 QQ
為你以後爆肝的打扣日常做準備
電腦不是只會二進位嗎?
可能是複習
print('Hello, world!')
temp = input()
print('123','456', sep = '#')
print('yeah', end = '.')
temp = int(input()) # 這行在幹嘛啊?
問題來了,我今天想輸出 What's your name? 於是:
print('What's your name?')
因為文字必定由兩個雙引號或單引號包住,
所以我們需要用反斜線,使某些符號有特殊意義
print('What\'s your name?')
print("Hello, World!")
# 電腦會跳過這行
print("Hi mom") # 這些中文也不會被執行
'''
這幾行也
不會被
執行
'''
print("1 + 1 =", 1 + 1) # 1 + 1 = 2
Text Type: str
Numeric Types: int, float
Sequence Types: list, tuple, range
Mapping Type: dict
Boolean Type: bool
builtin_function_or_method
宣告並賦值:
有個變數叫 msg,賦予它的值是 "喔是喔"
msg = "喔是喔"
print(type("Discord")) # <class 'str'>
print(type(7122)) # <class 'int'>
print(type(3.14159)) # <class 'float'>
print(type(input())) # <class 'NoneType'>
print(type(True)) # <class 'bool'>
由上至下分別為字串、整數、浮點數
命名規則:
s1 = "Hello, "
s2 = "World"
print(s1+s2) # 串接
print("The price of this iphone is " + 70 + "dollars")
# 太弱了
x = 70
print(f'The price of this iphone is {x} dollars')
x = int(input())
if x > 50:
print("那你很大")
elif x > 30:
print("有點小")
else:
print("太小了吧")
縮排 (indent) 非常重要!
condition = x > 10 and y < 100
# 剛剛忘了說,變數的加減乘除符號基本上相同
# 除了 and 還有 or
# 一般的函式,有必要嗎?
def f(x):
return x+10
x = lambda a : a + 10
print(x(5))
# 酷
同理
x = lambda a, b : a * b
print(x(5, 6))
# 還是很酷
list1 = [] # 一個空的 list
list2 = list() # 另一個空的 list
list = [] # 不要這樣!為什麼?
特性: 可迭代 (iterable)、可以重複、有序
像 set 的資料結構就不可重複、無序
基本上,list 裡面要塞什麼都行
list1 = ["a", "b", "c", "d"]
print(list1[0]) # a
print(list1[3]) # d
print(list1[-1]) # d
list1 = ["a", "b", "c", "d", "e", "f", "g"]
print(list1[1:3]) # ["b", "c"]
print(list1[0:-1]) # ["a", "b", "c", "d", "e", "f"]
print(list1[0:5:2]) # ["a", "c", "e"]
我有一個 100 項的數列。
(1) 請幫我印出最後 30 項
(2) 請幫我倒著印出這個數列的偶數項。
怎麼做? 幫我打在 Google Meet 的聊天室。
list1 = ["a", "b", "c", "d", "e"]
list1[1] = "p" # ["a", "p", "c", "d", "e"]
list1[2:4] = ["p", "l"] # ["a", "p", "p", "l", "e"]
list1 = [1, 2, 3]
print(list1 + [4]) # [1, 2, 3, 4]
print(list1 * 3) # [1, 2, 3, 1, 2, 3, 1, 2, 3]
print(list1) # list1 沒有被改,所以是 [1, 2, 3]
list2 = [1, 2, 3]
list2 += [4]
print(list2) # [1, 2, 3, 4]
list2 *= 3
print(list2) # [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
list1 = []
print(len(list1)) #0
list1 = [1, 2, 3]
print(len(list1)) #3
list1 = [1, 2, 3]
print(1 in list1) # True
print(10 in list1) # False