2016/12/30_python

Source: Internet
Author: User

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) operation of the dictionary

Print (dicts["name"]) #取出 value corresponding to "name" key

dicts["name"] = "June" #对 the value corresponding to the "name" key is modified; If this key is not present, add

Del dicts["name"] #用Python中通用的del方法删除dicts字典的 "name" Key and value

Dicts.pop ("name") #删除

Dicts.popitem () #随机删除

Print (dicts["name"]) #如果键不存在则会报KeyError

Print (Dicts.get ("name")) #效果同上, but will not report Keyerror

Print (name in dicts) #判断 "name" Whether the Dicts dictionary has a "name" key if there is a return true

Print (Dict.items ()) #把dicts字典转换为列表

3) traversing the dictionary

For I in Dicts:for Key,value in Dicts.items ():

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

"' The first traversal dictionary is more efficient than the second traversal dictionary"

2, the use of the collection

1) How to create a collection

set = Set ([1,3,5,7,9]) #创建一个集合

LIST_01 = [2,4,6,8,3] #创建一个列表

list_02 = [2,3,2,6,8,2] #创建一个列表

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

list_02 = Set (list_02) reuslt:{2,3,6,8} #在列表转换为集合时, the duplicate value is removed and the same value is never present in the collection

2) Multi-collection operation

Print (list_01 & list_02) #交集用符号: "&"

Print (list_01 | list_02) #并集用符号: "|"

Print (list_01-list_02) #差集用符号: "-"

Print (list_01 ^ list_02) #对称差集用符号: "^"

Print (List_01.intersection (list_02)) #交集

Print (List_01.union (list_02)) #并集

Print (List_01.difference (list_02)) #差集

Print (List_01.symmetric_difference (list_02)) #对称差集

Print (List_01.issubset (list_02)) #判断list_01是否是list_02的子集

Print (List_01.issuperset (list_02)) #判断list_01是否是list_02的父集

Print (List_01.isdisjoint (list_02)) #判断list_01与list_02是否有相同的值

3) operation of single set

List_01.add (999) #对list_01添加一个数据项

List_01.update ([101,102,103]) #对list_01添加一组数据项

List_01.remove (101) #移除指定项, error if not present

List_01.discard (101) #移除指定项, no error

List_01.pop () #移除随机项

"In" and "not" operations (same dictionary)

3. File Operation 01

1) Process of file operation

Open the file first, then manipulate the file, and then close the file

File = Open ("Demo.txt", "R", encoding= "Utf-8") #第一个参数是指定文件夹, the second parameter is the action to be performed ("R": Read, "W": Write, "A": Append, "B": binary), Encoding specifying the encoding format

data = File.read () print (data) #读取txt文件的全部内容并打印在屏幕上

Print (File.readline ()) #读取txt文件的一行内容打印在屏幕上

File.write ("I love Python") #写入字符到demo. txt file, overwriting the original text if it is not in append mode

File.close () #关闭连接

2) file deletion and modification

Print (File.tell ()) #显示文件内容指针所在的位置

File.seek (0) #设置文件内容指针的位置为0

File.flush () #刷新缓存

4. Progress bar

Import Sys,time

For I in range (50):

Sys.stdout.write ("#")

Sys.stdout.flush ()

Time.sleep (0.1)

2016/12/30_python

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.