"23" Python basic Note 2

Source: Internet
Author: User

1. Use the code to do this: use underscores to stitch each element of a list into a string

li=[‘alex‘, ‘eric‘, ‘rain‘]print("_".join(li))print(li[0]+"_"+li[1]+"_"+li[1])

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

li=[' Alex ', ' Eric ', ' rain '
? calculate the length of the list and output
print(len(li))
The list appends the element "seven" and outputs the added list

li.append("seven")print(li)

? Please insert the element "Tony" in the 1th position of the list and output the added list

li.insert(1,"Tony")print(li)

? Please modify the element in the 2nd position of the list to "Kelly" and output the modified list

li[1]="kelly"print(li)

? Remove the Element "Eric" from the list and output the modified list

li.remove("eric")li.pop(1)del li[1]print(li)

? Delete the 2nd element in the list and output the value of the deleted element and the list after the element is deleted

for i in li:    if i == li[2]:        li.pop(2)        print(i)        print(li)

? Delete the 2nd to 4th element in the list and output the list after the delete element

li=[‘alex‘, ‘eric‘, ‘rain‘,‘ls‘]print(li[2:4])del li[2:4]print(li)

? Please invert all the elements in the list and output the inverted list

print("反转前:",li)li.reverse()print("reverse后",li)

? Use the index of the for, Len, range output list

for i in range(len(li)):    print(i)

? Use the Enumrate output list element and ordinal (ordinal starting from 100)

for x,y in enumerate(li):    print(x+100,y)

? Use the For loop to output all the elements of the list

for i in li:    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.
print(li[2][1][1])
? Please use the index to find the ' all ' element and modify it to "all", such as: li[0][1][9] ...
print(li[2][2].upper())

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

long=len(tu)print(long)

Gets the 2nd element of a tuple and outputs
print(tu[1])
Gets the 第1-2个 element of the tuple and outputs
print(tu[:2])
Please use the for output tuple element

for i in tu:    print(i)

Use the index of the for, Len, and range output tuples

for i in range(len(tu)):    print(i)

Use the Enumrate output primitive element and ordinal (ordinal starting from 10)

for i,into in enumerate(tu):    print(i+10,into)

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])
Tell me about Ganso features
答:不可变,有序。元组内第一层元素不可变(如tu元组中的alex,k3,44)。第二层及以上可以通过深copy浅copy修改
Can you tell me if the first element in the TU variable, "Alex", could be modified?
答:不能被修改
What type of value is the "K2" in the TU variable? Is it possible to be modified? If you can, add an element, "Seven", to the

答:list,可修改li=tu[1][2]["k2"]li.append("Seven")print(tu)

What type of value is the "K3" in the TU variable? Is it possible to be modified? If you can, add an element, "Seven", to the
答:tu变量中的k3对于的是tuple元组,不可以修改

7. Dictionaries
DiC = {' K1 ': "v1", "K2": "V2", "K3": [11,22,33]}
? Please loop out all the keys

for i in dic.keys():    print(i)

? Please loop out all the value

for i in dic.values():    print(i)

? Please loop out all the keys and the value

for k,v in dic.items():    print(k,v)

? Please add a key-value pair in the dictionary, "K4": "V4", and output the added dictionary

dic["k4"]="v4"print(dic)dic.setdefault("k4","v4")print(dic)

? In the modified dictionary, the value of "K1" corresponds to "Alex", Output the modified dictionary

dic["k1"]="alex"print(dic)

? Please append an element 44 to the value of the K3, and output the modified dictionary

dic["k3"].append(44)print(dic)

? Please insert element 18 in the 1th position of the corresponding value of K3, output the modified dictionary

dic["k3"].insert(0,18)print(dic)

8. Conversion
Convert the string s = "Alex" into a list
print(list(s))
Convert the string s = "Alex" to Cheng Yuanju
print(tuple(s))
? Convert list li = ["Alex", "seven"] to Narimoto Group

li = ["alex", "seven"]print(tuple(li))

Convert meta-progenitor tu = (' Alex ', ' seven ') to a list

tu = (‘Alex‘, "seven")print(list(tu))

Convert list li = ["Alex", "seven"] to a dictionary and the dictionary key is incremented by 10

li = ["alex", "seven"]lis=[10,11]print(dict(zip(lis,li)))

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}

li=[11,22,33,44,55,66,77,88,99,90]max66=[]mix66=[]for i in li:    if i >66:        mix66.append(i)    else:        max66.append(i)dic={}dic["k1"]=mix66dic["k2"]=max66print(dic)

13. There are two lists

x = [11,22,33]
y = [22,33,44]
L=[]
Get a list of elements of the same content

for i in x:    if i in y:        l.append(i)print(l)

Get a list of elements in L1 that are not in the L2

for i in x:    if i in y:        pass    else:        l.append(i)print(l)

Get 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

for i in range(100):    print(i)

? While loop from large to small output 1-100

? While loop from small to output 100-1

count=0while count<100:    if  count<=100:        print(count)    else:        break    count +=1

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

for n in range(1,10):    for m in range(1,10):        print("%s * %s = %s" %(n,m,n*m))    print("")

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 '}

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

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)

"23" Python basic Note 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.