Python study notes the next day

Source: Internet
Author: User
Tags new set readline set set shallow copy

    • list, tuple operations
    • String manipulation
    • Dictionary operations
    • Collection operations
    • File operations
    • Homework

(PS: Yesterday wrote did not save ....) Want to cry, so today write the content of the Code demo part in order to save time on the direct copy of others)

One, list, tuple operations

1.1. List operation

Initialization of the list:

names = [' Alex ', ' Tenglan ', ' Eric ']

List of slices: (left closed right open, front small after large)

>>> names = ["Alex", "Tenglan", "Eric", "Rain", "Tom", "Amy"]>>> Names[1:4]  #取下标1至下标4之间的数字, including 1, Not including 4[' Tenglan ', ' Eric ', ' Rain ']>>> names[1:-1] #取下标1至-1 value, excluding -1[' Tenglan ', ' Eric ', ' Rain ', ' Tom ']>> > Names[0:3] [' Alex ', ' Tenglan ', ' Eric ']>>> names[:3] #如果是从头开始取, 0 can be ignored, like the effect of the sentence [' Alex ', ' Tenglan ', ' Eric '] >>> names[3:] #如果想取最后一个, must not write-1, can only write [' Rain ', ' Tom ', ' Amy '] >>> names[3:-1] #这样-1 will not be included [' Rain ', ' Tom ']>>> names[0::2] #后面的2是代表, every other element, take one [' Alex ', ' Eric ', ' Tom '] >>> names[::2] #和上句效果一样 [' Alex ', ' Eric ', ' Tom ')

Increment of list (append + insert):

>>> names[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ']>>> names.append ("I'm New") >>> names[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm the new ']>>> names[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm the new ']>>> Names.insert (2, "forcibly inserted from Eric's Front") >>> names[' Alex ', ' Tenglan ', ' forcibly inserted from Eric's front ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm the new one '

List of deletions (Pop,del,remove):

>>> del names[2] >>> names[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm the new ']& Gt;>> del names[4]>>> names[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new ']>>> > >> names.remove ("Eric") #删除指定元素 >>> names[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', ' I'm the new ']>>> Names.pop () #删除列表最后一个值 ' I'm the new ' >>> names[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy '

Changes to the list:

>>> names[' Alex ', ' Tenglan ', ' force the insertion from Eric ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm the new ']>> > names[2] = "The substitution" >>> names[' Alex ', ' Tenglan ', ' the substitution ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new here ' ‘]

List check: (For In..,index)

>>> names[' Tom ', ' Tenglan ', ' Amy ', ' Amy ', ' Alex ', ' 3 ', ' 2 ', ' 1 ']>>> names.index ("Amy") 2 #只返回找到的第一个下标

Extension of the list:

>>> names[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ']>>> b = [1,2,3]>>> Names.extend (b) >> > names[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', 1, 2, 3]

Statistics for the list:

>>> names[' Alex ', ' Tenglan ', ' Amy ', ' Tom ', ' Amy ', 1, 2, 3]>>> names.count ("Amy") 2

Copy of the list (shallow copy + deep copy):

Not currently

1.2, tuple operation

Tuples and lists are similar, but are not allowed to be modified, and are represented by ()

Names = ("Alex", "Jack", "Eric")

Tuples are not modifiable, so only count and index two methods

Second, string manipulation

Note: No matter what method, the string itself is not modified

Common Operations for strings:

Name.capitalize () uppercase Name.casefold () uppercase all lowercase name.center (50, "-") output '---------------------Alex Li----------------------' name.count (' lex ') statistics the number of Lex occurrences name.encode () encodes the string into bytes format name.endswith ("Li") to determine whether the string ends with Li " Alex\tli ". Expandtabs (10) Output ' Alex Li ', convert \ t to a long space name.find (' a ') find A, Find returns its index, cannot find return-1 format: >>> msg  = "My name is {}, and" was "{}" >>> Msg.format ("Alex", "Isa") ' My name is Alex ', and ' was ' >>> msg = "My name is {1}, and" was "{0}" >>> Msg.format ("Alex", "Isa") ' My name is ', ' and ' was Alex ' >& gt;> msg = "My name is {name}, and age is {age}" >>> Msg.format (age=22,name= "ale") ' My name is ale, and Format_map >>> msg.format_map ({' name ': ' Alex ', ' age ': +}) ' My name is Alex, and the is ' Msg.index '  (' A ') returns the index ' 9aA ' of the string where a is located. isalnum () True ' 9 '. IsDigit () is an integer name.isnumeric Name.isprintablename.isspacename.istitlename.isupper "|". Join ([' Alex ', ' Jack ', ' Rain ']) ' Alex|jack|raIn 

  

Third, the dictionary operation

Definition of Dictionary:

info = {    ' stu1101 ': ' Tenglan Wu ',    ' stu1102 ': ' Longze luola ',    ' stu1103 ': ' Xiaoze Maliya ',}

Features of the dictionary:

    • Disordered
    • Key must be unique

Increase in Dictionary:

>>> info["stu1104"] = "Cang jing Empty" >>> info{' stu1102 ': ' Longze luola ', ' stu1104 ': ' Cang jing empty ', ' stu1103 ': ' Xiaoze Mal Iya ', ' stu1101 ': ' Tenglan Wu '}

Deletion of the dictionary: Pop,del

>>> info{' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze maliya ', ' stu1101 ': ' Enrique '}>>> info.pop (" stu1101 ") #标准删除姿势 ' Enrique ' >>> info{' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze Maliya '}>>> del info[' stu1103 '] #换个姿势删除 >>> info{' stu1102 ': ' Longze Luola '}

Changes to the dictionary:

>>> info[' stu1101 ' = "Enrique" >>> info{' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze maliya ', ' stu1101 ': ' Enrique '}

Dictionary search:

>>> info = {' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze maliya '}>>> >>> ' stu1102 ' in info #标准 Usage true>>> info.get ("stu1102")  #获取 ' Longze luola ' >>> info["stu1102"] #同上, but look below ' Longze Luola ' >>> info["stu1105"]  #如果一个key不存在, error, get no, not exist only return Nonetraceback (most recent call last):  File "< Stdin> ", line 1, in <module>keyerror: ' stu1105 '

The cycle of the dictionary:

#方法1for key in Info:    print (Key,info[key]) #方法2for k,v in Info.items (): #会先把dict转成list, do not use    print (K,V) when the data is large

  

Iv. Collection Operations

Set set, unordered, non-repeating

Common operations:

s = set ([3,5,9,10])      #创建一个数值集合    t = set ("Hello")         #创建一个唯一字符的集合  A = T | s          # t and S of the set    B = t & s
   
    # T and s    C = t–s          # differential Set (item in T, but not in s)    d = t ^ s          # symmetric difference set (items in t or S, but not both)         basic operation:    T.add (' x ') 
    # Add a    s.update ([10,37,42])  # Add multiple items in s use Remove         () to delete an item:    t.remove (' H ')      Len (s)  The length of the set    x in S  tests if X is a member of S X not in S to    test if X is  not a member of S    s.issubset (t)  s <= T  Test if s  Each of the elements in T    s.issuperset (t)  s >= T  tests if every element in T is in S    s.union (t)  s | t  returns a new set containing Each element in S and T    s.intersection (t)  S & T  returns a new set containing the common elements in S and T    s.difference (t)  s-t  Returns a new set containing an element in s but not in T    s.symmetric_difference (t)  s ^ T  returns a new set containing non-repeating elements in S and T    s.copy () 
     returns a shallow copy of the set "s"
   

  

V. Operation of files

Basic operating Procedures for files:

    • Open the file, get the file handle and assign a value to a variable
    • By manipulating a file handle
    • Close File

Basic operation:

f = open (' lyrics ') #打开文件first_line = F.readline () print (' first line: ', First_line) #读一行print (' I am the divider '). Center (50, '-')) data = F.read () # reads all the rest of the content, do not use print (data) #打印文件 F.close () when the file is large #关闭文件

ReadLine () is a read line, and read () is read all

Open mode of File:

    • R, read-only mode
    • W, write-only mode (unreadable!!!!! Note that this mode, if already have the same name of the file, will create a new file overwrite the original file, resulting in the original file empty)
    • A, append mode (unreadable!!) Compared to W, will be directly in the original name of the file appended content)
    • r+ (Readable and writable)
    • w+ (can be read, but there is still the problem when opening the file first)
    • A + (a)
Vi.. Homework
    1. Implementation of level three menu
    2. Optimization of Shopping Cart
    3. Implements a simple shell sed replacement function
    4. Modifying the Haproxy configuration file

Python study notes the next day

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.