当前位置:网站首页>Tuples, lists, sets, and some notes on exception handling
Tuples, lists, sets, and some notes on exception handling
2022-07-22 09:43:00 【Shangguan dada】
Xi xi xi ,Python Xiaobai came to report
This is a b Explain the video on the station , I'm just a child who likes taking notes
Some places , If you want to realize, pay attention to delete those annotations
''' Tuples tuple And list similar , The difference is tuple Can't modify tulpe Write in parentheses , The elements are separated by commas The elements of tuples are immutable , But it can contain variable objects , Such as list Be careful : Define a 1 An element of tuple, Must be added ,. You can use subscripts , Slice access , The nature of the visit is the same as list num1 = (1,)# That's the plus , Why num2 = (2) print(type(num1)) print(type(num2)) # increase : Connect tul1 = (1,2,3) tul2 = (3,34,4) tul = tul1 + tul2 print(tul) # Delete : Deleted the entire tuple variable del tul #print(tul)# Will report a mistake NameError: name 'tul' is not defined., # check print(tul1.count(1))# Return the number of occurrences # Change #tul1[0] = 2 This will change the report , So it can't be changed '''
#dict( Dictionaries )
''' A dictionary is an unordered collection of objects , Use key value storage , With extremely fast search speed . Keys must be of immutable type The key must be immutable '''
# Dictionary definition
info = {
"name":" Shangguanhao ","age":19}
# Access to dictionaries
''' print(info["name"]) print(info["age"]) '''
# Accessed a nonexistent key
#print(info["gender"]) Direct access will report an error
''' print(info.get("gender",2 ))# Use get Method , No corresponding key found , Default return :None print(info.get("age",18))# If you find it , Return value # increase info["id"] = "123456" # Delete #[del] del info["age"] # If you access the deleted key value pair after deletion , Will report a mistake # It's fine too del info Delete entire dictionary , If you visit the dictionary again, you will report an error # Sure %s Output brother Zhen dictionary #print("%s"%info) #[clear] #info.clear() print(info)# The output will not report an error , Output an empty dictionary # Change # Just access the change through the key # check print(info.keys())# Get all the keys ( list ) print(info.values())# Get all the values ( list ) print(info.items())# Get all the items ( list ), Each key value is a tuple # Traverse all the values , Automatic assignment for key,value in info.items(): print("key=%s,value=%s"%(key,value)) # The same goes for traversing all values '''
''' # Get the subscript and element content of the list by enumerating mylist = ["a","b","c","d"] for i,x in enumerate(mylist): print(i,x) The resulting output 0 a 1 b 2 c 3 d '''
#set( aggregate ) It can be called a collection of dictionary keys , The elements inside are not repeated and disordered , Repeated elements will be times filtered
s = set([1,1,2,2,3,4,4])
print(s)
''' list [] Orderly Variable type Dictionaries {} disorder key immutable value variable Tuples () Orderly Immutable type aggregate {} disorder Variable type ( No repetition ) '''
exception handling
try: Inside are local variables
# Code snippets that may report exceptions
except :# Type of exception , If it's one, don't add ()
# If there are two or more (error1,error2) Parentheses required
# If you want to include all exceptions , use exception To replace mistakes
# If you need to print an error and there will be no error report, which will cause the program to stop running
# Can be in error Add after as result
else:
Don't have to else, use finally It means that it will be executed no matter whether an error is reported
边栏推荐
猜你喜欢
随机推荐
Solution of exceeding the margin after padding or border when wechat applet width100%
微信小程序富文本插件 Parser
Common properties of flex layout
Oracle error list
[STM32 ]内部独立看门狗IWDG
File read / write operation (files under the specified file directory)
让盒子居中且距离浏览器左右各100px做动画
The error interface of EAS web page preview is blank
自定义滚动条
粘性定位(sticky)详解
Andorid view activity task stack
Wechat applet text limit lines
Responsive layout viewport and common units
window系统下使用nvm安装后node生效但是npm不生效
生物化学复习题V·酶
25.二叉树的中序遍历
生物化学复习III·糖类
ConstraintLayout从0到0.n学习
Three implementation methods of Kingdee EAS unpacking deployment
oracle错误一览表