python | 基本数据类型函数


mark一下

Sequence

Operations on Any Sequence

Operation NameOperatorExplanation
indexing[ ]Access an element of a sequence
concatenation+Combine sequences together
repetition*Concatenate a repeated number of times
membershipinAsk whether an item is in a sequence
lengthlenAsk the number of items in the sequence
slicing[ : ]Extract a part of a sequence

List

Methods Provided by Lists

Method NameUseExplanation
appenda_list.append(item)Adds a new item to the end of a list
inserta_list.insert(i,item)Inserts an item at the i^th position in a list
popa_list.pop()Removes and returns the last item in a list
popa_list.pop(i)Removes and returns the i^th item in a list
sorta_list.sort()Modifies a list to be sorted
reversea_list.reverse()Modifies a list to be in reverse order
deldel a_list[i]Deletes the item in the i t h i^th ith position
indexa_list.index(item)Returns the index of the first occurrence of item
counta_list.count(item)Returns the number of occurrences of item
removea_list.remove(item)Removes the first occurrence of item

String

Methods Provided by Strings

Method NameUseExplanation
centera_string.center(w)Returns a string centered in a field of size w
counta_string.count(item)Returns the number of occurrences of item in the string
ljusta_string.ljust(w)Returns a string left-justified in a field of size w
lowera_string.lower()Returns a string in all lowercase
rjusta_string.rjust(w)Returns a string right-justified in a field of size w
finda_string.find(item)Returns the index of the first occurrence of item
splita_string.split(s_char)Splits a string into substrings at s_char

Set

Operations on a Set

OperatorUseExplanation
inx.in(set)Set membership
lenlen(set)Returns the cardinality (i.e. the length) of the set
|set1 | set2Returns a new set with all elements from both sets
&set1& set2Returns a new set with only the elements common to both sets
-set1 - set2Returns a new set with all items from the first set not in second
<=set1<=set2Asks whether all elements of the first set are in the second

Methods Provided by Sets

Method NameUseExplanation
unionset1.union(set2)Returns a new set with all elements from both sets
intersectionset1.intersection(set2)Returns a new set with only the elements common to both sets
differenceset1.difference(set2)Returns a new set with all items from first set not in second
issubsetset1.issubset(set2)Asks whether all elements of one set are in the other
addset.add(item)Adds item to the set
removeset.remove(item)Removes item from the set
popset.pop()Removes an arbitrary element from the set
clearset.clear()Removes all elements from the set

Dict

Operators Provided by Dictionaries

OperatorUseExplanation
[]my_dict[k]Returns the value associated with k, error otherwise
inkey in my_dictReturns True if key is in the dictionary, False otherwise
deldel my_dict[key]Removes the entry from the dictionary

Methods Provided by Dictionaries

Method NameUseExplanation
keysmy_dict.keys()Returns the keys of the dictionary in a dict_keys object
valuesmy_dict.values()Returns the values of the dictionary in a dict_values object
itemsmy_dict.items()Returns the key-value pairs in a dict_items object
getmy_dict.get(k)Returns the value associated with k, None otherwise
getmy_dict.get(k,alt)Returns the value associated with k, alt otherwise
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值