Quick actions

cmd+k|ctrl+k

Navigation

Languages

20240417

Snippet info

Language

Python

Visibility

public

Author

hkchun

Created

2024-04-17T01:58:46.17511Z

Updated

2024-04-17T04:54:39.184941Z

print("### item應用 ###")
a = {"name":"john","age":30,"address":"main street","phone":1234} 
for key,value in a.items():
    print (key,value)

'''print ("paython 3.8")
a = {"name":"john","age":30,"address":"main street","phone":1234} 
#[key,value for key,value in a.items() if (values>10)]:
for key, value in [(key, value) for key, value in a.items() if value > 10]:
    print (key,value)'''

print("### *應用 List 去 TRUPLE###")
a,*b,(c,d,e) = [1,2,3,'xyz']
print(a)
print(b)
print(c)
print(d)
print(e)
print("########")
a = [1,2,3,'xyz']
print(a[3][0])
print("########")
a = [1,'xyz',3, 4]
print(a[1][0])
print("########")
a,*b,(c,*d)= [1,2,3,'python']
print(a)
print(b)
print(c)
print(d)
print("########")
def func1(a, b, c):
    print(a, b, c)

func1(10, 20, 30)
# example 1 : simply destructure
print("# example 1 : simply destructure")
l=[1,2,3,4,5,6]
a,b =l[0],l[1:]
print(a,b)
# example 2 :destructure *
print("# example 2 :destructure *")
a,*b = l
print(a,b)
# example 3 : string as list
print("# example 3 : string as list")
a,*b='pathon'
print(a,b)
# example 4 : substring destructure
print("# example 4 : substring destructure")
s ='pathon'
a,b,c,d=s[0],s[1],s[2:-1],s[-1]
print(a,b,c,d)
a,b,*c,d=s
print(a,b,c,d)
# example 5: destructure and re-grouo
print("# example 5: destructure and re-grouo")
l1=[1,2,3]
l2=[4,5,6]
l= [*l1,*l2]
print(l)
# example 6: destructure unordered data
print("# example 6: destructure unordered data")
s ={10,20,30,'d'}
a,b,*c =s
print(a,b,c)
*a,=s #*a, *攞晒數字 ,變成TRUPLE
print(a)
# example7 :polyfill
print("# example7 :polyfill")
print("help(set)個DOC自己看")
#help(set)
# example 8 join set
print("# example 8 join set")
s1={1,2,3}
s2={4,5,6}
s3={7,8,9}
s4={4,2,'b'}
print(s1.union(s2).union(s3).union(s4))
print(s4.union(s2).union(s3).union(s1))
print(*s1,*s2,*s3,*s4)
# example 9 combine dictionary
print("# example 9 combine dictionary")
d1= {"key1":1,"key1":2}
d2= {"key2":1,"key1":2}
print({**d1,**d2})
print({**d2,**d1})

print("#"*30)
d1= {"key1":1,"key2":2}
d2= {"key2":1,"key3":2}
print({**d1,**d2})
print({**d2,**d1})
print({"key1":100,**d1,**d2,"key3":200,})
# example 10 nested destructure
print("# example 10 nested destructure")
a,b,(c,d)=[1,2,['x','y']]
print(a,b,c,d)
a,b,(c,d)=[1,2,'xy'] # string as list
print(a,b,c,d)
print("#"*30)
l = [1,2,3,'python']
print (l[0],l[1:-1],l[-1][0],l[-1][1],list(l[-1][2:]))

# example 11 postion argument
print("# example 11 postion argument")
def func1(a,b,c):
    print(a,b,c)
func1(10,20,30) 
# example 12
print("example l2")
func1(b=20,a=10,c=30)
func1(10,c=30,b=20)
# example 13 *args
print("# example 13 *args")
def func2(a,b,*args,d):
    print(a,b,args,d)
# func2(10,20,"a","b",100)行唔到因為* 攞晒數值
print("# error can't use position argument after *args")
func2(10,20,"a","b",d=100)  #then it is ok行因為* 攞晒數值,但定義了d=100
# example 14 *
print("# example 14 *")
def func3(*args,d):
    print(args,d)
func3(d="hello")
'''func3() #error missing keyword argument'''
# example 15
print("# example 15")
def func4(*args,d=100):
    print(args,d)
func4(1,2,3) #arg =[1,2,3], d=100
func4() # arg =(), d =100

# example 16
print("# example 16")
def func5(*, d=100):  # end positional arguments
    print(d)
func5() 
'''func5(10,d=200) #dont work'''
print("#"*30)
def func5(*, d): 
    print(d)
func5(d=200)
print("#"*30)
def func5(*,func):
    print(func())
func5(func=func4)
INFO