Python basic grammar (2)


day12 2018.05.07

1. Please implement it with code, use underscore to splice each element of the list into a string, li = ['alex','eric','rain']

li = ['alex','eric','rain']
print("_".join(li))

2. Find the elements in the list, remove spaces from each element, and find all elements starting with a or A and ending with c.

li = ["a lec","ada c","Alex","Tony "," rain"]
tu = ("alec","adad","Alex","Tony","rain")
dic = {"k1":"alec","k2":"adad","k3":"Alex","k4":"Tony","k5":"rain"}


print("Change the previous li: ",li) #Remove
spaces
for item in li:
li[li.index(item)]=item.replace(" ","")
print("Changed li:" ,li)

for item in li:
if (item.startswith("a") or item.startswith("A")) and item.endswith("c"):
print("List li starts with A or a and ends with c: ",item)


3. Write code, have the following list, and implement each function as required

li = ["alex","eric","rain"]
a. Calculate the length of the list and output

print (li .__ len __ ())

b. Append the element "seven" to the list, and output the added list

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

c. Please insert the element "Tony" at the first position of the list, and output the added list

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

d. Please modify the element in the second position of the list to "Kelly", and output the modified list

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

e. Please delete the element "eric" in the list and output the modified list

li.remove("eric")
print(li)

f. Please delete the second element of the list, and output the value of the deleted element and the list after the deleted element

li.pop (1)
print (li)

g. Please delete the 3rd element in the list and output the list after deleting the element

v = li.pop (1)
print (li, v)

h. Please delete the 2nd to 4th elements in the list, and output the list after deleting the elements

del li [1: 4]
print (li)

i. Please reverse all the elements of the list and output the reversed list.

li.reverse ()
print (li)

j. Please use for, len, range to output the index of the list

for item in range(li.__len__()):
print(item,li[item])

k. Please use enumrate to output list elements and serial numbers (the serial numbers start from 100)

for i,item in enumerate(li):
print(i+100,item)


l. Please use for loop to output all elements of the list

for item in li:
print(item)

4. Write code, there is the following list, please implement each function according to the functional requirements
li = ["hello",'seven',["moon",["h","kelly"],'all'],123,446]
a. Please output "Kelly" according to the index

print (li [2] [1] [1])

b. Please use the index to find the 'all' element and modify it to 'ALL'.

li[2][2]="ALL"
print(li)

5. Write the code, there are the following ancestors, and implement each function according to the requirements
tu = ('alex','eric','rain')
a. Calculate the length of the ancestor and output

print (tu .__ len __ ())

b. Get the second element of the tuple and output

print(tu[1])

c. Get the first to second elements of the tuple and output

print(tu[0:2])

d. Please use for to output the elements of the tuple

for item in tu:
print(item)

e. Please use for, len, range to output the index of the tuple

for item in range(tu.__len__()):
print(item)

f. Please use enumrate to output the tuple element and serial number (the serial number starts from 10)

for i,item in enumerate(tu):
print(i,item)

6. There are the following variables, please implement the required function
tu = ("alex",[11,22,{"k1":"v1","k2":["age","name"],"k3": (11,22,33)},44])
a. Describe the characteristics of the ancestor

The first-level elements of the ancestor, cannot be changed.

b. Can the first element "alex" in the tu variable be modified?

can not

c. What type is the value corresponding to "k2" in the tu variable? Can it be modified? If possible, add an element "Seven" to it

"k2" corresponds to a list, which can be modified.

tu[1][2]["k2"].append("Seven")
print("Modified tu",tu)

d. What type of value corresponds to "k3" in the tu variable? Can it be modified? If possible, please add an element "Seven"
"k3" corresponds to the tuple and cannot be modified.

7. Dictionary
dic = {'k1':"v1",'k2':"v2",'k3':[11,22,33]}
a. Please output all keys in a loop

for item in dic:
print(item)

b. Please loop and output all the values

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

c. Please loop and output all keys and values

for item in dic.items():
print(item)

d. Please add a key-value pair to the dictionary, "k4": "v4", and output the added dictionary

dic.update({"k4":"v4"})
print(dic)

dic.update(k4="v4")
print(dic)

e. Please set the value corresponding to "k1" in the modified dictionary to "alex", and output the modified dictionary

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

f. Please append an element 44 to the value corresponding to k3, and output the modified dictionary

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

g. Please insert element 18 in the first position of the value corresponding to k3, and output the modified dictionary

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

8. Convert
a. Convert the string s="alex" to a list

s = list(s)
print(s)

b. Convert the string s="alex" to a tuple

s = tuple(s)
print(s)

c. Convert the ancestor tu=('Alex', "seven") into a list

l = list(you)
print(l)

d. Convert the list li=["alex", "seven"] into a dictionary and the key of the dictionary starts to increment backward according to 10

li=["alex","seven"]
dic = {}
print(dic)
for i in range(10,10+li.__len__()):
dic[i] = li[i-10]
print(dic)


9. The element classification
has the following value sets [11, 22, 33, 44, 55, 66, 77, 88, 99, 90], save all values ​​greater than 66 to the first key of the dictionary, and store all values ​​less than 66 in the first key of the dictionary. The value is stored in the value of the second key.

li = [11,22,33,44,55,66,77,88,99,90]
dic = {"k1":[],"k2":[]}
print(dic)
for item in li:
if item > 66:
dic["k1"].append(item)
elif item < 16:
dic["k2"].append(item)
print(dic)


10. Output the product list, the user enters the serial number, and displays the product selected by the user.
Product li=["mobile phone", "computer", "mouse pad", "yacht"]
a. Allow the user to add products
b. The user enters the serial number to display the content

dic ={"1":"Mobile phone","2":"Computer","3":"Mouse pad","4":"Yacht"}
print(dic)
k = input("Please enter the product serial number:" )
print(dic[k])
goods = input("Please enter the product name:")
dic.update({str(dic.__len__()):goods})
print(dic)


11. User interaction displays similar provinces, cities and counties Choice of N-level linkage
a. Allow users to add content
b. User input serial number to display content



12. List all values ​​whose boolean value is False

None,0,"",[],{},()

13. There are two lists
l1=[11,22,33]
l2=[22,33,44]
a. Get a list of elements with the same content


b. Get a list of elements in l1 but not in l2
c. Get a list of elements in l2 Yes, the list of elements not in l1
d. Get elements with different contents in l1 and l2

14, use for loop and range to output a. for
loop output 1-100
from big to small b.for loop from small to big output 100- 1
c.while loop outputs 1-100
from large to small d.while loop outputs 100-1 from small to large

15. Shopping cart
Functional requirements:
require the user to input the total assets, for example: 2000
to display the list of products, let the user select the product according to the serial number , add to the shopping cart
to purchase, if the total amount of goods is greater than the total assets, it will prompt the account balance is insufficient, otherwise, the purchase is successful.

goods=[
{"name":"computer","price":1999},
{"name":"mouse","price":10},
{"name":"yacht","price":20} ,
{"name":"beauty","price":998},
]


16. Paging display content
a. Create 301 pieces of data through the for loop, the data type is not limited, such as:
alex-1 [email protected] pwd1
alex-2 [email protected] pwd2
alex-3 alex-3 @live.com pwd3

b. Prompt the user to enter the page number to be viewed. When the user enters the specified page number, the specified data will be displayed.
Note:
10 pieces of data are displayed on each page
.


17. There are 1, 2, 3, 4, 5, 6, 7, 8, 9 numbers, how many two-digit numbers can be formed that are different from each other and have no repeating numbers

18. Use for loop and range to output 9*9 multiplication table

19. There is a list, nums=[2,7,11,15,1,8,7]
Please find the set of elements that can be equal to 9 by adding any two elements in the list,

20. Use Python to develop a program to automatically calculate the plan:
5 cents for a rooster, 3 cents for a hen, 3 cents for a chick, and 100 cents to buy 100 chickens, including roosters, hens, and chicks. Must have, may I ask how many roosters, hens, and chicks should I buy to make up for 100 cents?

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325700448&siteId=291194637