文章加密

;

2019年11月13日 星期三

python 第一堂課

https://training.pada-x.com/online.htm
接下來要看流程控制

工具
Jupyter Notebook,
anaconda


關於python無法顯示中文的問題:SyntaxError: Non-ASCII character '\xe4'
http://white5168.blogspot.com/2017/01/pythonsyntaxerror-non-ascii-character.html#.XdUKnjIzai4


Almost.
Semicolons have very little use in Python, and the use of them is not considered “Pythonic.” Nevertheless, they typically do not do anything, as one who has moved from a language such as C or Java may observe.
print(“Hello, World!”);
will still print.
The semicolon does have use in Python, however. It is used to separate commands on a single line.
For example,
  1. x = 5; print(x)
This, however, as has already been mentioned, is not considered “Pythonic” syntax.
幾乎python不用分號;


#start.py
print("Hello Python")

in cmd ..

key in "python start.py"
it show "Hello Python"

key in "cls"(window)/"clear"(OS X)
it clear cmd window

# 有順序,可變動的資料集合  List [ ]
[2,3,4]
["hello","world"]
# 有順序,不可變動的資料集合 Tuple ( ), 操作方式和List一樣
(2,3,4)
("hello","world")
# 無順序的資料集合 Set { }
{2,3,4}
{"hello","world"}
# 鍵值對(Key-Value Pair)的集合 Dictionary { "apple" : "蘋果" , " data " : " 資料 " }

# X的N次方  X**Y (js console.log 的output是8, 但會有錯誤紅線在code下面 )
# 整數除法 7//6  結果為1 (js console.log 的output是error )

# 字串的串接,在python中除了用+以外,還可以用空格串接
# 字串的換行可以用前後三個雙引號或單引號包起,就可以在裡面任意的換行


list[], tuple()基本操作

x="hello"*3+"world"
print(x)  # hellohellohelloworld  (only python)

# every char in string can be called in index, like ..
x="hello"
print(x[0]) # h
print(x[1:4]) # ell, [start:end] 包含開頭,不包含結尾。
print(x[1:]) # ello 如果不給結尾,則從開頭取到最後
print(x[:4]) # hell 如果不給開頭,則從第一位取到結尾指定index

grades=[12, 44, 23, 56, 70];
狀況一
grades[1:4]=[]
print(grades) #[12, 70];

狀況二
grades=grades+[3,4]
print(grades) # [2, 44, 23, 56, 70, 3, 4];

狀況三
print(len(grades)) # 5

data=[[3,4],[5,6]];
data[0][0:2]=[5,5,5];
print(data[0]) #[5,5,5,4]
but 
data[0:2][0:2]=[5,5,5]
not working, if print(data), it'll be [[3, 4], [5, 6]]

set{}, dictionary{} 基本操作

s1={3,4,6}
s2={4,6,7,8}
利用 in, not in 判斷資料是不是存在集合,會回傳true or false
print(3 in s1) # True
print(3 not in s1) # False

s3=s1&s2 # &交集,取兩個集合中相同的資料
print(s3) # {4,6}

s3=s1 | s2 # | 聯集,取兩個集合中所有的資料,但不重複
print(s3) # {3,4,6,7,8}

s3=s1 - s2 # - 差集,從s1中減去和s2重複的資料
print(s3) # {3}

s3=s1^s2 # ^反交集,取兩個集合中不重疊的資料
print(s3) # {3,7,8}

s=set("Hello")  # set(字串),會把字串拆解進集合
print(s) # set(['H', 'e', 'l', 'o']),不過set無順序性,所以也可以印出的是 set(['e', 'H', 'l', 'o'])
print("H" in s) # True

dic={ "apple" : "蘋果" , " data " : " 資料 " }
print("apple" in dic)   # True ,判斷key是不是在dictionary中(此處注意不是判斷value
del dic["apple"]  #刪除字典中的鍵值對key-value pair
print(dic) # {" data " : " 資料 " }

dic={ x:x*2 for x in [3,5,7]} # 從列表的資料去產生字典
print(dic)  # {3: 6, 5: 10, 7: 14}

沒有留言:

張貼留言