Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the python exercises?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the python exercises". In the daily operation, I believe many people have doubts about the python exercises. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the python exercises?" Next, please follow the editor to study!

1. Supplement of basic knowledge points

Join () converts a list to a string

Split () cut. Turn a string into a list.

Delete the question.

List and dict

List deletes things that cannot be deleted directly during the loop

Do not change the size of the dict during the loop.

You need to save the content to be deleted in a list. Loop this list to delete the old list.

Fromkeys (a, b) gets each element in an and assembles b into a new dictionary to return.

2. Set collection: no repetition, disorder, content must be hashable (immutable).

3. Deep and shallow copy

1. Assignment: no new object is created. Two variables point to the same object

two。 Shallow copy: copy the first layer of content. Created a new object [:] copy ()

3. Deep copy: copies all the contents associated with the current object. Deepcopy ().

Exercises

1. To determine whether a number is a daffodil number, the daffodil number is a three-digit number, and the sum of the third power of each of the three digits is equal to this number. Then this number is a number of daffodils, for example: 153 "1" 3 "5" 3 "3" 3.

Method 1:

# method 1

While 1:

A = int (input ("enter number"))

If len (str (a)) = = 3:

If (a / 100) * * 3 + (a% 100 / / 10) * * 3 + (a% 10) * * 3 = = a:

Print ("d is number of daffodils" a)

Else:

Print ("not daffodils")

Else:

Print ("please re-enter a three-digit number")

Continue

Method two

Str = input ("Please enter a number")

S = 0

For c in str:

S + = int (c) * * 3

If s = = int (str):

Print ("number of daffodils")

Else:

Print ("not daffodils")

two。 Complete the lottery 36 select 7 function. Randomly generate 7 numbers from 1 to 36, and finally get 7 non-repetitive data as the final lottery result.

Random number:

From random import

Random number of randint randint (0meme20) # 0-20

# generate random numbers without repetition

From random import randint

B = set ()

While len (b)

< 7: a = randint(1, 36) b.add(a) print(b) 3.给出一个纯数字列表.请对列表进行排序(升级题)… 思路: 完成a和b的数据交换。例如,a=10,b=24交换之后,a=24,b=10循环列表。判断a]和ali+1]之间的大小关系,如果al比ali+1]大.则进行互换。 循环结束的时候.当前列表中最大的数据就会被移动到最右端. 想一想,如果再次执行一次上面的操作。最终第二大的数据就移动到了右端.以此类推.如果反复的进行执行相应的操作,那这个列表就变成了一个有序列表! 方法一:冒泡排序法 ls = [1,2,34,14,43,32,3,2,3,42,424,24,41,4] count = 0 while count < len(ls): i = 0 while i < len(ls) -1-count: #优化了,减少不必要的循环 if ls[i] >

Ls [iTun1]:

Ls [I], ls [I] = ls [I], ls [I]

I + = 1

Count + = 1

Print (ls)

Method two

S = set ()

While 1:

A = input ("enter a number")

If a ='QQ:

Break

Else:

S.add (int (a))

Lst = list (s) # create a list without repetition

Print (lst)

For i in range (len (lst)-1):

For j in range (iMag1Llen (lst)):

If lst [I] > lst [j]:

Lst [I], lst [j] = lst [j], lst [I]

Print (lst) at this point, the study of "what are the python exercises" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report