python-Basic Exercise 2

Source: Internet
Author: User

1, please use the code to achieve: the use of underline the list of each element of the mosaic into a string, li=[' Alex ', ' Eric ', ' rain '

l1=['Alex'Eric'rain'  Print('%s_%s_%s'% (l1[0],l1[1],l1[2))
l1=['Alex','Eric','rain' n="_". Join (L1)print(n)

2. Find the elements in the list, remove the spaces for each element, and look for all elements that begin with a or a and end with C.

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

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

DiC = {' K1 ': ' Alex ', ' K2 ': ' Aric ', ' K3 ': ' Alex ', ' K4 ': ' Tony '}

3, write code, such as the following table, as required to achieve each function

li=[' Alex ', ' Eric ', ' rain '

    • Calculates the list length and outputs
    • Append the element "seven" to the list and output the added list
    • Please insert the element "Tony" in the 1th position of the list and output the added list
    • Please modify the element in the 2nd position of the list to "Kelly" and output the modified list
    • Please remove the element "Eric" from the list and output the modified list
    • Delete the 2nd element in the list and output the value of the deleted element and the list after the element is deleted
    • Delete the 3rd element in the list and output the list after the element is deleted
    • Delete the 2nd to 4th element in the list and output the list after the element is deleted
    • Invert all the elements in the list and output the inverted list
    • Please use the index of the for, Len, range output list
    • Use Enumrate to output list elements and ordinal numbers (starting from 100)
    • Use the For loop to output all the elements of the list
#calculates the list length and outputsl1=['Alex','Eric','Rain']Print(len (L1))#Append the element "seven" to the list and output the added listL1 = ['Alex','Eric','Rain']l1.append ('Seven')Print(L1)#Please insert the element "Tony" in the 1th position of the list and output the added listL1 = ['Alex','Eric','Rain']l1.insert (0,'Tony')Print(L1)#please modify the element in the 2nd position of the list to "Kelly" and output the modified listL1 = ['Alex','Eric','Rain']l1[1]='Kelly'Print(L1)#Please remove the element "Eric" from the list and output the modified listL1 = ['Alex','Eric','Rain']l1.remove ('Eric')Print(L1)#Delete the 2nd element in the list and output the value of the deleted element and the list after the element is deletedL1 = ['Alex','Eric','Rain']delL1[2]Print(L1)#Delete the 3rd element in the list and output the list after the element is deletedL1 = ['Alex','Eric','Rain']delL1[2]Print(L1)#Delete the 2nd to 4th element in the list and output the list after the element is deletedL1 = ['Alex','Eric','Rain']delL1[1:4]Print(L1)#invert all the elements in the list and output the inverted listL1 = ['Alex','Eric','Rain']l1.reverse ()Print(L1)#Please use the index of the for, Len, range output listL1 = ['Alex','Eric','Rain'] forIinchRange (len (L1)):Print. IO#use enumrate to output list elements and ordinal numbers (starting from 100)L1 = ['Alex','Eric','Rain'] forKvinchEnumerate (l1,100):    Print(k,v)#use the For loop to output all the elements of the list forIinchL1:Print(i)

4, write code, such as the following table, please follow the functional requirements to achieve each function

Li = ["Hello", ' Seven ', ["Mon", ["H", "Kelly"], ' all '], 123, 446]

    • Please output "Kelly" according to the index.
    • Please use the index to find the ' all ' element and modify it to "all", such as: li[0][1][9] ...
Li = ["Hello",'Seven', ["Mon", ["h","Kelly"],' All'], 123, 446]#please output "Kelly" according to the index.Print(li[2][1][1])#Please use the index to find the ' all ' element and modify it to "all"Print(li[2][2]) li[2][2]=' All'Print(LI)

5, write code, there are the following tuple, please follow the functional requirements to achieve each of the functions

tu= (' Alex ', ' Eric ', ' Rain ')

    • Computes the tuple length and outputs
    • Gets the 2nd element of a tuple and outputs
    • Gets the 第1-2个 element of the tuple and outputs
    • Please use the for output tuple element
    • Use the index of the for, Len, and range output tuples
    • Use the Enumrate output primitive element and ordinal (ordinal starting from 10)

Tu= ('Alex','Eric','Rain')#computes the tuple length and outputsPrint(Len (TU))#gets the 2nd element of a tuple and outputsPrint(tu[1])#gets the 第1-2个 element of the tuple and outputsPrint(Tu[1:3])#please use the for output tuple element forIinchTu:Print(i)#Use the index of the for, Len, and range output tuples forIinchrange (len (TU)):Print(i)#Use the enumrate output primitive element and ordinal (ordinal starting from 10) forIndex,iinchEnumerate (tu,10):    Print(index,i)

6, there are the following variables, please implement the required functions

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

    • Describe the characteristics of Ganso
    • Can the first element in the TU variable "alex" be modified?
    • What type is the value of "K2" in the TU variable? Is it possible to be modified? If you can, add an element, "Seven", to the
    • What type is the value of "K3" in the TU variable? Is it possible to be modified? If you can, add an element, "Seven", to the

7. Dictionaries

DiC = {' K1 ': "v1", "K2": "V2", "K3": [11,22,33]}

    • Please loop out all the keys
    • Please loop out all of the value
    • Please loop out all the keys and value
    • Please add a key-value pair in the dictionary, "K4": "V4", and output the added dictionary
    • In the modified dictionary, the value of "K1" corresponds to "Alex", Output the modified dictionary
    • Please append an element 44 to the value of the K3 to output the modified dictionary
    • Please insert element 18 in the 1th position of the K3 corresponding value, output the modified dictionary

8. Conversion

    • Convert string s = "Alex" to list
    • Convert string s = "Alex" to Cheng Yuanju
    • Convert list li = ["Alex", "seven"] to Narimoto Group
    • Convert meta-progenitor tu = (' Alex ', ' seven ') to a list
    • Converts the list li = ["Alex", "seven"] to a dictionary and the dictionary key increments backwards by 10

9. Element classification

There is a value set [11,22,33,44,55,66,77,88,99,90] that holds all values greater than 66 to the first key in the dictionary, and a value less than 66 to the value of the second key.

That is: {' K1 ': All values greater than 66, ' K2 ': All values less than 66}

10, the output product list, the user enters the serial number, displays the user to select the product

Product Li = ["Mobile phone", "Computer", "mouse pad", ' yacht ')

    • Allow users to add items
    • User input serial number display content

11, the user interactive display similar provinces and counties N-Level linkage choice

    • Allow users to add content
    • Allow users to choose to view a level of content

12. Enumerate all values for which the Boolean value is False

3. There are two lists

L1 = [11,22,33]

L2 = [22,33,44]

    • Get a list of elements of the same content
    • Gets a list of elements in L1 that are not in the L2
    • Gets a list of elements in L2 that are not in the L3
    • Get elements with different contents in L1 and L2

14. Use for loop and range output

    • For loop from large to small output 1-100
    • For loop from small to output 100-1
    • While loop from large to small output 1-100
    • While loop from small to output 100-1

15. Use for loop and range output 9 * 9 multiplication table

 for  in range (1,10):    is in range (1,i+1):        print(str (i) + " * "+str (j) +"="+str (i*j), end=")     Print()

16, for the prime number within 100 and. (Programming Questions)

17. [1,3,2,7,6,23,41,24,33,85,56] from small to large (bubbling method) (programming)

python-Basic Exercise 2

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.