Python3 Basics-Lists and dictionaries

Source: Internet
Author: User

List

name = ['KZX','666','None','Truth']#InterceptPrint(Name[1:3])#Remove the element labeled 1 to 3 (containing the element with subscript 1, but not 3)
[' 666 ', ' none ']
Print(Name[1:-1])#also remove the element labeled 1 to-1 (that is, subscript 3) (including the element with subscript 1, but not 1)[' 666 ', ' none ']
Print(Name[0:3])#Remove the element labeled 0 to 3 (containing the element with subscript 1, but not 3)
[' Kzx ', ' 666 ', ' none ']
Print(Name[:3])#also remove the elements from 0 to 3
[' Kzx ', ' 666 ', ' none ']
Print(name[1:])#take all elements except subscript 0.[' 666 ', ' none ', ' truth ']Print(Name[1:4])#when the current label is larger than the maximum subscript for the list, it is equivalent to Name[start,max] #(Start is the subscript to start with, Max is the largest subscript of the list)[' 666 ', ' none ', ' truth ']

#AddName.append ("New")#add from LastName.insert (2,"Insert")Print(name) [' Kzx ', ' 666 ', ' Insert ', ' none ', ' Truth ', ' new ']
#ModifyNAME[2] =" Change"Print(name) [' Kzx ', ' 666 ', ' change ', ' none ', ' Truth ', ' new ']
#DeletedelNAME[2]#Delete the element labeled 2 in the listName.remove ("KZX")#Specify Delete elementPrint(name) [' 666 ', ' none ', ' Truth ', ' new ']name.pop ()#Delete the last value of a list#Merge 2 listsb = [1, 2, 3]name.extend (b)Print(name) [' 666 ', ' none ', ' Truth ', 1, 2, 3]
#CopyCopy_name =name.copy ()Print(copy_name) [' 666 ', ' none ', ' Truth ', 1, 2, 3]
#number of Statistics element occurrencesnames = ['KKK','RR','KKK','e','RR', 1, 2, 3]Print(Names.count ("KKK")) 2
#Sortc = [1, 2, 3]c.sort ()Print(c)#Python3 Different data types cannot be sorted together[1, 2, 3]
C.reverse ()Print(c)#reversal
[3, 2, 1]
# Get subscript Print (Names.index ("KKK"))  # returns only the first subscript found 0

Dictionary

DIC = {' name ': ' A ', ' age ': +, ' gender ': ' Middle '}# add dic[' habbit '] = ' a_lot ' Print (DIC) {' name ': ' A ', ' gender ': ' Middle ', '  Habbit ': ' A_lot ', ' age ': 21}# modify dic[' habbit '] = ' none ' Print (DIC) {' name ': ' A ', ' gender ': ' Middle ', ' habbit ': ' None ', ' age ': 21}# Delete dic.pop (' habbit ')  # or use (Del dic[' Habbit ']) print (DIC) {' name ': ' A ', ' gender ': ' Middle ', ' age ': 21}# find Print ( "Age" in dic) Trueprint (Dic.get ("Age")  # or use (dic[' age ')) 21# Othersprint (Dic.values ()) print (Dic.keys ()) dict_ VALUES ([' A ', ' middle ', +]) dict_keys ([' Name ', ' gender ', ' age ']) # traversal # method 1for key in dic:    print (key, Dic[key]) # method 2for K , V in Dic.items ():  # will first turn the dict into a list, the data in the big time Mo with    print (k, v)

Weak slag I'll introduce you to the list and dictionary of Python3, and a little mention of tuples

About tuples

NAMES  =  (" Al ", " U ", " C ")

Tuples are actually similar to the list, but also to save a group of numbers, it is not once created, it can not be modified, so it is called a read-only list

[1, 2, 3] [3, 2, 1]

Python3 Basics-Lists and dictionaries

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.