Python Data Structure exercises, python Data Structure

Source: Internet
Author: User

Python Data Structure exercises, python Data Structure

1. Specify the list L, such as [2, 5, 3, 8, 10, 1], in ascending order and output.

Code: list = [2, 5, 8, 10, 1]
Print (list)
List. sort ()
Print (list)

2. Specify the string s, for example, '123', and output it in reverse order using slices.

Code: str = '000000'

Print (str [:-1])

3. Specify the dictionary dict, for example, {'A': 1, 'B': '2', 'C': '3'}, and output its key and value respectively. Insert the dictionary {'D': 4} into the dictionary and output the new zid.

Code:

Dict = {"a": "1", "B": "2", "c": "3 "}
Print (dict. keys ())
Print (dict. values ())
Dict. setdefault ("d ")
Dict ["d"] = "4"
Print (dict)

4. Obtain all prime numbers within 100

 

For I in range (2,101 ):
Fg = 0
For t in range (2, (I-1 )):
If (I % t = 0 ):
Fg = 1
If (fg = 0 ):
Print (I)

 

Related Article

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.