編譯: 把我們看的懂的(程式語言)變成 電腦看得懂的(電腦指令)
visual studio
visual studio code
jupyter notebook(大推
print("Hello,World!")
#iamweak
'''
iamweak
'''
import math
a=2
print(math.sqrt(a))
#result: 1.4142135623730951
python | C++ |
---|---|
不用宣告 | 要宣告 |
不能取關鍵字 | 不能取關鍵字 |
有分全域、區域 | 有分全域、區域 |
這個概念可以先留著,之後會再作解釋
a = "yeedragorz"
print(a)
#result: yeedragorz
x = 1+1j
print(type(x))
#result: (1+1j)
a=True
a=False
接下來的變數我們會用
資料結構
來叫它們
tuple和list都相當於C++的array
差別是tuple不可修改
list可以
thistuple = ("apple", "banana", "cherry")
print(thistuple)
#result: ('apple', 'banana', 'cherry')
thislist = ["apple", "banana", "cherry"]
print(thislist)
#result ['apple', 'banana', 'cherry']
兩個一組
像是數學中的函數
有對應關係
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
#result: Ford
整數序列!
print(range(-5,5,2))
#result: range(-5,5,2)
for i in range(5):
print(i)
'''
result:1
2
3
4
5
'''
for i in range(2,5):
print(i)
'''
result:2
3
4
5
'''
for i in range(-5,5,2):
print(i)
'''
result:-5
-3
-1
1
3
'''
就是數學中的集合
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}
a=a+b
a=a-b
a=a*b
a=a/b
注意:這裡的除跟C++的不一樣
是人類的除
ex.5/3=1.666667
a//b=\( \lfloor\frac{a}{b}\rfloor\)
a=a//b
ex.5//2=2
備註:
\(\lfloor a\rfloor\) 下高斯
a=a**b
ex.5**2=25
a=a%b
ex.10%3=1
a=a+b
a+=b
a=a/b
a/=b
a=a%b
a%=b
以下算式兩個一組等價
==
!=
>
<
>=
<=
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)
x = ["apple", "banana"]
print("pineapple" not in x)
# returns True because a sequence with the value "pineapple" is not in the list
and
or
not
&&
||
!
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 |
二進位:
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 a<5:
print(a)
a+=1
while a<5:
if a<3:
break
a+=1
while a<5:
if a<3:
continue
a+=1
while a<5:
a+=1
else:
print("ORZ")
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
for x in [0, 1, 2]:
continue
for x in [0, 1, 2]:
pass
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
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 = 1
print(type(x))
#result: <class 'int'>
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
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'
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