Python Learning second article

Source: Internet
Author: User

1:3-dollar operation
1 if Else 2

The ternary operation first determines if the statement after the if is established, like the above code if A>0 is the result = 1, not the result = 2.

Two: string and bytes convert each other
1 " My name is Lei Feng " 2 Print (name) 3 Print (Name.encode (encoding= "Utf-8")) # convert Name "My name is Lei Feng" to bytes 4 Print (b'\xe6\x88\x91\xe7\x9a\x84\xe5\x90\x8d\xe5\xad\x97\xe5\x8f\xab\xe9\x9b\xb7\xe9\x94\x8b '. Decode ())

Convert string to bytes first (default = Utf-8)

Then decode the bytes into a string

Three: List

1. List definition and output

1name = ["Don Sanzang","Monkey King","Pig","Sha Monk","White Dragon Horse"]2 Print(name)#output all the data in name3 Print(Name[0])#Output Name This list is labeled 0 data, "Don Sanzang"4 Print(Name[1:3])#Output name Subscript 1-3 does not include 3 of the data that is [' Monkey King ', ' Pig '] (Gu Tou regardless of the tail)5 Print(Name[-1])#Output Last data6 Print(name[-2:])#output last two values7 Print(Name[-2:-1])#Output Countdown Second value (Gu Tou regardless of tail)
8 print (Name[0:-1:2]) #从下标为0的数据输出致最后一个 with a step of 2 (three values can be defaulted)

2.: List modification

name = ["Don Sanzang","Monkey King","Pig","Sha Monk","White Dragon Horse"]name.append ("Watch the sound of the world")#Append a valueName.insert (1,"Six-eared Macaque")#position the six-eared macaque in the list labeled 1NAME[4] ="General Roller Blinds"#change the position of subscript 4 to "General roller shutter"Name.remove ("Monkey King")#Delete the Monkey King in the listdelNAME[1]#Remove the data that is labeled 1 in the listA = Name.pop (1)#Delete and return the data labeled 1b = Name.index ("White Dragon Horse")#Locate and return the first value for the subscript of the White Dragon horsec = Name.count ("Don Sanzang")#The number of values in the statistics list is "Don Sanzang" DataPrint(Name,a,b,c) name.clear ()#emptying data in a listPrint(name)

3. List merging

num = [1,2,3,4= [5,6,7,8]num.extend (num2)# adds data from num2 to numprint(num, NUM2)

4. List replication

1name = [0,1,2,[3.1,3.2],4,5,[6.1,6.2]]2name2 = Name.copy ()#Copy the data stored in name to Name23 Print(Name,'\ n', name2)4 Print('-------------------------------')5Name[0] = 106NAME[6] = 607Name[3][0] = 308 Print(Name,'\ n', name2)

Results such as

You can see that name2 copied the name data (that is, the name change does not affect name2)

The list can also be nested, but the first-level list of columns holds the pointer to the bottom-level list where it is stored, not the data itself

The name2 copy is also the pointer stored in name, so to Name[3] and name2[3] are stored in the pointer to [3.1,3.2] This list, change name[3][0] make changes by the pointer to the child list changes

So the output data of the name2 will also be affected.

NAME[6]=60 is to change the pointer in name[6] to 60, the sub-list is unaffected, and the name2 output is unaffected.

Quad: tuples

Can not be modified only, also known as read-only list

Tuple definitions:

num = (1,2,3,4)
Five: String common functions

1.

Name ='My name is \tjack'Print(Name.capitalize ())#Capitalize first letterPrint(Name.count ('a'))#number of a in the statistics stringPrint(Name.center (10,'+'))#Print 10 characters put the value of name in the middle, not enough +Print(Name.endswith ('ck'))#determine if a string is terminated by CKPrint(Name.expandtabs (20))#convert a \ t to 20 spacesPrint(Name.find ('name'))#returns the location where name is not found return-1Print(Name[1:6])#slice output, element of output 1-6Information ='{name1} is {age1} years old'Print(Information.format (name1 ='Jack', Age1 = 20))Print(Information.format_map ({'name1':'Jack','Age1':' -'}))#Using DictionariesPrint(Name.index ('name'))#returned the name where the throw exception was not found

The results are as follows

2.

Print('AA'. Isalpha ())#Judging is not a plain English characterPrint('+'. Join (['1','2','3','4']))#Change the list to a string, with + as a spacerPrint('ABCD'. Ljust (50,'+'))#50 lengths less than the right side with + as a placeholderPrint('ABCD'. Rjust (50,'-'))#50 lengths not enough for left side-as placeholdersPrint('Jack'. Lower ())#return lowercasePrint('Jack'. Upper ())#return uppercaseP=str.maketrans ('ABCDEFG','[Email protected]/*+5%')#two string one by one correspondence, specifying character conversion rulesPrint('Fog'. Translate (p))#' fog ' is encrypted according to the P rulePrint('Jack Jone'. replace ('J','J', 2))#Change J in string to J convert two (default = Replace All)Print('1+2+3+4'. Split ('+', 2))#returns the value of the list as a delimiter, performing 2 times (default = All)Print('Jone Jack .'. Swapcase ())#Case ReversalPrint('Jone Jack .'. Title ())#convert to Heading style

Results

VI: Dictionary

1.

data = {'Student1':'Jack','Student2':'Joker','Student3':'Jone'}Print(data['Student1'])#the dictionary has no order or subscript, and the value is passed keydata['Student1'] ='Sam' #Modifying the value of a student1data['Student4'] = {'Amy': 11}#add a value that can be a string, a dictionary, a list, a tupledata['student5'] = ['Jack','Linda']data[6] = (1,2,3,4)Print(data)delDATA[6]#DeleteData.pop ('student5')#DeletePrint(data)Print(Data.get ('student5'))#Find and return according to Key, cannot find return none same (if ' student5 ' in Data:else)Print(Data.values ())#Print all ValuesPrint(Data.keys ())#Print all keys

2.

A = {'a': 1,'b': 2= {'a ' : 7,'C': 3# update A with B as parameter (key repeat replace, do not increment)print  (a)print# changes A to a list, and the element becomes a tuple

Python Learning second 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.