Python's third day of study review, set set, file manipulation, functions (common functions, recursion, higher order functions), character encoding and decoding

Source: Internet
Author: User
Tags function definition readline

Ternary operations

Age =# is the simple notation  if Else 25

Set Set

# The collection is unordered and does not repeat, # when the list is repeated, you can directly use Set (list) to set the list, and remove the middle duplicate list = [1,2,3,4,5,5,6,7,8,9,1= set (list)

Run Results: You can find the duplicate in the list is removed, and the type becomes set, and then use the list (set), to the list

Collection operations

#Author:zylongSet1 = Set ([1,2,3,4,5,5,6,7,8,9,1]) Set2= Set ([0,2,4,6,8,10])#intersectionPrint(Set1.intersection (Set2))Print(Set1 &Set2)#and setPrint(Set1.union (Set2))Print(Set1 |Set2)#Difference SetPrint(Set1.difference (Set2))Print(Set1-Set2)#symmetric difference sets, which is the result of the intersection with the setPrint(Set1.symmetric_difference (Set2))Print(Set1 ^Set2)#is a subset, the parent setSet1.issubset (Set2) set1.issuperset (Set2)#DeleteSet1.remove (1)#when the element does not exist The Times wrongSet1.discard (1)#there is no error#IncreaseSet1.add (10)#determine if there isPrint(10inchSet1)#Modifyset1.update (Set2)Print(Set1)

Character encoding and decoding

  

#python internal encoding is Unicode#encode and Decode, that is--convert by a Unicode#If you specify the encoding #-*-coding:gbk-*-in the header of the file, this means that the file is parsed by what encoding, not the content in the file is parsed by what encoding,#the contents of the file are encoded in Unicode and are bytes after encodingString ="Hello"String_to_utf8= String.encode ("Utf-8") STRING_UTF8_TO_GBK= String_to_utf8.decode ("Utf-8"). Encode ("GBK"). Decode ("GBK")#This is the last word to be Unicode.Print(String_to_utf8)Print(STRING_UTF8_TO_GBK)

File operations

#Author:zylong#file additions and deletions change#with this method, with end, the program automatically shuts down the program, which is called the F.close () methodWith open ("Review. Py", mode="R", encoding="Utf-8") as F:"read by row"     forLineinchF:Print(line)#Open ModeF= Open ("Review. Py", mode="R", encoding="Utf-8")#This is read-only mode and cannot be written if the method of calling write error#F1 = open ("AA","W", encoding="Utf-8")#This w method is to create a new file and overwrite the original file if the file existsF1.write ("Hello") F1.close ()#Append to end of filef = open ("AA","a", encoding="Utf-8") F.write ("\ n All right.") F.close ()#This is read-write mode open, this mode is written, is appended to the end of the filef = open ("BB","r+", encoding="Utf-8")Print(F.tell ())Print(F.readline ())#The next line that reads the file pointer prints the result "Hello"Print(F.tell ()) F.seek (3)#This cannot be set to 1, 2 because the file starts with a Chinese, Chinese station three bytes, here is the byte positionPrint(F.readline ())#The result of the printing is "good"F.write ("\naaaa\n") F.close ()#search, that is, using loops, using forLineinchF:#This is high efficiency, this is a line of reading    Pass forLineinchF.readlines ():#This approach is to load the contents of the file into memory, when the file is larger than the efficiency of the relatively low    Pass

Function:

  

#Author:zylong#function definition: Use def keyword, method name, method body#If the return value is not written, Python returns none by defaultdeffun_test1 ():Print("First function")#has a return valuedeffun_test2 ():return0#with Parametersdeffun_test3 (x, y):Print(x)Print(y)#when called, there are two ways of calling#1. Formal parameters [call according to the position of the parameter]FUN_TEST3 ()#2. Actual ParametersFun_test3 (y=2,x=1)#There are default values on function parametersdefFun_test4 (x=4,y=5):    Print(x)Print(y)#When this is called, the parameter can be passed or not, and when the value is not passed, the default value is used .Fun_test4 ()#The parameter must precede the argumentFun_test3 (1,y=2);#indefinite parameters, followed by indeterminate parameters, Python wrapped into a tupledefFUN_TEST5 (x,*AVGs):Print(x)Print(AVGS)#There are two ways of callingFUN_TEST5 (10,11,12,13,14,15) Fun_test5 (10,*[11,12,13,14,15]) Fun_test5 (10,* (11,12,13,14,15))#def fun_test6 (X,**KVAVGS):#print (x)#print (KVAVGS)#This call will be an error, because the first form parameter, has been assigned to X, the following arguments are assigned to x, so error, you can say the name of the argument for a change#This is a dictionary that is passed in the form of an actual argument, and Python is encapsulated into a dictionarydefFun_test6 (a,**Kvavgs):Print(a)Print(Kvavgs) Fun_test6 ("a", x="b", y="C", z="D")#Multiple return Valuesdeffun_test7 ():return0,"a",("b","C"),["D","e","e"],{"F","g"},{"name":"Zhang"," Age": 20}res=fun_test7 ()#This returns the returned result into a tuplePrint(RES)#recursive functions that function themselves to call themselves#recursive functions must have a few three special: 1. There are definite end conditions, 2 each entering a deeper layer, the problem scale is correspondingly reduced (next layer, using the result of the previous layer) 3 recursive efficiency is not high#the remainder of a number that cannot be divisible by 2 in the end#when a recursive function is required to return a value, the place where the call is recursive must return so that the result of the lower layer can be returned to the upperdeffun_recursive (n):ifN% 2! =0:returnNElse:        returnFun_recursive (N/2) n= Fun_recursive (30)Print(n)#The higher order function, the function parameter is a functiondefFun_highorde (x,y,f):Print(f (X) +f (Y)) Fun_highorde (20,6,fun_recursive)


  

Python's third day of study review, set set, file manipulation, functions (common functions, recursion, higher order functions), character encoding and decoding

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.