編譯: 把我們看的懂的(程式語言)變成 電腦看得懂的(電腦指令)
visual studio
visual studio code
jupyter notebook(大推
就直接print()
print(你要印的東西)
print(A, B, sep = ',', end = 'XD')
輸出"Hello World!"
print("Hello World!")
input()
s = input()
讀一個字串,然後把它印出來
s = input()
print(s)
#iamweak
'''
iamweak
'''
import math
import math as mh
import math
a=2
print(math.sqrt(a))
#result: 1.4142135623730951
from math import sqrt
from math import sqrt as sq
from math import *
from math import sqrt as rt
a=2
print(rt(a))
#result: 1.4142135623730951
python | C++ |
---|---|
不用宣告 | 要宣告 |
不能取關鍵字 | 不能取關鍵字 |
有分全域、區域 | 有分全域、區域 |
這個概念可以先留著,之後會再作解釋
print(type('dolphin'))
# result:<class 'str'>
pi = 3.14
print(type(pi))
# result:<class 'float'>
a = "linkiorz"
print(a)
#result: linkiorz
x = 123
print(type(x))
#result: <class 'int'>
y = 1.23
print(type(y))
#result: <class 'float'>
z = 1+1j
print(type(x))
#result: <class 'complex'>
a=True
a=False
接下來的變數我們會用
資料結構
來叫它們
thistuple = ("dolphin", "knight", True, 4, "color", "color")
len(thistuple) # 6
thistuple[0] # "dolphin"
thislist = ["dolphin", "knight", True, 4, "color", "color"]
len(thislist) # 6
thislist[0] # "dolphin"
thislist.append(7122)
thislist.insert("bad", 0)
thislist.pop(0)
thislist.remove("color")
thislist.reverse()
print(range(-5,5,2))
#result: range(-5,5,2)
for i in range(5):
print(i)
'''
result:0
1
2
3
4
'''
for i in range(2,5):
print(i)
'''
result:2
3
4
'''
for i in range(-5,5,2):
print(i)
'''
result:-5
-3
-1
1
3
'''
thisdict = {
"Jerry": "dolphin knight",
"color": True,
"num": 7122
}
print(thisdict["color"])
#result: True
就是數學中的集合
frozenset不可修改,set可以
ex.一個班級也是集合
班級中的學生就是集合中的元素
A = {1,2,3}
z
x
y
\(A\)=x+z
\(B\)=y+z
z
x
y
\(A \cap B\)=z
z
x
y
A={1,2,3}
B={2,3,4}
A=A.intersection(B)
print(A)
#result: {2, 3}
\(A \cup B\)=x+y+z
z
x
y
A={1,2,3}
B={2,3,4}
A=A.union(B)
print(A)
#result: {1, 2, 3, 4}
\(A\)-\(B\)
=\(A\)-\(A \cap B\)=x
z
x
y
A={1,2,3}
B={2,3,4}
A=A.diference(B)
print(A)
#result: {1}
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
用這個方法我們就可以把input()進來的東西轉成數運算了
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
a = (7, 1, 2, 2)
print(list(a))
a = [7, 1, 2, 2]
print(tuple(a))
a = [("one", 1), ("two", 2), ("three", 3)]
print(dict(a))
把等號右邊的值指定給左邊的變數
a = 7122
b = 3.14
c = "dolphin"
a = 7
b = 3
c = a + b # 10
d = a - b # 4
e = a * b # 21
f = a / b # 2.333333333333333
對float也可以這樣做
s1 = "dolphin"
s2 = "knight"
s3 = s1 + s2 # "dolphinknight"
s4 = s1 * 3 # "dolphindolphindolphin"
對string 做加法、乘法
a//b=\( \lfloor\frac{a}{b}\rfloor\)
a = 5
b = 2
c = a // b # 2
a, b可以是int, float
a = 5
b = 2
c = a ** b # 25
a ** b = \(a^b\)
a, b可以是int, float
甚至負數(!
a = 10
b = 3
c = a % b # 1
a % b : a 除以 b 的餘數
python的 % 出來在0~b - 1之間
a = a + b
a += b
a = a - b
a -= b
a = a * b
a *= b
以下算式兩個一組等價
a = a / b
a /= b
a = a % b
a %= b
==
!=
>
<
>=
<=
可以對int, float, string比大小
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
# returns True
print(x is y)
# returns False
print(x == y)
# returns True
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is not z)
# returns False
print(x is not y)
# returns True
print(x != y)
# returns False
x = ["apple", "banana"]
print("banana" in x)
# True
x = ["apple", "banana"]
print("pineapple" not in x)
# True
a = 3
b = 2
c = 1
print(a > b and b > c) # True
print(a > c or c > b) # True
print(not a > b) # False
warning:有難度!
\(7_{(2)}\)=\(111_{(2)}\)
1 | 1 | 1 |
---|
\(2^0\)
\(2^1\)
\(2^2\)
\(11_{(10)}\)=\(1011_{(2)}\)
\(5_{(10)}\)=\(101_{(2)}\)
(位元就像個位十位百位)
a=a&b
AND | 1 | 0 |
---|---|---|
1 | 1 | 0 |
0 | 0 | 0 |
真值表
第一個 | 1 | 0 | 1 |
第二個 | 0 | 1 | 1 |
結果 | 0 | 0 | 1 |
二進位:
都是1的時候1
其他0
a=a|b
AND | 1 | 0 |
---|---|---|
1 | 1 | 1 |
0 | 1 | 0 |
真值表
第一個 | 1 | 0 | 1 |
第二個 | 0 | 1 | 1 |
結果 | 1 | 1 | 1 |
二進位:
其中至少一個1的時候1
其他0
a=a^b
AND | 1 | 0 |
---|---|---|
1 | 0 | 1 |
0 | 1 | 0 |
真值表
第一個 | 1 | 0 | 1 |
第二個 | 0 | 1 | 1 |
結果 | 1 | 1 | 0 |
二進位:
一樣的時候0
其他1
a=~a
NOT | |
---|---|
1 | 0 |
0 | 1 |
真值表
第一個 | 1 | 0 | 1 |
結果 | 0 | 1 | 0 |
二進位:
a=(a<<1)
第一個 | 0 | 1 | 0 | 1 |
結果 | 1 | 0 | 1 | 0 |
二進位:
a=(a>>1)
第二個 | 1 | 0 | 1 |
結果 | 0 | 1 | 0 |
二進位:
如果 條件1成立:
做某件事
否則 如果 條件2成立 :
做另一件事
...
否則
以上條件都不成立做這件事
if name == "Jerry":
print("dolphin knight")
elif name == "Eason":
print("yennnn")
else:
print("Who are you?")
if name == "Jerry":
print("dolphin knight")
elif name == "Eason":
print("yennnn")
else:
print("Who are you?")
if a>b:
print("ORZ")
if a>=b:
print("ORZ")
if a<b:
print("ORZ")
if a<=b:
print("ORZ")
if a!=b:
print("ORZ")
if a==b:
print("ORZ")
if a!=b and a>b:
print("ORZ")
if a!=b or a>b:
print("ORZ")
if a!=b:
print("O")
else :
print("R")
if a!=b:
print("O")
elif a>b:
print("R")
else :
print("Z")
while 條件成立 :
做一些事情
hard = 0
while hard < 100:
print("I am listening")
hard += 1
while 後面加 ' : '
用縮排分隔程式區塊
for 變數 in 序列 :
做一些事情
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
for 後面加 ' : '
用縮排分隔程式區塊
sum = 0
for x in range(5, 10, 2):
sum += x
print(sum) # 21
for迴圈常常也會搭配 range 服用
break
continue
else
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
# apple
# banana
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
# apple
# cherry
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
def my_function():
print("Hello from a function")
my_function()
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
def func(a,b):
if a<10:
print(b)
func(a+1,b)
a+=1
func(1,"7122")
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Tobias", lname = "Refsnes")
def my_function(x):
return 5 * x
print(my_function(3))
def myfunction():
pass
物件
Object
Data
Function
Function
import math
class point:
def __init__(self, x, y):
self._x = x
self._y = y
def getx(self):
return self._x
def gety(self):
return self._y
def print(self):
print("({}, {})".format(self._x, self._y))
return
def dist(self, point2):
# distance
return math.sqrt((self._x - point2.getx()) ** 2 + (self._y - point2.gety()) ** 2)
def move(self, dx, dy):
return point(self._x + dx, self._y + dy)
A = point(3, 4)
print(A.getx(), A.gety(), sep=',')
B = A.move(5, 12)
B.print()
d = A.dist(B)
print(d)
(承上頁的扣的)
lambda arguments : expression
x = lambda a : a
print(x(5))
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
x = min(5, 10, 25)
y = max(5, 10, 25)
print(x)
print(y)
#result: 5
#25
x = pow(4, 3)
print(x)
#result: 64
x = -1
print(x)
x=abs(x)
print(x)
#result: -1
#1
import math
import math
a=2
print(math.sqrt(a))
#result: 1.4142135623730951
import math
x = math.ceil(1.4)
y = math.floor(1.4)
print(x) # returns 2
print(y) # returns 1
備註:
\(\lceil a\rceil\)上高斯
\(\lfloor a\rfloor\) 下高斯
import math
x = math.pi
print(x)
#result: 3.141592653589793