Chapter 2 exercises

Source: Internet
Author: User

1. Use the code: concatenate each element of the list into a string using an underscore, Li = ['Alex ', 'Eric', 'rain']

This question is mainly about the concatenation method of the test string, jion method, S = "" Li = ['Alex ', 'Eric', 'rain'] S = "_". jion (Li)

2. Search for elements in the list, remove spaces for each element, and search for all elements starting with a or a and ending with C.

Li = ["Alec", "Aric", "Alex", "Tony", "Rain"]

Tu = ("Alec", "Aric", "Alex", "Tony", "Rain ")

Dic = {'k1 ': "Alex", 'k2': 'aric', "K3": "Alex", "K4": "Tony "}

li=["alec","aric","Alex","Tony","rain"]for i in li:         i=i.strip()     if i.startswith("a")ori.startswith("A") and i.endswith("c"):         print(i)
View code

3. write code with the following list to implement each function as required

Li = ['Alex ', 'Eric', 'rain']

  • Calculate the list length and Output
  • Append the element "seven" to the list and output the added list.
  • Insert the "Tony" element in the second position of the list and output the added list.
  • Modify the element of the 2nd position in the list to "Kelly" and output the Modified list.
  • Delete the element "Eric" in the list and output the Modified list.
  • Delete the 2nd elements in the list and output the values of the deleted elements and the list after the deleted elements.
  • Delete the 3rd elements in the list and output the list after the elements are deleted.
  • Delete the 2nd to 4 elements in the list and output the list after the elements are deleted.
  • Please reverse all the elements in the list and output the reversed list
  • Please use the index of the For, Len, Range Output list
  • Please use enumrate to output the list element and serial number (sequence number starts from 100)
  • Please use all elements in the for loop output list
    Li = ['Alex ', 'Eric', 'rain'] # Calculate the list length and output print (cold (LI) # Add the list element "seven ", and output the added list Li. append ("seven") print (LI) # Insert the element "Tony" at the first position in the list and output the added list of Li. insert (0, "Tony") print (LI) # modify the element of the 2nd position in the list to "Kelly ", and output the Modified list Li [1] = "Kelly" Print (LI) # Delete the element "Eric" in the list and output the Modified list Li. remove ("Eric") # specify the print (LI) element # Delete the 2nd elements in the list, and output the deleted element value and the list after the deleted element li_pop = Li. pop (1) # specify the subscript position print (li_pop, Li) # Delete the 3rd elements in the list, and output the list del Li [2] # specify the subscript position print (LI) # Delete the 2nd to 4 elements in the list, and output the list of deleted elements # Please reverse all the elements in the list, and output the inverted list # Use the index for I in range (LEN (LI) of the For, Len, and range output lists: Print (I) # Use enumrate to output the list element and serial number (sequence number starts from 100) for index, Val in enumerate (Li, 100): Print (INDEX) # Use all elements of the for loop output list for I in Li: Print (I)
    View code

    4. write code. The following list is provided. implement each function as required.

    Li = ["hello", 'seven', ["mon", ["H", "Kelly"], 'all'], 123,446]

    • Output "Kelly" based on the index"
    • Use the index to locate the 'all' element and change it to "all", for example, Li [0] [1] [9]...
      # Output "Kelly" Li [2] [1] [1] based on the index # Use the index to locate the 'all' element and change it to "all", for example: li [0] [1] [9]... li [2] [2]. upper () Li [2] [2] = "all"
      View code

      5. write code with the following tuples. implement each function as required.

      Tu = ('Alex ', 'Eric', 'rain ')

      • Calculate and output the length of the tuples
      • Obtain the 2nd elements of the tuples and Output
      • Obtain the 1-2 elements of the tuples and Output
      • Use for to output the elements of the tuples
      • Use for, Len, range to output the index of the tuples
      • Use enumrate to output the ancestor element and serial number (sequence number starts from 10)
        5. write code. There are the following tuples. implement each function according to the functional requirements. Tu = ('Alex ', 'Eric', 'rain ') calculate the length of the tuples and output the 2nd elements for obtaining the tuples. Then, output the 1-2 elements for obtaining the tuples, and output the elements that use for output tuples. Use for, Len, and range to output the index of the tuples. Use enumrate to output the ancestor element and sequence number (the sequence number starts from 10)
        View code

        6. There are the following variables. Please implement the required functions.

        Tu = ("Alex", [11, 22, {"K1": 'v1 ', "K2": ["Age", "name"], "K3 ": (11,22, 33)}, 44])

        • Describe the features of the ancestor
        • Can the first element "Alex" in the TU variable be modified?
        • What type is the value of K2 in the TU variable? Can it be modified? If yes, add an element "seven" to it"
        • What is the value type corresponding to "K3" in the TU variable? Can it be modified? If yes, add an element "seven" to it"
          # Describe the features of the original ancestor 1. Ordered Set 2. Data is obtained through offset 3. The object is an immutable object and cannot be modified in the original place, without sorting, modification, or other operations. # Can the first element "Alex" in the TU variable be modified? The third feature of the tuples in the above question # what type of values does the "K2" in the TU variable correspond? Can it be modified? If yes, add an element "seven" type (Tu [1] [2] ["K2"]) -- list can be tu[ 1] [2] ["K2"]. append ("seven") # What is the value type of "K3" in the TU variable? Can it be modified? If you can, add an element "seven" type (Tu [1] [2] ["K3"]) tuple # tuple tuples to it. The read-only list cannot be modified. No
          View code

          7. Dictionary

          Dic = {'k1 ': "V1", "K2": "V2", "K3": [11, 22, 33]}

          • Please output all keys cyclically
          • Please output all values cyclically
          • Please output all keys and values cyclically
          • Add a key-value pair to the dictionary, "K4": "V4", and output the added dictionary.
          • In the dictionary modified, the value corresponding to "K1" is "Alex", and the modified dictionary is output.
          • Append an element 44 to the value corresponding to K3 and output the modified dictionary.
          • Insert element 18 at the 1st position of the value corresponding to K3, and output the modified dictionary.
            # Please output all keydic cyclically. keys () # Please output all valuedic cyclically. values () # Please output all keys and valuedic cyclically. items () # Add a key-value pair to the dictionary, "K4": "V4 ", output the dictionary DIC ["K4"] = "V4" # in the dictionary, change the value corresponding to "K1" to "Alex ", output the modified dictionary DIC ["K1"] = "Alex" {'k2': 'v2', 'k1': 'Alex ', 'k3': [11, 22, 33]} # Add an element 44 to the value corresponding to K3, and output the modified dictionary DIC ["K3"]. append (44) {'k2': 'v2', 'k1 ': 'v1', 'k3 ': [11, 22, 33, 44]} # insert element 18 at the first position of the value corresponding to K3, and output the modified dictionary DIC ["K3"]. insert (1, 18) {'k2': 'v2', 'k1': 'v1 ', 'k3': [11, 18, 22, 33]}
            View code

            8. Conversion

            • Convert string S = "Alex" to list
            • Converts the string S = "Alex" to the ancestor
            • Convert the list li = ["Alex", "Seven"] to a metagroup
            • Convert yuanzu Tu = ('Alex ', "Seven") to a list.
            • Convert the list li = ["Alex", "Seven"] to a dictionary and the dictionary key increments backward according to 10.
              # Convert string S = "Alex" to list (s) # convert string S = "Alex" to ancestor tuple (s) # convert list li = ["Alex ", "Seven"] converting to a metagroup tuple (LI) # converting yuanzu Tu = ('Alex ', "Seven") to a list (TU) # convert the list li = ["Alex", "Seven"] to a dictionary, and the dictionary key increments backward after 10 (ZIP ([I for I in range (10, len (LI) + 11)], Li ))
              View code

              9. Element Classification

              There is a set of values [, 44,] as follows to save all values greater than 66 to the first key of the dictionary, save the value smaller than 66 to the value of the second key.

              That is, {'k1 ': All values greater than 66, and 'k2': All values smaller than 66}

              li = [11, 22, 33, 44, 55, 66, 77, 88, 99]dic = {    "k1": [],    "k2": [],}for i in li:    if i >= 66:        dic["k1"].append(i)    else:        dic["k2"].append(i)print(dic)
              View code

               

Chapter 2 exercises

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.