2023年3月27日 星期一

連宸宏python:集合set串列或清單list字典dictionary元組tuple

w3schools截圖

w3schools程式碼

#連宸宏 集合{},字典{key:value,},元組(),清單]
s= {"台積電", "鴻海", "聯發科"}
t= ("台積電", "鴻海", "聯發科")
list= ["台積電", "鴻海", "聯發科"]
d= {2330:"台積電",2317:"鴻海",2454:"聯發科"}
print("型態" + str(type(s)) + str(s))
print("型態" + str(type(t)) + str(t))
print("型態" + str(type(d)) + str(d))
print("型態" + str(type(list)) + str(list))
#字串與字串才能+所以要用str轉成字串
i = 0
for a in list: 
  i = i+1
  print("台灣第" + str(i) + "大的公司是")
  print("    " + a)
'''大區域註解用三個引號set集合沒有順序unordered, 練習使用迴圈輸出集合內的內容'''

w3schools集合方法

Python has a set of built-in methods that you can use on sets.
MethodDescription
add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
difference_update()Removes the items in this set that are also included in another, specified set
discard()Remove the specified item
intersection()Returns a set, that is the intersection of two other sets
intersection_update()Removes the items in this set that are not present in other, specified set(s)
isdisjoint()Returns whether two sets have a intersection or not
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update()inserts the symmetric differences from this set and another
union()Return a set containing the union of sets
update()Update the set with the union of this set and others

w3schools清單或串列方法

Python has a set of built-in methods that you can use on lists.
MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list
Python has a set of built-in methods that you can use on lists.
MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

w3schools元組方法

Python has two built-in methods that you can use on tuples.
MethodDescription
count()Returns the number of times a specified value occurs in a tuple
index()Searches the tuple for a specified value and returns the position of where it was found

2023年3月20日 星期一

連宸宏Pyton字典

w3schols截圖

w3schols練習程式碼

#字典 keys:values, 連宸宏
#w3schools原來 字串:字串,改成 整數:字串
a =	{   #市場價值最大的五家公司
  2330: "台積電",
  2317: "鴻海",
  2454: "聯發科",
  2412: "中華電",
  6505: "台塑化"
  }
print(a)
print(a[6505])
print(a.get(2330))#功能同 a[2330]
print(a.keys())   #keys()方法列出key搜尋鍵
print(a.values()) #keys()方法列出values值
b = a.copy()
print("列出b " + str(b))
print(b[2317])
print(b.clear())
a.update({2308: "台達電"})
print(a.values())
print("用迴圈列出字典a的所有值")
for t in a:
   print(a[t])
.

W3chools字典用法

a
MethodDescription
clear()Removes all the elements from the dictionary
copy()Returns a copy of the dictionary
fromkeys()Returns a dictionary with the specified keys and value
get()Returns the value of the specified key
items()Returns a list containing a tuple for each key value pair
keys()Returns a list containing the dictionary's keys
pop()Removes the element with the specified key
popitem()Removes the last inserted key-value pair
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()Updates the dictionary with the specified key-value pairs
values()Returns a list of all the values in the dictionary