Python programming Quick Start-making tedious work automated fourth chapter list exercises and their answers

Source: Internet
Author: User
Tags shallow copy

Fourth. List of exercises and their answers 1, what is []?

A: An empty list value, which is a list that does not contain any list items. This is similar to the null string value.

2, how to assign ' hello ' to the third value of the list, and the list is saved in a variable named spam? (Suppose the variable contains [2, 4, 6, 8, 10])

A: spam[2] = ' Hello ' (note that the 3rd value in the list is subscript 2 because the first value subscript is 0.) )

For the next 3 questions, assume that the spam contains the list [' A ', ' B ', ' C ', ' d ']

3, Spam[int (' 3 ' * *)/11] What is the value?
For:'D'(Note'3'* * is a string' -', it is passed in Int () and then divided by 11. This is the final value of 3. You can use an expression where the value is used. >>> spam=['a','b','C','D']>>> Spam[int ('3'* *)//11]'D'
4, Spam[-1] evaluation of how much?

Answer: ' d ' (negative number subscript from the end of the countdown).

5, Spam[:2] evaluation of how much?
Answer:>>> spam['a'b' C ' ' D ' ]>>> spam[:2['a''b' ]

For the next 3 questions. Assume bacon contains list [3.14, ' cat ', One, ' cat ', True]

6. What is the value of Bacon.index (' cat ')?
Answer: ' Cat ' ' Cat ' , True] >>> bacon.index ('cat')
7, Bacon.append (99) How to make the list value in bacon?
' Cat ' ' Cat ' , True] >>> bacon.append >>> bacon['cat'  cat', True,[+]
8. What does Bacon.remove (' cat ') make list values in bacon?
Answer:>>> bacon['cat'cat', True, 99 ]>>> bacon.remove ('cat')>>> bacon[  'cat', True,[+]
9. What are the operators for list connection and replication?

A: The list connection operator is +, and the copied operator is * (this is the same as the string).

10. What is the difference between the append () and the Insert () list methods?

A: Append () will only add values at the end of the list, and insert () can add values to any location in the list.

11. What are the two ways to remove a value from a list?

A: The DEL statement and the Remove () List method are two methods that are worth removing from the list.

12. Please say a few similarities between the list value and the string.

A: Both the list and the string can pass in Len (), with subscripts and slices for the For loop, join or copy, and use with the in and not in operators.

13, what is the difference between a list and a tuple?

A: Lists can be modified, and they can add values, delete values, and modify values. Tuples are not modifiable, they cannot be changed at all. Also, tuples use parentheses, and the list uses square brackets [].

14, if there is only one integer value 42 in the tuple, how to enter the tuple?

A: The comma at the end of Spam= (42,) is required.

15. How do I get from a list worth to a tuple form? How do I get the list form from a tuple?

A: Use the tuple () and list () functions, respectively.

16, the "include" List of variables, actually does not really directly contain the list. What do they contain?

A. They contain a reference to the list that is worth.

What is the difference between 17, copy.copy () and copy.deepcopy ()?

A: the Copy.copy () function will shallow copy the list, while the copy.deepcopy () function will copy the list deep. That is, only copy.deepcopy () duplicates all lists within the list.

4.10 Practical Projects

As a practice, programming completes the following tasks.

4.10.1 Comma Code

Suppose you have the following list:
Spam = [' apples ', ' bananans ', ' tofu ', ' cats ']
Write a function that returns a string with a list value as an argument. The string contains all of the table entries, separated by commas and spaces between the table items.
and insert and before the last table item. For example, passing the previous list of spam to the function will return ' apples, bananas, tofu, and cats '.
But your function should be able to handle any list that is passed to it.

"""4.10 Practice Projects as a practice, programming to complete the following tasks. The 4.10.1 comma code assumes the following list: spam = [' apples ', ' bananans ', ' tofu ', ' cats ') write a function that returns a string with a list value as an argument. The string contains all the table entries, separated by commas and spaces, and inserted before the last table entry. For example, passing the previous list of spam to the function will return ' apples, bananas, tofu, and cats '. """spam= ['Apples','Bananans','Tofu','Cats']defList_str (LST):return ','. Join (lst[:-1]+[' and'+lst[-1]]) a=list_str (spam)Print(a)
4.10.2 Word metalized Network

Suppose you have a list of lists, and each value of the inner list is a string that contains one character, like this:
Grid = [['. ', '. ', '. ', '. ', '. ', '. '],
['. ', ' o ', ' o ', '. ', '. ', '. '],
[' O ', ' o ', ' o ', ' o ', '. ', '. '],
[' O ', ' o ', ' o ', ' o ', ' o ', '. '],
['. ', ' o ', ' o ', ' o ', ' o ', ' o '],
[' O ', ' o ', ' o ', ' o ', ' o ', '. '],
[' O ', ' o ', ' o ', ' o ', '. ', '. '],
['. ', ' o ', ' o ', '. ', '. ', '. '],
[‘.‘, ‘.‘, ‘.‘, ‘.‘, ‘.‘, ‘.‘]]

You can think of grid[x][y] as a "graph" character at x, y coordinates, which consists of text characters. The origin (0,0) in the upper-left corner, the right x-coordinate increases,
The downward y-coordinate increases.
Copy the previous grid value and write the code to print out the image.
.. Oo. Oo..
. Ooooooo.
. Ooooooo.
.. Ooooo.
... Ooo...
.... O....

Hint: You need to use loop nesting loops, print out grid[0][0], then grid[1][0], and then grid[2][0], and so on,
Until Grid[8][0]. This completes the first line, so the next line is printed. Then the program will print out grid[0][1], then grid[1][1], then grid[2][1],
And so on The program finally prints out grid[8][5].
Also, if you do not want to automatically print line breaks after each print () call, remember to pass the end keyword argument to print ().

#!/usr/bin/env Python3#-*-coding:utf-8-*-#Author:davie"""4.10.2 Word metalized The network assumes that there is a list of lists, each value of the inner list is a string that contains one character, like this: Grid = ['. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' o ', '. ', '. ', '  . '],[' o ', ' o ', ' o ', ' o ', '. ', '. '],[' o ', ' o ', ' o ', ' o ', ' o ', '. '],['. ', ' o ', ' o ', ' o ', ' o ', ' o '],[' o ', ' o ', ' o ', ' o ', ' o ',    '. '],[' o ', ' o ', ' o ', ' o ', '. ', '. '],['. ', ' o ', ' o ', '. ', '. ', '. '],[', ', '. ', '. ', '. ', '. ', '. '] You can think of grid[x][y] as a "graph" character at x, y coordinates, which consists of text characters.    The origin (0,0) in the upper-left corner, the right x-coordinate increases, and the downward y-coordinate. Copy the previous grid values, write the code to print out the image ... Oo. Oo... Ooooooo. Ooooooo ... Ooooo ..... Ooo....... O.... Tip: You need to use loop nesting loops, print out grid[0][0], then grid[1][0], then grid[2][0], and so on, until Grid[8][0]. This completes the first line, so the next line is printed. The program will then print out grid[0][1], then grid[1][1], then grid[2][1], and so on.    The program finally prints out grid[8][5]. Also, if you do not want to automatically print line breaks after each print () call, remember to pass the end keyword argument to print (). """Grid= [['.','.','.','.','.','.'],['.','O','O','.','.','.'],['O','O','O','O','.','.'],['O','O','O','O','O','.'],['.','O','O','O','O','O'],['O','O','O','O','O','.'],['O','O','O','O','.','.'],['.','O','O','.','.','.'],['.','.','.','.','.','.']]list_len=len (grid) List_col=Len (grid[0]) forIinchRange (List_col): forJinchRange (List_len):Print(grid[j][i],end="')        Print()

Python programming Quick Start-making tedious work automated fourth chapter list exercises and their answers

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.