顯示具有 python 標籤的文章。 顯示所有文章
顯示具有 python 標籤的文章。 顯示所有文章

2019年6月30日 星期日

【Python】dict的範例code

dict1={'name':'miller','age':30,'weight':60} #key-value的格式#
print(1,str(dict1['name'])) #指定Key以取得vale,此行加用str函數轉型輸出#
dict2=dict1.copy() #copy()複製dict物件 dict1給dict2#
dict2.pop('name') #pop()移除dict2的其中一個Key欄位'name'#
print(2,dict2)
dict2['name']='Nicole' #加回key欄位'name'並指定value為'Nicole'#
print(3,dict2)
dict2['name']='Jerry' #修改'name'的value為'Jerry'#
print(4,dict2)
-------輸出結果--------
1 miller 2 {'age': 30, 'weight': 60} 3 {'age': 30, 'weight': 60, 'name': 'Nicole'} 4 {'age': 30, 'weight': 60, 'name': 'Jerry'} PS C:\Users\miller\Python測試code>

【python】Set物件的使用範例

smp1=set(['張飛','關羽','劉備','趙雲'])
print(smp1)
smp1.add('馬超') #新增'馬超'至smp1
print("新增'馬超'至smp1:" , smp1)
smp2=smp1.copy() #複製相同set object#
print("複製相同set object:",smp2)
smp2.remove('馬超')
print("移除set中的陣列:",smp2)
#----------------------------------
#判斷某值是否存在set物件中#
print(smp1)
print('馬超' in smp1)
print(smp2)
print('馬超' in smp2)
#------------------------------------
print(smp1.intersection(smp2)) #smp1跟smp2的交集#
print(smp1.union(smp2)) #smp1與smp2的聯集
print(smp1.difference(smp2))#smp1與smp2的差集,只存在smp1的值#


-------------輸出結果---------------
移除set中的陣列: {'關羽', '趙雲', '張飛', '劉備'} {'關羽', '趙雲', '馬超', '張飛', '劉備'} True {'關羽', '趙雲', '張飛', '劉備'} False {'關羽', '張飛', '趙雲', '劉備'} {'關羽', '趙雲', '馬超', '張飛', '劉備'} {'馬超'} PS C:\Users\miller\Python測試code>

2018年6月25日 星期一

python3-筆記

1.程式利用縮行行數區分程式區塊,若空格不一致則會發生程式錯誤。
ex:
if True:
    print ("Answer")
    print ("True")
else:
    print ("Answer")
  print ("False")    # 縮行不一致,會導致程式錯誤

========================================================================
2.import 與 from...import
在 python 用 import 或者 from...import來導入相對的moudle。
將整個模組(some_module)導入,格式為: import some_module
從某個模組(some_module)導中導入某個函数(function),格式為: from some_module import some_function_name
從某個模組(some_module)中導入多個函数,格式為: from some_module import first_func_name, second_func_name, third_func_name
將某個模組(some_module)中的全部函數全部導入,格式為: from some_module import *

========================================================================
3.變數
  a.變數不需要宣告,但一定要有預設值,利用預設值的指定,來建立變數。
ex:
counter = 100          # 整數變數
miles   = 1000.0       # 浮點變數
name    = "runoob"     # 字串變數
========================================================================
4.List/Tuple/Set/dictionary 差異
a.List的值可以變更,Tuple的值不可變更。
b.List用[]包值,Tuple用()包值。
c.set用{}來包值,而且值不能重覆,且值不可變更,注意:若要建一個空set,語法要用set()。
d.dictionary是Key:vale的集合,用{}來包值且Key值必須為唯一值。

ex:List
>>> tinylist = [123, 'runoob']
>>> tinylist[1]="aa"
>>> print(tinylist)
[123, 'aa'] -->輸出結果

ex:Tuple變更值會出現error
>>> tinylist =(123, 'runoob')
>>> tinylist(1)="aa"
  File "<stdin>", line 1                        --->error
SyntaxError: can't assign to function call
>>>

ex:set中的值不可重覆,若重覆會自動過濾。
>>> TestSet = {123, 'runoob',123, 'runoob'}
>>> print(TestSet)
{123, 'runoob'} -->輸出結果

ex:dictionary是Key:value集合
testdic={'name':'miller_wang','age':28}
>>> print(testdic['name'])
miller_wang -->輸出結果

>>> print(testdic)
{'name': 'miller_wang', 'age': 28} -->輸出結果
========================================================================
資料型別說明
List:將數字或字串,依序容納的串列(List=[1,2,3])
Tuple:不可覆寫的元組(tuple),可作為Dict的key(例:tuple=(1,2,3))
Set:不允許重複的無序資料列(例:set={1,2,3})
Dict:Key-Value的字典(dict)(例:dict={'one':1,'two':2,'three':3})

2018年1月7日 星期日

python3筆記-基本練習

指令模式:idle
快速鍵: Alt+N 帶回上個指令
Alt+P 清空指指令行
單行註解為 # ,多行註解則用 """ 開頭與結尾
exit() 來離開 Python 主控台
-----------------------------------------------------------------------------------------------
字串表示式:
>>> print ("test")
test
>>> print "aaa";
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("aaa";)?
>>> print (2222)
2222
>>> print ("test")
test
>>> print ("test");
test
>>> print ("aaaaa"+"bbbbb")
aaaaabbbbb
>>> print ("test"*8)
testtesttesttesttesttesttesttest
>>> print ("test\n"*8)
test
test
test
test
test
test
test
test
-------------------------------------------------
str 函數可將物件轉為 字串
int 函數可將物件轉為 整數
--------------------------------------------------------------------
變數
>>> name="Jerry"
>>> print(name)
Jerry
--------------------------------------------------------------------
串列(List)
如果你只想要秀某一個數字,你可以使用 索引(index) 來指定。索引就是一個指出 List 某個物件位置的數字。

>>> IamList=[1,2,"a","b"]
>>> len(IamList)
4
>>> IamList.sort
<built-in method sort of list object at 0x05EF92D8>  #串列中不同型態值 ,不能排序

>>> print(IamList[3]) #取串列中特定位置值,開始值0
b
>>>
--------------------------------------------------------------------
字典(Dictionary) -->Key-Value物件
Dictionary 和 List 有點像,差別在於,你可以用鍵值(key)而非索引值(index)來取得在 Dictionary 中的值(value)。一個 key 可以是字串或數字。你可以使用兩個大括號來宣告一個 dictionary

>>> MyDIs={'name':'Miller','birthday':'1979/05/23','tall':'171cm','age':26}
>>> print(MyDIs['age'])

26
-----------------------------------------------------------------------
比對(compare)
>>> 5 > 2
True
>>> 3 < 1
False
>>> 5 > 2 * 2
True
>>> 1 == 1
True
--------------------------------------------------------------------------

If...elif...else

MyAge=int(input("Please input some value"))    #輸入文字並轉型
if MyAge>100:
print('Ha..too..Hight')
elif MyAge>40 and MyAge<=79:
print('Ha..Ha..Just Right')
else:
print('Ben...Ben..too...Low')

----------------------------------------------------------------------------------
自訂函數

def Hello(Lenag):
        if Lenag=="CHINESE":
                print("你好!!")
        elif Lenag=="English":
                print("Hello!!")
        elif Lenag=="Japanese":
                print("こんにちは")
        elif Lenag=="French":
                print("Bonjour!!")
        else:
                print("I don't know,What are you saing?")

run to shell的結果如下:
>>> Hello("French")
Bonjour!!

--------------------------------------------------------------------------------
迴圈(Loop)
ex1:

for i in range(1,6):
    print(i)

結果如下:(注意:range 會輸出第二個參數的前一個整數)
1
2
3
4
5

ex2:

warriors=['張飛','關羽','趙雲','馬超','黃忠','太史慈','夏候惇']
for name in warriors:
    print(name)

結果如下:
張飛
關羽
趙雲
馬超
黃忠
太史慈
夏候惇