Python basic three-----Basic data type Summary supplement

Source: Internet
Author: User

1. A small problem:

Deleting a list element during a loop will be different from the expected result, but will not cause an error

Lis =[11,22,33,44,55] forIinchRange (len (LIS)):#When you use range () for the first time, the value is saved by default, and the value in the subsequent loop value is fixed to dead.    Print(i)delLis[i]Print(LIS)delLis[1::2]Print(LIS) forIinchRange (0, 2):    Print(I,type (i))delLis[i]Print(LIS)
View Code

For dictionary dict: Cannot delete dictionary key value pair during loop, error

DIC = {'K1':'v1','K2':'v2','A3':'v3'} forIinchDIC:if 'k' inchI:delDic[i]
Correct procedure 1
Dic1 = {} for in dic:    if'k' is not inch I:         = Dic1print(DIC1)
Correct Practice 2: Put the  deletion to the list, the loop list to delete

DIC = {'K1':'v1','K2':'v2','A3':'v3'}l= [] forIinchDIC:if 'k' inchi:l.append (i) forIinchL:delDic[i]Print(DIC)

2. Tuple issues (Interview questions)

 
TU1 = (1= (2)print(Tu1,type (TU1))   #  1 <class ' int ' >Print (Tu2,type (TU2))   #  (2,) <class ' tuple ' >= ([1= ([1],)  Print(Tu1,type (TU1))    #  [1] <class ' list ' >print(TU2, Type (TU2))    #  ([1],) <class ' tuple ' >

Python basic three-----Basic data type Summary supplement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.