PYTHON

python介紹

特色:

直譯式語言
對空白、換行敏感

眾多(有夠多)套件

有點慢(?

語法簡單

小知識:編譯式與直譯式

編譯:
把我們看的懂的(程式語言)變成
電腦看得懂的(電腦指令)

編譯式:編譯完再執行

直譯式:邊執行邊編譯

python IDE

學校有的:python IDLE

回家時推薦的:

visual studio

visual studio code

jupyter notebook(大推

教學:

講師私推jupyter notebook時間

jupyter notebook好處:


1.多個程式語言整合

2.cell分區塊,可以區分功能、分區執行

3.可以用cmd快速下載、移除python套件

Hello,world!

in python

print("Hello,World!")

comments(註解)

#iamweak

'''
iamweak
'''

引入函式庫

函式庫:

想要用套件就需要引入!

ex.math 中的sqrt

import math

a=2
print(math.sqrt(a))

#result: 1.4142135623730951

變數

與C++比較

python C++
不用宣告 要宣告
不能取關鍵字 不能取關鍵字
有分全域、區域 有分全域、區域

小知識:全域與區域

全域:整份code都會存在他

區域:只有一個區域會有他

這個概念可以先留著,之後會再作解釋

變數類型列表

字串:str

 

數字:int(整數)、float(小數)、complex(複數)


布林值:bool


序列:list(陣列)、tuple(不能修改的序列)、range(一系列的數字)

 

集合:set(集合)、frozenset(集合不能刪)

 

注意:

以下變數都有很多細節的用法
簡報上就不列出來了
詳情請見w3school

簡單部分

string 

a = "yeedragorz"
print(a)

#result: yeedragorz

int、float:同C++(範圍不同)

complex

x = 1+1j

print(type(x))
#result: (1+1j)

bool

只有零跟一的型態

a=True 
a=False

困難部分

接下來的變數我們會用

資料結構

來叫它們

tuple list

tuple和list都相當於C++的array
差別是tuple不可修改
list可以

tuple

thistuple = ("apple", "banana", "cherry")
print(thistuple)

#result: ('apple', 'banana', 'cherry')

list

thislist = ["apple", "banana", "cherry"]
print(thislist)

#result ['apple', 'banana', 'cherry']

dictionary

兩個一組
像是數學中的函數
有對應關係

dictionary

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict["brand"])

#result: Ford

range

整數序列!

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
'''

set frozenset

就是數學中的集合

frozenset不可修改,set可以

簡單來說,集合就是一個群體

ex.一個班級也是集合

班級中的學生就是集合中的元素

表示法(數學):

集合\(A\)和他的元素可以用以下符號表示
\(A=\{1,2,3\}\)

A = {1,2,3}

code:

接下來會用文氏圖來解釋

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}

operator(運算子)

加減乘除

a=a+b
a=a-b
a=a*b
a=a/b

注意:這裡的除跟C++的不一樣
是人類的除

ex.5/3=1.666667

跟C++一樣的除

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

以下算式兩個一組等價

==	
!=	
>		
<		
>=	
<=

跟C++一樣

is

not is

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

in

x = ["apple", "banana"]

print("banana" in x)

not in

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

&&

||

!

C++

python

位元運算(bitwise)

warning:有難度!

轉成二進位!

\(7_{(2)}\)=\(111_{(2)}\)

1 1 1

\(2^0\)

\(2^1\)

\(2^2\)

example:

\(11_{(10)}\)=\(1011_{(2)}\)

\(5_{(10)}\)=\(101_{(2)}\)

二進位下做

類似加減乘除的事

用真值表來執行
一個一個位元來做!

(位元就像個位十位百位)

bitwise and

a=a&b
AND 1 0
1 1 0
0 0 0

真值表

ex.5&3=1

第一個 1 0 1
第二個 0 1 1
結果 0 0 1

二進位:

都是1的時候1
其他0

bitwise or

a=a|b
AND 1 0
1 1 1
0 1 0

真值表

ex.5\(|\)3=7

第一個 1 0 1
第二個 0 1 1
結果 1 1 1

二進位:

其中至少一個1的時候1
其他0

bitwise xor

a=a^b
AND 1 0
1 0 1
0 1 0

真值表

ex.5^3=6

第一個 1 0 1
第二個 0 1 1
結果 1 1 0

二進位:

一樣的時候0
其他1

bitwise not(取反)

a=~a
NOT
1 0
0 1

真值表

ex.~5

第一個 1 0 1
結果 0 1 0

二進位:

bitwise 左移

a=(a<<1)

ex.5<<1

第一個 0 1 0 1
結果 1 0 1 0

二進位:

左移

bitwise 右移

a=(a>>1)

ex.5>>1

第二個 1 0 1
結果 0 1 0

二進位:

if else

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

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")

for

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!")

function

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

不用名字的function

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))

常用function

x = 1
print(type(x))

#result: <class 'int'>

輸出變數型態

變數型態轉變function

x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

轉int

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

轉float

轉string

x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'

math

不需引入的函數

max(取最大值)

min(取最小值)

x = min(5, 10, 25)
y = max(5, 10, 25)

print(x)
print(y)

#result: 5
#25

pow(a,b)=\(a^b\)

x = pow(4, 3)

print(x)

#result: 64

abs(a)=\(|a|\)

x = -1
print(x)
x=abs(x)
print(x)

#result: -1
#1

需要引入的函數

import math

sqrt(a)=\( \sqrt{a}\)

import math

a=2
print(math.sqrt(a))

#result: 1.4142135623730951

ceil(a)=\(\lceil a\rceil\)

floor(a)=\(\lfloor a\rfloor\)

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\) 下高斯

\(\pi\)

import math

x = math.pi

print(x)

#result: 3.141592653589793

reference:

Made with Slides.com