Statement :
- 每行敍述不用分號 ; 做結尾, 除非語法敍述寫在同一行
print('A')
print ('B')
print('A') ;print ('B') #當寫在同一行時,加分號 ; - 用 tab 內縮取代括號, 同一層的內縮表示在同一對括號內
for n in names:
print("Hello, %s."%n) - for n in names:print("Hello, %s."%n)
變數:
- 變數是有資料型態的但使用前不用事先宣告其資料型態,如int, str, float, set..
- type () 傳回資料型態
- 變數是一種label ,可以用來代表所指object的一個名字,
- del 刪除變數/label
>>> type(10)
<class 'int'>
>>> type('hello')
<class 'str'>
>>> type(24.3)
<class 'float'>
>>>
>>> num=10
>>> type(num)
<class 'int'>
>>> print(num)
10
>>> del num
>>>i,j=5,10 #指定i=5,=10
>>>print(i) -->卬出5
>>>print(i,j) -->卬出5 10
>>>print(i,j,sep=',') -->卬出5,10
>>>print("i") --> 卬出i
>>> print('a','b','c','d',sep="")
abcd
>>> print('a','b','c','d',sep="|")
於命令列
> python -c "print('hello')"
變數
#在Python 中所有的data都是object, 凡object 都會有 id, type, value
#可變的資料型態,如 list, ,變數指定時會產生新的id
a=[1,2,3]
b=[1,2,3]
a is b ?
#不可變的資料型態, 如6,變數指定時"可能"會對應相同的id
判斷id 最準
# is , ==
a=1000
b=10**3
a is b ==> False
a == b ==> True
#讀取變數(lable)名稱的id, a is b ==> True, 表示其ID相同
id(a)
id(b)
type 資料型態種類
- 整數 int 不可變(immutable)
a=6 - 浮點數 float 不可變(immutable)
a=6.2 - 複數 complex 不可變(immutable)
- 字串 str ,不可變( immutable)
a='robot' or a="robot" - 字節 bytes , 不可變(immutable)
- 串列list 可變( immutable) a=[1,2,'hello']
a+=['world']
a[1]=3
b=a --> b=[1,3,'hello','world'] - 序對 tuple ,不可變(immutable)
- 集合 set 可變( immutable)
- 字典 dict 可變( immutable)
註: 那int 不變又為何可以做
x=3
x=4
x只是一個tag , 被重新指向到 4的空間, 而3若沒有人指到 (refer to ) 則會自動被GC (Garbage collection)
字串處理
ord('a') -> 97 ( return ASCII code)a='hello world'
a[6] --> 'w'
a[7] --> 'o'
a[-1]-->'d'
a[-5]--> 'w'
b='Hi'
a+b --> 'Hi hello world'
len(a)-->11
a[0:5]-->'hello'
a[6:10]-->'worl'
a[6: ] --> 'world'
a[0:5]*4 --> 'hellohellohellohello'
4*a[0:5] --> 'hellohellohellohello'
'o' in a --> True
'o' not in a --> False
max(a) --> 'w'
min(a) --> ' '
a.index('o') --> 4
a.count('o') --> 2
---
Escape Character
這些字元和C語言一樣
\n, \r , \t, \',\'', \\
>>> a="c:\windows\test\newfolder"
>>> print(a)
c:\windows est
ewfolder
>>> a="c:\windows\\test\\newfolder"
>>> print(a)
c:\windows\test\newfolder
===================================
>>>["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][1]
'Monday'
===========
功能和List完全一樣,差別在裡面的內容是不可變(immutable), 唯讀
>>> a=('hello','robotic','lab')
>>> type(a)
<class 'tuple'>
>>> a[0]
'hello'
>>> a[1]
'robotic'
>>> a[2]
'lab'
>>> a[2][1]
'a'
>>> a[1][0]
'r'
>>>
tuple內容是唯讀的
>>> a[1]='ittraining'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
==============
集合Set Example
>>> s1={1,1,2,3,5}
>>> s1
{1, 2, 3, 5}
>>> s2={2,4}
>>> s2
{2, 4}
>>> s1
{1, 2, 3, 5}
>>> s1 == s2
False
>>> s1 | s2
{1, 2, 3, 4, 5}
>>> s1 & s2
{2}
>>> s1 ^ s2
{1, 3, 4, 5}
>>> s2
{2, 4}
>>> s2.add(3)
>>> s2
{2, 3, 4}
>>> s1
{1, 2, 3, 5}
>>> s1 & s2
{2, 3}
>>> s1.remove(5)
>>> s1
{1, 2, 3}
字典 Dict Example
>>>day={1:"Sunday",2:"Monday",3:"Tuesday",4:"Wednesday",5:"Thursday",6:"Friday",7:"Saturday"}
>>>day[2]
'Monday'
>>>day[6]
'Friday'
>>> names=['joseph','onions','king']
>>> a={}
>>> a
{}
#fromkeys : creates a new dictionary with List
>>> a.fromkeys(names,0)
{'joseph': 0, 'king': 0, 'onions': 0}
>>> a
{}
>>> b=a.fromkeys(names,0)
>>> b
{'joseph': 0, 'king': 0, 'onions': 0}
>>> b['joseph']=100
>>> b
{'joseph': 100, 'king': 0, 'onions': 0}
>>> b['joseph123']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'joseph123'
>>> b.get('joseph123')
>>> b.get('joseph123',-1) '若Key找不到, 則設定傳回值為 -1
-1
>>> b.get('joseph',-1)
100
>>> b.items()
dict_items([('joseph', 100), ('king', 0), ('onions', 0)])
>>> b.keys()
dict_keys(['joseph', 'king', 'onions'])
>>> b.values()
dict_values([100, 0, 0])
>>>
>>> b
{'joseph': 100, 'king': 0, 'onions': 0}
>>> b.pop('joseph')
100
>>> b
{'king': 0, 'onions': 0}
>>> b.setdefault('joseph',20)
10
>>> b
{'joseph': 10, 'king': 0, 'onions': 0}
>>> a
{}
>>> a={'bella':30,'peter':40}
>>> a
{'bella': 30, 'peter': 40}
>>> b
{'joseph': 10, 'king': 0, 'onions': 0}
>>> b.update(a)
>>> b
{'onions': 0, 'peter': 40, 'joseph': 10, 'bella': 30, 'king': 0}
num= '1000' #str字串型態
若需要除以2運算後得到數字500的話怎麼辦?
int(num)/2
Exercise 2:
nums= [1,1,1,2,2,3,4,5] #list串列型態
若需要將重複的元素去除的話怎麼辦?
<作法1>
i=0
for (..,i++)
if nums.count(nums[i]) > 1
nums.remove(nums[i])
<作法2>
用set(x), list(x) 用來將資料型態轉換
>>> nums
[1, 1, 1, 2, 2, 3, 4, 5]
>>> num1=list(set(nums))
>>> num1
[1, 2, 3, 4, 5]
Exercise 3:
nums= (10,5,7,1,6,2) #tuple序對型態
tuple並不提供排序的方法,若要排序(由小到大)該怎辦?
<作法1>
>>> nums=(10,5,7,1,6,2)
>>> nums
(10, 5, 7, 1, 6, 2)
>>> type(nums)
<class 'tuple'>
>>> list=[]
>>> list.extend(nums)
>>> list
[10, 5, 7, 1, 6, 2]
>>> list.sort()
>>> list
[1, 2, 5, 6, 7, 10]
>>> nums=tuple(list)
>>> nums
(1, 2, 5, 6, 7, 10)
>>>
<作法2>
>>> nums
(10, 5, 7, 1, 6, 2)
>>> list(nums)
[10, 5, 7, 1, 6, 2]
>>> nums
(10, 5, 7, 1, 6, 2)
>>> t=list(nums)
>>> t
[10, 5, 7, 1, 6, 2]
>>> t.sort()
>>> nums=tuple(t)
>>> nums
(1, 2, 5, 6, 7, 10)
>>>
參考
串列list [ ]
a=[1,2,'hello']
b=[3,4,'world',a]
b --> [3, 4, 'world', [1, 2, 'hello']]
b --> [3, 4, 'world', [1, 2, 'hello']]
b[2] --> 'world'
b[3][1] -->2
c=a+b --> [1, 2, 'hello', 3, 4, 'world', [1, 2, 'hello']]
# List 操作 append
>>> num=[]
>>> type(num)
<class 'list'>
>>> num.append(1)
>>> num.append(2)
>>> num.append(3)
>>> num
[1, 2, 3]
>>>
============
# List 操作 copy
>>> b=[1,2,3,4,5]
>>> b
[1, 2, 3, 4, 5]
>>> b.remove(3)
>>> b
[1, 2, 4, 5]
>>> a=b.copy()
>>> a
[1, 2, 4, 5]
>>> id(b)
24470992
>>> id(a)
24470352
>>> a==b
True
>>>
>>>["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][1]
'Monday'
===========
序對 tuple ( )
功能和List完全一樣,差別在裡面的內容是不可變(immutable), 唯讀
>>> a=('hello','robotic','lab')
>>> type(a)
<class 'tuple'>
>>> a[0]
'hello'
>>> a[1]
'robotic'
>>> a[2]
'lab'
>>> a[2][1]
'a'
>>> a[1][0]
'r'
>>>
tuple內容是唯讀的
>>> a[1]='ittraining'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
==============
集合Set Example
- 元素內容無順序的, 所以set元素無法用index去取得的
- Set元素內容是不會重覆的
- 集合內元素充許任意資料型態
>>> s1={1,1,2,3,5}
>>> s1
{1, 2, 3, 5}
>>> s2={2,4}
>>> s2
{2, 4}
>>> s1
{1, 2, 3, 5}
>>> s1 == s2
False
>>> s1 | s2
{1, 2, 3, 4, 5}
>>> s1 & s2
{2}
>>> s1 ^ s2
{1, 3, 4, 5}
>>> s2
{2, 4}
>>> s2.add(3)
>>> s2
{2, 3, 4}
>>> s1
{1, 2, 3, 5}
>>> s1 & s2
{2, 3}
>>> s1.remove(5)
>>> s1
{1, 2, 3}
==========================================
字典 Dict Example
- key-value pair
- {Key0:value0, Key1:value1,.....}
- 元素內容無順序的, 所以dict元素無法用index去取得的, 而是以key去找
>>>day={1:"Sunday",2:"Monday",3:"Tuesday",4:"Wednesday",5:"Thursday",6:"Friday",7:"Saturday"}
>>>day[2]
'Monday'
>>>day[6]
'Friday'
>>> names=['joseph','onions','king']
>>> a={}
>>> a
{}
#fromkeys : creates a new dictionary with List
>>> a.fromkeys(names,0)
{'joseph': 0, 'king': 0, 'onions': 0}
>>> a
{}
>>> b=a.fromkeys(names,0)
>>> b
{'joseph': 0, 'king': 0, 'onions': 0}
>>> b['joseph']=100
>>> b
{'joseph': 100, 'king': 0, 'onions': 0}
>>> b['joseph123']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'joseph123'
>>> b.get('joseph123')
>>> b.get('joseph123',-1) '若Key找不到, 則設定傳回值為 -1
-1
>>> b.get('joseph',-1)
100
>>> b.items()
dict_items([('joseph', 100), ('king', 0), ('onions', 0)])
>>> b.keys()
dict_keys(['joseph', 'king', 'onions'])
>>> b.values()
dict_values([100, 0, 0])
>>>
>>> b
{'joseph': 100, 'king': 0, 'onions': 0}
>>> b.pop('joseph')
100
>>> b
{'king': 0, 'onions': 0}
>>> b.setdefault('joseph',20)
10
>>> b
{'joseph': 10, 'king': 0, 'onions': 0}
>>> a
{}
>>> a={'bella':30,'peter':40}
>>> a
{'bella': 30, 'peter': 40}
>>> b
{'joseph': 10, 'king': 0, 'onions': 0}
>>> b.update(a)
>>> b
{'onions': 0, 'peter': 40, 'joseph': 10, 'bella': 30, 'king': 0}
==========================
Exercise 1:
若需要除以2運算後得到數字500的話怎麼辦?
int(num)/2
Exercise 2:
nums= [1,1,1,2,2,3,4,5] #list串列型態
若需要將重複的元素去除的話怎麼辦?
<作法1>
i=0
for (..,i++)
if nums.count(nums[i]) > 1
nums.remove(nums[i])
<作法2>
用set(x), list(x) 用來將資料型態轉換
>>> nums
[1, 1, 1, 2, 2, 3, 4, 5]
>>> num1=list(set(nums))
>>> num1
[1, 2, 3, 4, 5]
Exercise 3:
nums= (10,5,7,1,6,2) #tuple序對型態
tuple並不提供排序的方法,若要排序(由小到大)該怎辦?
<作法1>
>>> nums=(10,5,7,1,6,2)
>>> nums
(10, 5, 7, 1, 6, 2)
>>> type(nums)
<class 'tuple'>
>>> list=[]
>>> list.extend(nums)
>>> list
[10, 5, 7, 1, 6, 2]
>>> list.sort()
>>> list
[1, 2, 5, 6, 7, 10]
>>> nums=tuple(list)
>>> nums
(1, 2, 5, 6, 7, 10)
>>>
<作法2>
>>> nums
(10, 5, 7, 1, 6, 2)
>>> list(nums)
[10, 5, 7, 1, 6, 2]
>>> nums
(10, 5, 7, 1, 6, 2)
>>> t=list(nums)
>>> t
[10, 5, 7, 1, 6, 2]
>>> t.sort()
>>> nums=tuple(t)
>>> nums
(1, 2, 5, 6, 7, 10)
>>>
參考
- http://www.tutorialspoint.com/python
沒有留言 :
張貼留言