While loop exercise 23

Source: Internet
Author: User

Practice one, displaying all the elements in the specified list, individually;

Method one in [+]: l1out[25]: [1, 2, 3, 4]in []: while L1: ....: Print L1[0] ....: L1.pop (0) ....: 1234//method Two in [countout[67]: Count = 0In []:]: 0In [L1[count]: While Count < Len (L1): ....: Print] ....: Coun T + = 1 .....: 1234

Exercise two, ask for the sum of all even numbers within 100;

In [215]: sum = 0;         i = 0In [216]: While I <=: I+=1 .....: If I% 2 = = 0: ....: sum = Sum +i ....: In [217]: Print sum2550

Exercise three, display all the values of the specified dictionary one by one, and indicate the total number of keys after the results are displayed;

In [the]: print d1{' y ':, ' X ': 2, ' z ': 90}in [+]: while Keylist: ....: Print Keylist[0] ....: Keylist.pop (0) .....: Else: ....: Print len (D1) ....: yxz3

Exercise four, create a list containing all the odd numbers within 100;

In [max]: L1 = []in]: x = 1In [98]: While X <: ....: L1.append (x) ....: x + = 2 ....: in [99]: Print L1[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59 , 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

Exercise five, which shows all the elements of a list in reverse order;

In [the]: L1 = [1,2,3,4]in]: while L1: ....: Print L1[-1] ....: L1.pop () ....: 4321

Exercise six, List l1=[0,1,2,3,4,5,6], list l2=["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", with the elements in the first list as keys, and the elements in the second list as values to generate a dictionary D1;

in [+]: d1={}in [121]: x=0in [122]: L1=[0,1,2,3,4,5,6]in [123]: l2=["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" in [130 ]: While x < Len (L1): d1[l1[x]] = l2[x] x + = 1 ....: in [131]: Print d1{0: ' Sun ', 1: ' Mon ', 2: ' Tue ', 3: ' Wed ', 4: ' Thu ', 5: ' Fri ', 6: ' Sat '}


This article is from "Jessen Liu's blog," Please make sure to keep this source http://zkhylt.blog.51cto.com/3638719/1708089

While loop exercise 23

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.