Python Learning Path _python Basics (3)

Source: Internet
Author: User

Learning content:

1. Python Collection

2. Python file operation

3. Python character encoding

4. Introduction to Python functions

One, Python collection 1) Creation of the collection:
List_1 = [1,3,5,7,26,8,65]
List_1=set (list_1)

List_2 =set ([2,4,5,6,7,9,65])

Print (list_1,list_2)
2) operation between the assembly:
Print ("Intersection:", List_1.intersection (list_2)) #交集
Print ("Set", List_1.union (list_2)) #并集
Print ("Difference set:", List_1.difference (list_2)) #差集 in List_1 and in list_2
Print (List_2.difference (list_1))
List_3 = Set ([5,7])
Print (List_3.issubset (list_2)) #判断是否是子集
Print (List_2.issuperset (list_3)) # #判断是否是父集

Print (List_1.symmetric_difference (list_2)) #对称差集, take list_1 and list_2 each other did not remove, will be removed.
List_4 = Set ([98,97])
Print (List_1.isdisjoint (list_4)) #交集为空 returns true; False otherwise
List_1.add (999)  #添加一项
List_1.update ([888,777,666]) #添加多项
#list_1. Remove (' H ') #删除
Print ("HHHH:", List_1.discard (' O ')) #删除字, if deleted, if there is nothing to do
#x in a# Test if X is a member
#x not a member of #测试x是否不在 a
Second, Python file operation

1) Write the file (W):

Creates a new file and writes the contents into the file, overwriting the file if any.

F=open (' Yesterday2 ', ' W ', encoding= ' utf-8 ')
F.write ("Wo ye bu zhi dao ni zai shuo shen me!")

2) Read the file (R):

F=open (' Yesterday2 ', ' R ', encoding= ' utf-8 ')
Print ( ()) #全部读出
Print (F.readline ()) #只读一行
Print (F.readlines ()) #读出剩余的行

2) append (a) to the file:

F=open (' Yesterday2 ', ' a ', encoding= "Utf-8") #文件句柄 to optimize the contents of the file to the back of the file

3) file Traversal:

#要把内容全部读出来, wasting memory, slow
For Index,line in Enumerate (F.readlines ()):
If Index ==9:
Priint ("---------------I am a split line---------------")
Print (Line.strip ())
For line in F:
If Count ==9:
Print ("-------------Split Line------------")
Print (Line.strip ())
Print (Line.strip ())

4) Modification of the file:

f = open (' Yesterday2 ', ' R ', encoding= ' utf-8 ')
F_bak = open (' Yesterday2_bak ', ' W ', encoding= ' utf-8 ')
For line in F:
If "When I am young and frivolous" in line:
Line=line.replace (' When I was young and frivolous ', ' Young and frivolous ')
F_bak.write (line)
F.close ()
F_bak.close ()

5) Other actions of the file:

Print (f.encoding) (0) #处理光标的位置
Print (F.fileno ()) #系统处理文件的编号
Print (F.flush ()) #等到缓存满了 swipe into memory together
Print (f.buffer) #刷新缓存
F.truncate (#不写就是清空)  , truncate from the beginning
F=open (' Yesterday ', ' r+ ', encoding= ' utf-8 ') #可以先读后写------Read-write mode
F=open (' Yesterday ', ' w+ ', encoding= ' utf-8 ') #可以先写后读-------Read-write mode
F=open (' Yesterday ', ' A + ', encoding= "Utf-8") #追加读写
F=open (' Yesterday ', ' RB ') #读取二进制文件 can only be used in binary format during the network transmission of socked; Download file binaries
F=open (' Yesterday ', ' WB ')   #二进制的写
F.write ("Hello, binary\n". Encode ())   # bytes turn into binary format

C. Python character encoding

Unicode is a universal code, and all other character encodings are converted to Unicode, "string". Decode ("one"). Encode ("another"):

The file decodes (decode) into Unicode and tells Unicode my current encoding format, and then encodes (encode) the encoding format of the program that will be used to change the file

Iv. description of Python functions

1) Definition of function:

def test1 ():
Print ("The funtion")
def test1 (x):  #带参数
"The Funtion"
X +=1
return x
def sum (A,b,*args): #*args receives n positional parameters, converts Narimoto group
Print (a)
Print (b)
Print (args)
def sun1 (**kwargs): #**kwargs receives n keyword arguments, converts to dictionaries
Print (Kwargs)
Print (kwargs[' name '])

2) Recursion of the function:

#执行效率低, recursion causes the stack overflow function to be called through the computer's stack
# def Calc (m):
# print (m)
# If m<100:
# Return Calc (m+1)
# Else:
# Pass

3) Higher order functions:

#变量可以指向函数, a function parameter can accept a variable, and a function can accept another function as a parameter, a function called a higher order function.
def add (a,b,f):
Return f (a) +f (b)

Res=add (3,-6,abs)
Print (RES)

Python Learning Path _python Basics (3)

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: 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.