程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Collect several Python tips that are easy to use

編輯:Python

CDA Data Analyst Produce

author :CDA The star lecturer Cao Xin

edit :Mika

01 Python And None No 0
Today we are going to talk about none Follow 0 The relationship between .

I also wrote the title very clearly ,none No 0 .

How can we verify this ? At the same time, we put none Follow 0 Put it on both sides of the equal sign , Let's take a look , It returns the result that false.

none In fact, it is a special null value , It can only be none is none yes true. and none be equal to none The time is true, therefore none No 0 , You must remember .

02 Python It traverses the dictionary in order
Let's talk about traversing dictionaries in order .

To tell the truth is a need for a more false proposition , But we still try our best to let everyone have a try .

I have a dictionary here a_dict , its key yes 2,3,1 , In fact, we can feel that it is not arranged in a certain order .

So if we print directly , its key The order of is still 2,3,1, If I want it to be 1,2,3 To sort , What should I do ?

Here I will introduce a method called sorted This built-in function .

That is, before you print , First you will a_dict Of keys First sorted Sort out , After sorting , He will sort in a default way , hold 1,2,3 Arrange the order for you , That's what we're going to get .

But what I want to tell you is , Actually, dictionaries have no order , Then we must specify a sort method for him , It is possible to print in sequence , If you meet in the future , Just remember one point , Dictionary it has no order .

03 Python To delete dictionary elements
Let's learn how to delete the elements of the dictionary .

Here I have a a_dict , Zhang San 、 Li Si 、 Wang Wu's height .

That way 1 It's using delete This method , Abbreviations are del , direct del Space a_dict, Specify the of the element you want to delete key, For example, Li Si .

Let me run it , First run the dictionary , then delete, No results returned .

Let's take another look a_dict When , I found that Li Si had been deleted .

The first 2 There are three ways we still need to a_dict Give me a copy , Because I have already deleted .

The first 2 One method is called pop , Its usage is a_dict.pop, Then specify the... Of this dictionary key.

Run it , You will find that it returns value , Follow the one above del There is a difference , But the end result is the same , Also delete this element .

Have you learned these two methods ?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved