Python learning - Beginners Introduction 3

A collection of

1. unordered collection is not repeated

2. set operations:

(1) to the intersection list

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Authon:Jerry Z

list_1 = set([1,3,4,5,6])
list_2 = set([1,5,11,23])

print("并集")
print(list_2.union(list_1))
print(list_1 | list_2)

print("交集")
print(list_1.intersection(list_2))
print(list_2 & list_1)

print("差集") # in list1 not in list2
print(list_2.difference(list_1))
print(list_2 - list_1)
print(list_1.difference(list_2))
print(list_1 - list_2)


print("对称差集")
print(list_2.symmetric_difference(list_1))
print(list_1 ^ list_2)

print("子集")
list_3 = set([3,6])
print(list_3.issubset(list_1))
print("父集")
Print (list_1.issuperset (list_3)) 

Print ( "Is there a way intersection, otherwise there is no intersection back True False") 
Print (list_2.isdisjoint (list_3)) 
Print (list_3.isdisjoint (list_1)) 

for the X-in list_1 : 
    Print (the X-)

print ( "Basic Operation") 
print ( "Add")
list_4 = SET ([9,1,2,3,4])
list_4.add (. 5) to add a #
Print (list_4)

list_4.update ([. 6, 7,8]) # add multiple
Print (list_4)

Print ( "you can use the remove deleted item, this error does not exist")
list_4.remove (8)
Print (list_4)

Print ( "len (S) length" )
Print ( "x in s test x is not in s, x may be a list of characters tuples, etc.")

Print ( "POP delete any one")
list_4.pop ()
Print (list_4)

 

Two file operations

1. Process file operations: Open -> Operation -> Close

2. The basic file operations:

print ( "File Basic Operation") 
# Data = Open ( "Yesterday", encoding = "UTF-. 8"). Read () 
# # Data = Open ( "Yesterday"). Read () 
# Print (Data) 

F = open ( "yesterday", "r ", encoding = "utf-8") # file handle 
data = f.read () # first pass read 
data2 = f.read () # is read from the first pass where place the end of the start reading. So, after reading the first pass and then read the file no content of 
Print (the Data) 
Print ( "% S --- --------- ---- data2"% data2) 
f.close () 

print ( "write") 
F2 = Open ( "yesterday2", "A", encoding = "UTF-8") # file handle 
f2.write ( "I love Beijing Tiananmen \ the n-") 
f2.write ( "Tiananmen Square on the sun l \ n-") 
# = f2.read DATA3 () can not read # 
# Print (" ------- DATA3: ", DATA3) 
f2.close () 


Print ("
 
F3 = Open ( "Yesterday", "R & lt", encoding = "UTF-. 8") 
# for index, in the enumerate Line (f3.readlines ()): 
# == IF index. 9: 
# Print ( "- ----------------- not print ---------------- ") 
# the Continue 
# Print (line.strip ()) 
'' ' 
enumerate defect is read into memory first began circulating, read into memory takes time to wait, such as files too large than memory 
so enumerate only suitable for handling small files 
' '' 

# Print ( "---------- - ******* ------------ ") 
# = List1 f3.readlines () 
# for index in the Range (len (List1)): 
# 9 == IF index: 
# print ( "------------------ not print ----------------") 
# the Continue 
# Print (List1 [index] .Strip ()) 
# 

Print ( "file for each method of efficiently take line") 
Print ( "----------------------- ----------- ******* ")
= 0 COUNT 
Print ( "flush refresh disk")
for line in f3:
    COUNT ==. 9 IF: 
        Print ( "not print ------------------ ----------------") 
        COUNT + = . 1 
        Continue 
    Print (line.strip ()) 
    COUNT = +. 1 
f3.close () 


F4 = Open ( "Yesterday", "R & lt", encoding = "UTF-. 8") 
Print ( "Tell return when read file pointer , Print position ") 
Print (f4.readline ()) 
Print (f4.tell ()) 
Print (" Seek specified position pointer is set ") 
f4.seek (. 4) 
Print (f4.readline ()) 

Print (" printing encoding format ") 
Print (f4.encoding) 
Print (" interface number fileno document processing system ") 
Print (f4.fileno ()) 

Print (" whether Seekable move the pointer ") 
Print (f4.seekable ()) 

Print ( "readable file is readable") 
Print (F4.

Print (f4.flush ()) 


F5 = Open ( "yesterday3", "A", encoding = "UTF-. 8") 
f5.seek (. 5) 
# f5.truncate (10) taken # later deleted from the beginning of the file , seek failure 

print ( "file modified ******** ********") 
print ( "R & lt +: read-write") 
F6 = Open ( "yesterday3", "R & lt +", encoding = "UTF-. 8") 
Print (f6.readline ()) 
Print (f6.readline ()) 
Print (f6.readline ()) 
f6.write ( "\ NR + write operation \ n-") 


Print ( "+ W: write read ") 
F7 = Open (" yesterday4 "," W + ", encoding =" UTF-. 8 ") 
f7.write (" AAAA write read \ n-") 
f7.write (" AAAA write read \ n-") 
F7 .write ( "aaaa read write operation \ the n-") 

Print (f7.tell ()) 
f7.seek (10) 
Print (f7.readline())
f7.write("aaaaaaaaaaaaa\n")
f7.close()

print("a+:追加读写")
print ( "rb: binary format file reading when used: a network transmission socket like python3 only can also open the corresponding file binary python2 by character binary file manipulation corresponding to the operator.") 
F8 = Open ( " yesterday4 "," RB ") 
Print (f8.readline ()) 
Print (f8.readline ()) 
Print (f8.readline ()) 

F9 = Open (" yesterday5 "," WB ") 
f9.write (" the Hello binary ".encode ()) 


Print (" edit file ") 
" "" 
. 1 Vim 
2 to re-read the file into the new file 
"" " 


F10 = Open (" Yesterday "," R & lt ", encoding =" UTF-. 8 ") 
F11 = Open (" yeterday6 "," w ", encoding =" UTF-8 ") 

for Line in F10: 
    IF" silly game "in Line: 
        # Line =" I enjoy them as happy life of the game \ n "
        line = line.replace ( "I tease them as silly game of life", "I enjoy them as happy life of the game") 
    f11.write (Line) 

f10.close () 
f11.close ()



print ( "with data, can be avoided to forget to close the file, automatically closed forget") 
 
with Open ( "yesterday7", "W", encoding = "UTF-. 8") AS F1, \
        Open ( "yesterday8", "W", encoding = "UTF-. 8") AS F2: 
    f1.write ( "dddd") 
    f2.write ( "dddd")

 

Guess you like

Origin www.cnblogs.com/zhenhua37/p/11454949.html