Python basic knowledge list, Ganso, dictionary/String.

Source: Internet
Author: User
Tags new set

A list of basic knowledge:

The list is defined by [] and can be appended and deleted by querying the index values.

Common usage is as follows

names = [' Zhang ', ' Zhuang ', [' Alx ', ' Bob '], ' Chen ', ' Qiang ']print (len (names))  # Gets the list length print ("Aaffaa". Count ("a"))  # Get a number of occurrences of a string print (Names[1:3])  # Slice print (names[-1])  # get the last element print (names[-2:])  #  Names.append (' qianqq ') #追加模式names. Insert (1, ' Chengrouhua ')  # inserting Names.insert (3, "Xinzhiyu") names[2] = "IXNIXN"  # Modify Names.remove ("Qiang") # del names[0]# names.pop (0)  Delete element and return the value of the deleted element del Names[2:4]  # Slice Delete for i in names:< C10/>print (i)  # Loop through the list for I in range (1, 2):    print (i)   #每隔2位数打印print (Names[::2])  # interlace filter Print (' Zhuang ' in names) #判断字段是否在列表里names2 = Names.copy () #浅浅的copyprint (dir (names))  #获取列表所支持的方法 # #分割 # s = ' Hello World ' # S.S Plit (') # [' Hello ', ' World ']# s2= ' Hello,world ' # s2.split (', ') # # #连接 # l = [' Hi ', ' Eva ']# '! '. Join (L) # ' Hi!eva '

 

2. Yuan Zu, is a kind of not become a list form, once the definition can not be changed, generally used for database connection, binding IP and port use.

Ganso Features:

1. Immutable, when defining the ancestor if there is only one element that must be distinguished by commas after ganso. Note: The meta-ancestor can be modified if there are mutable elements such as lists

2. Only query function. Querying internal data through indexed values.

3. Dictionaries: defined in the form of {k1:v1} in key:value form to create, is a mutable data type

Dictionaries can be added and modified by the following methods:

info={#         ' stu1101 ': "Wu  teng  lang", #         ' stu1102 ': "Cang", #         ' stu1103 ': "Xiaoze", #         ' stu1104 ': " Bo Jie "#         }# # b={1:2,3:4}# # # # print (info[' stu1101 '])   #查看字典stu1101 key corresponding to value# # info[' stu1101 ']= ' mmmmm '   # If the dictionary has this key value then modify, no then add # # info[' stu1105 ']= ' sssss ' # # Info.setdefault (' stu1101 ', ' alxe ')   #如果key的值 in the dictionary is modified, no then add # # Info.update (b) # Appends the field of the B dictionary to the info dictionary, if repeated updates are followed by fields. # # Print (info) # # Info.pop (' stu1105 ')  #通过key的值来删除字典的对应的值 pop method has a return value that returns the value of the deleted element # # # # # print (Info.get (' stu1106 ') # This is a way to get the value through the key so it won't be an error, no then return none# # Print (' stu1104 '  in info)  #判断key的值在不在字典中 to be judged by key values. # # Print (Info.keys ()) #返回所有的key  #打印所有字典key的 Value # # print (Info.values ()) #返回字典所有的value值 # # Print (Info.items ()) # Return to the Yuan Zu c15/> the key with the value of the meta-ancestor #  #   for K in info:# K is the key   of info traversal dictionary key value Printing # # Print     (K,info[k])

Set: The whole of one or more elements is called a set.

Features of the collection:

1. Natural weight

2. The inner elements of the collection are unordered

3. By operation you can take out two sets of eligible element types to form a new set

The common set operations are as follows:

b={1,2,3,4,5,5,5,6}  #互异性天然去重的原则 # c={2,3,4,8,9,10,56,45}# info={#     ' stu1101 ': "Wu  teng  lang", #     ' stu1102 ': "Cang", #     ' stu1103 ': "Xiaoze", #     ' stu1104 ': "Bo Jie" #     }# # #print (b-c)  # The idea is to take each element of B minus the elements in C and get a different set. # # Print (b|c)  #并集两个集合的合并 # # print (b^c)  #两个集合中不是共同部分的元素取出来 # # D=set (info)  #设置集合  Dictionary is the value of key # # print (d # B.add (  #增加元素 # b.remove) #删除元素 If there is no element then error # # B.discard (4) #删除元素  no error # # print (b) # # # # B.pop ()  #删除元素 (any) # # print (b)

Some of the operating postures of characters are as follows

# name= ' aaalcx lill ' # Print (Name.index ("L")) # Print (Name.count ("L")) # Statistic string number of a string # print (Name.capitalize ()) #首字母大写 # Print (Name.center, ' * ')) #把name放在中间 50 characters # print (Name.replace (' l ', ' l ', 4)) #代替 three parameter # print ("123". IsDigit ()) #判断是否是整数 # print ("Aaaooolll". EndsWith (' ll ')) # msg = "My name is {}, and the age is {}" # Print (Msg.partition (' is ')) #is in the middle of the meta-ancestor # print (msg . Format ("ZJ", "+") # Name.join ("CX") # Print (Name.join ("Cxb") # to manipulate numbers "' Name= ' abcd Lill ' name1=" ABC \tlill "#  Name.strip () #变成字符串 # name.title () #变成标题 # Print (Name.rsplit ()) print (Name.capitalize ()) #首字母大写print (Name.count ("a")) # Statistical character print (Name.center ("*")) #一共 print 50 characters not enough to fill, put name in middle print (Name.endswith ("ll")) # to determine the end of the string print (Name1.expandtabs (30)) Print (Name.find ("Li")) # Find Index! The string can be sliced name3= "My name is {name} and the age was {age}" print (Name3.format (name= "Alix", age= ")" Print (Name3.format_map ({" Name ":" Alex "," Age ":"} ") Print (Name3.index (" y ")) print (Name.isalnum ()) # is not the Arabic numerals print (Name.isalpha ()) # Judge the Pure English character print (Name.isdecimal ()) #判断十进制print (name.isDigit ()) # Determines if the integer print (Name.isidentifier ()) #判断是不是合法的标识符 (variable name) print (Name.islower ()) # is lowercase! Print (Name.isnumeric ()) #判断是否是数字!! Just the number returned Trueprint (Name.isspace ()) print (Name.istitle ()) print (Name.isprintable ()) # is printable! The TTY file device terminal file Print (Name.isupper ()) # is all uppercase print ("* * * *") print ("|"). Join (["1", "2", "3", "4"]) # The string inside the list becomes the string print ("* * * *") print (Name.ljust (50, "-")) # after the top *print (name.rjust (50, '-')) Print (Name.lower ()) # Uppercase becomes lowercase print (name.upper ()) print ("\nalex\n". Lstrip ()) #左边去空格或者回车print ("\nalex\n". Rstrip ()) # Go to the right side of the space or enter print (Name.strip ()) #两边都去空格print ("Funny keys\n") V=str.maketrans ("ABCdef", "123456") # number corresponding! Print ("Alex Li". Translate (v)) print ("Alex L". Replace (' l ', ' l ', 1)) print ("Alex Li". RFind ("L")) #找到最右边的的l and return to print ( Name.split ()) # The photo space is divided into the list print (Name.split (' A ')) # Photo A divided into list print (Name.splitlines ()) # According to line break Linux Windowprint (NAME.S Tartswith (' a ')) print (Name.swapcase ()) #大小写转换print (Name.title ()) # variable title print ("Alex Li". Zfill (50)) # 50 not enough in front of 0 "" Print ("Name AAA". Split ()) name= ' AA aaaa ' Print (Name.isaLpha ()) # Judging is not the letter! Print (len (' Hello '. Encode ()))

  

Python basic knowledge list, Ganso, dictionary/String.

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.