Python 3 day2 (bottom)

Source: Internet
Author: User
Tags shallow copy

For one, today is mainly about the use and distinction of shallow copy and deep copy in Python. For example, use Copy to copy a list completely.

Change the value of the subscript to 3 Alex to Alexander by names[3][0] = changed value (Note that the next subscript 0 is the meaning of Alex subscript position in the small list)

At this point the result is:

We will find that "Peng" selectively copy in Names and name2, but Alexander are copied in 1 and 2. Note that copy is only a shallow copy of the first layer copy.

If I change the value of name2, will names follow the copy? The answer is yes.

What should I do if I need to completely copy a list?

Then look at the application of step slices:

As a result, take one of the following values:

2. Tuples

Tuples are actually similar to the list, but also a set number, but once it is created, it can no longer be modified, so it is called a read-only list.

Grammar:

It has only 2 methods, one is count, the other is index, complete.

Program Exercises

Please close your eyes and write the following procedure

Program: Shopping Cart Program

Demand:

1. After starting the program, let the user enter the payroll and then print the list of items

2. Allow users to purchase items based on their product number

3. After the user selects the product, checks whether the balance is enough, enough on the direct payment, not enough on the reminder

4. Can exit at any time, exit Yes, print purchased goods and balances.

After you clear the main logical idea of a program, you will find that by accumulating these logical thinking, writing a program is no longer a blank face.

But at this point we will find that there is no product number in the result of the operation, only one time:

Since these products have subscript in the list, we can use subscript to product number:

After changing print to show, we can see that the subscript comes out:

It is also easier to use enumerate to take the subscript of the data in the list directly:

After the perfect version of the program script is this:

Look carefully will find that this program is only considering the purchase of the situation, do not involve the situation can not afford to buy, if you can not afford to write it? Simply add an else below the added line of code:

The results of the operation are shown below:

Then the problem again, the program can not run indefinitely, if you want to quit the program, how should it be operated?

Second, string manipulation

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 ' Maketrans >>> intab = "Aeiou" #This are the string having actual characters. >>> outtab = "12345" #This is the string has corresponding mapping character >>> Trantab = Str.mak    Etrans (Intab, outtab) >>> >>> str = "This is string EXAMPLE....WOW!!!" >>> str.translate (trantab) ' th3s 3s str3ng 2x1mpl2....w4w!!! ' msg.partition (' is ') output (' My name ', ' is ', ' {n  AME}, and age was {age} ') >>> "Alex Li, Chinese name was Lijie". Replace ("Li", "Li", 1) ' Alex Li, Chinese name ' is Lijie ' msg.swapcase case swap >>> Msg.zfill 00000my name is {name}, and age was {age} ' >>> N4.ljust (40, "- ") ' Hello 2orld-----------------------------' >>> n4.rjust (+,"-") '-----------------------------Hello 2orld ' >>> b= "ddefdsdff_ haha" >>> b.isidentifier () #检测一段字符串可否被当作标志符, that is, whether to conform to the variable naming rules true

third, the dictionary operation
a Key-value data type, used as a dictionary of our school, to check the details of the corresponding page by strokes and letters.
For example, we need to save people's age, height, gender and other information (in fact, using a list can also be stored operations, but need to remember the subscript information for each value, which is too cumbersome), here is a more convenient and practical way:
info = {    ' stu1101 ': ' Tenglan Wu ',    ' stu1102 ': ' Longze luola ',    ' stu1103 ': ' Xiaoze Maliya ',}

At this point we will find out that print is out of order after printing because the dictionary does not have a subscript for the cause. Because the dictionary is unordered, we take the value of the key through the, print (key name). It is found that:

Features of the dictionary:

(1) Dict is disordered.

(2) key must be unique and so is inherently heavy.

That now "check" can be queried, the dictionary can be modified operation? Yes, let's take a look at what happens when you modify existing information:

If you modify the information that does not exist in the original dictionary, can it be modified successfully? For example, the original dictionary does not stu1104 this key, and now we operate to change stu1104 to "Miyamoto Musashi", the result will be what?

The results after printing show that the non-existent information is automatically added to the non-existent dictionary information when it is modified.

The results are printed as follows:

Homework after this lesson:

Shopping Cart

User Portal:

1. Product information in the document

2. Purchased goods, balance record (long time retention).

Merchant Entrance:

1. Can add goods, change the price of goods

Python 3 day2 (bottom)

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.