2016/12/30 _ Python, 20161230_python

Source: Internet
Author: User

2016/12/30 _ Python, 20161230_python

Today's main learning content:

Python:

1. Use of dictionaries

1) how to create a dictionary

Dicts = {"name": "juncx", "age": 17}

Dicts = dict ("name: juncx", "age: 17 ")

2) dictionary operations

Print (dicts ["name"]) # obtain the value corresponding to the "name" Key

Dicts ["name"] = "jun" # modify the value corresponding to the "name" key. If this key does not exist, add

Del dicts ["name"] # Delete the "name" key and value of the dicts dictionary using the del method in Python

Dicts. pop ("name") # Delete

Dicts. popitem () # Delete randomly

Print (dicts ["name"]) # If the key does not exist, a KeyError is reported.

Print (dicts. get ("name") # The same effect as above, but no KeyError is reported.

Print (name in dicts) # judge whether "name" indicates whether the dicts dictionary has a "name" key. If yes, return True.

Print (dict. items () # convert dicts dictionary to list

3) traverse the dictionary

For I in dicts: for key, value in dicts. items ():

Print (I, dicts [I]) print (key, value)

'''The first dictionary is more efficient than the second one '''

2. Use of Collections

1) How to Create a set

Set = set ([, 9]) # create a set

List_01 = [2, 4, 6, 8, 3] # create a list

List_02 = [2, 3, 2, 6, 8, 2] # create a list

List_01 = set (list_01) result: {2, 4, 6, 8, 3}

List_02 = set (list_02) reuslt: {2, 3, 6, 8} # duplicate values will be removed when the list is converted to a set. The same value will never appear in the set.

2) Multi-Set Operations

Print (list_01 & list_02) # intersection Symbol :"&"

Print (list_01 | list_02) # Union symbol: "|"

Print (list_01-list_02) # symbol of the difference set :"-"

Print (list_01 ^ list_02) # symbol of the symmetric difference set: "^"

Print (list_01.intersection (list_02) # intersection

Print (list_01.union (list_02) # Union

Print (list_01.difference (list_02) # difference set

Print (list_01.symmetric_difference (list_02) # symmetric difference set

Print (list_01.issubset (list_02) # judge whether list_01 is a subset of list_02

Print (list_01.issuperset (list_02) # judge whether list_01 is the parent set of list_02

Print (list_01.isdisjoint (list_02) # determine whether list_01 and list_02 have the same value

3) Single Set Operations

List_01.add (999) # Add a data item to list_01

List_01.update ([101,102,103]) # Add a set of data items to list_01

List_01.remove (101) # Remove a specified item. If it does not exist, an error is returned.

List_01.discard (101) # Remove a specified item and no error is reported

List_01.pop () # Remove random items

''' And "in" and "not in" operations (same dictionary )'''

3. File Operation 01

1) File Operation Process

First open the file, then operate the file, and then close the file

File = open ("demo.txt", "r", encoding = "UTF-8") # The first parameter is the specified folder, and the second parameter is the operation to be performed ("r ": read, "w": write, "a": append, "B": binary file), encoding specifies the encoding format

Data = file. read () print (data) # read all contents of the txt file and print it on the screen

Print (file. readline () # Read a line of txt file and print it on the screen.

File. write ("I love Python") into the demo.txt file. If it is not in append mode, the original text will be overwritten.

File. close () # close the connection

2) add, delete, modify, and Query files

Print (file. tell () # display the position of the file content pointer

File. seek (0) # Set the file content pointer to 0

File. flush () # refresh the cache

4. progress bar

Import sys, time

For I in range (50 ):

Sys. stdout. write ("#")

Sys. stdout. flush ()

Time. sleep (0.1)

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.