廖家慧python集合set元組tuple清單或串列list迴圈輸出str,int

w3schools截圖

w3schools程式碼

#廖家慧 集合{},清單[],元組(),字典{key:value}
s = {"台積電", "鴻海", "聯發科", "中華電", "台塑化"}
t = ("台積電", "鴻海", "聯發科", "中華電", "台塑化")
list = ["台積電", "鴻海", "聯發科"]
d = {2330:"台積電", 2317:"鴻海", 2454:"聯發科"}
print("s型態" + str(type(s)))
print("t型態" + str(type(t)))
print("d型態" + str(type(d)))
print("t型態" + str(type(list)))
#字串與字串+
i = 0
for a in t:  
      i = i + 1
      print("台灣市場價格第" + str(i) + "大公司是")
      print("   " + a)
print("聯發科的位置" + str(t.index("聯發科")))
'''python大區塊的註解,前後用三個引號 str 轉成字串 int 轉成數字
'''

w3schools元組tuples的方法

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

w3schools清單或串接lists的方法

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集合sets的方法

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

教學影片

留言

張貼留言

這個網誌中的熱門文章

2024年第二次期貨商業務員測驗「期貨交易法規」

衍生性商品風險管理

廖家慧tkinter視窗套件Entry工具代替OptionMenu工具