Old boy python stack s21day04 homework

Keywords: Python

  1. Brief description of the difference between interpretative language and translational language?

    • Compiled Language: When the code is finished, the coder will compile it completely into another file that is closer to the machine language, and then send it to the computer for execution.
    • Interpretative Language: After the code is finished, the interpreter interprets the code line by line, which is executed while interpreting.
  2. List the data types of Python you know?

    int str bool list tuple

  3. Write code, have the following list, according to the requirements of each function.

    li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
    
    • Calculate the length of the list and output it

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      print(len(li))
      
    • Get all the even-numbered values by step size and print out the acquired list

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      print(li[::2])
      
    • Add the element "seven" to the list and output the added list

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      li.append("seven")
      print(li)
      
    • Insert the element "Tony" at the first place in the list and output the added list.

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      li.insert(1,"Tony")
      print(li)
      
    • Modify the element in the second place of the list to "Kelly" and output the modified list.

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      li[2]='Kelly'
      print(li)
      
    • Change the value of the third position of the list to "too white" and output the revised list.

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      li[3]='Taibai'
      print(li)
      
    • Please append each element of the list l2=[1,""a,""3,4,""heart"] to the list li and output the added list.

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      l2=[1,"a",3,4,"heart"]
      for i in range(len(l2)):
          li.append(l2[i])
      print(li)
      
    • Please add each element of the string s = QWERT to the list li, one line of code is implemented, and circular addition is not allowed.

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      s = "qwert"
      li.extend(','.join(s).split(','))
      print(li)
      
    • Delete the element "ritian" from the list and output the added list

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      li.remove('ritian')
      print(li)
      
    • Please delete the second element in the list and output the list after deleting the element.

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      li.pop(1)
      print(li)
      
    • Please delete the second to fourth elements in the list and output the list after deleting the elements.

      li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
      for item in range(1,4):
          li.pop(1)
      print(li)
      
  4. Please invert the name of the string in three ways: playing Angry Birds in bed at three o'clock in the middle of the night (step length, while, for)

    name = "Little Black Playing Angry Birds in Bedding at 3:00 in the middle of the night"
    print(name[::-1])
    
    name = "Little Black Playing Angry Birds in Bedding at 3:00 in the middle of the night"
    i=len(name)-1
    name2=''
    while i>=0:
        name2 +=name[i]
        i -=1
    print(name2)
    
    name2=''
    for i in range(len(name)):
        name2 +=name[len(name)-1-i]
    print(name2)
    
  5. Write the code, with the following list, using slices to achieve each function

    li = [1, 3, 2, "a", 4, "b", 5,"c"]
    
    • Form a new list by slicing the li list [1,3,2]

      li = [1, 3, 2, "a", 4, "b", 5,"c"]
      print(li[:3])
      
    • Form a new list by slicing the li list ["a", "4", "b"]

      li = [1, 3, 2, "a", 4, "b", 5,"c"]
      print(li[3:6])
      
    • Form a new list by slicing the li list [1,2,4,5]

      li = [1, 3, 2, "a", 4, "b", 5,"c"]
      print(li[::2])
      
    • Form a new list by slicing the li list [3,""a,""b"]

      li = [1, 3, 2, "a", 4, "b", 5,"c"]
      print(li[1:-1:2])
      
    • Form a new list by slicing the li list [3,""a,""b,""c"]

      li = [1, 3, 2, "a", 4, "b", 5,"c"]
      print(li[1::2])
      
    • Form a new list by slicing the li list ["c"]

      li = [1, 3, 2, "a", 4, "b", 5,"c"]
      print(li[-1:])
      
    • Form a new list by slicing the li list ["b", "a", 3]

      li = [1, 3, 2, "a", 4, "b", 5,"c"]
      print(li[-3::-2])
      
  6. Please use the code to achieve cyclic output elements and values: users = ["Wu Peiqi", "Jing Goddess", "Xiao Xia"], such as:

    0 Wu Peiqi
     1 Scene Goddess
     2 Xiao Xia
    
    users = ["Wu Peiqi","Jing Goddess","Xiao Daxia"]
    for i in range(len(users)):
        print("%d %s"%(i,users[i],))
    
  7. Please use the code to achieve cyclic output elements and values: users = ["Wu Peiqi", "Jing Goddess", "Xiao Xia"], such as:

    1 Wu Peiqi
     2 Goddesses of Scenery
     3 Xiao Xia
    
    users = ["Wu Peiqi","Jing Goddess","Xiao Daxia"]
    for i in range(len(users)):
        print("%d %s"%(i+1,users[i],))
    
  8. Write code, have the following list, according to the requirements of each function.

    lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
    
    • Change "k" in list lis to uppercase and print the list.

      lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
      lis[2]=lis[2].upper()
      print(lis)
      
    • Change the number 3 in the list to the string "100"

      lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
      lis[1]= "100"
      lis[3][2][1]='100'
      print(lis)
      
    • Change the string "tt" in the list to the number 101

      lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
      lis[3][2][1][0]='101'
      print(lis)
      
    • Insert the string "locomotive" before "qwe"

      lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
      lis[3].insert(0,"Locomotive")
      print(lis)
      
  9. Write code to achieve the following functions

    • If there is a variable googs = ['car','aircraft','rocket'] prompting the user to choose the goods:

      0, automobile
       1. Aircraft
       2. Rockets
      
    • After the user enters the index, the content of the specified commodity is spliced and printed. For example, if the user enters 0, the commodity you choose is the car.

      googs = ['automobile','aircraft','rocket']
      for i in range(len(googs)):
          print("%d,%s"%(i,googs[i]))
      buy = int(input("Please enter the purchased items:"))
      print("The goods you choose are%s"%(googs[buy]))
      
  10. Please implement it in code

    li = "alex"
    

    Each element of the list is spliced into a string "a_l_e_x" with an underscore

    li = "alex"
    print('_'.join(li))
    
  11. Use the for loop and range to find all even numbers within 0 - 100 and append them to a list.

    li=[]
    for i in range(0,101,2):
        li.append(i)
    print(li)
    
  12. Use the for loop and range to find the number that can be divided by 3 within 0 - 50, and add it to a list.

    li=[]
    for i in range(0,51,3):
        li.append(i)
    print(li)
    
  13. Using for loop and range, we find the number divisible by 3 in the range of 0 to 50, and insert it into the index position of the list. The final results are as follows:

    [48,45,42...]
    
    li=[]
    for i in range(0,51,3):
        li.insert(0,i)
    print(li)
    
  14. Find the elements in the list li, remove the spaces for each element, and find the one that starts with "a", add it to a new list, and print the new list in a loop.

    li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", "  aqc"]
    
    li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", "  aqc"]
    li_new=[]
    for i in range(len(li)):
        li_new.append(li[i].strip())
    print(li_new)
    
  15. Determine whether it can be implemented, and if so, write code to implement it.

    li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
    
    • Please change "WuSir" to "Wupeiqi"

      li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
      li[2]="Wu Peiqi"
      
    • Please amend ("ritian", "barry") to ['day','day']

      li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
      li[3]= ['Sundays','Sun land']
      print(li)
      
    • Please amend 88 to 87

      Tuples cannot be modified.

    • Delete "wenzhou" and insert "Wenzhou" at index 0 of the list

      li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
      li.pop(len(li)-1)
      li.insert(0,"Wen Zhou")
      print(li)
      

Posted by CSmith1128 on Sun, 28 Jul 2019 04:56:24 -0700