Python list syntax Learning (with example)

Source: Internet
Author: User

Create: list = [5, 7, 9]
Value and value change: list [1] = list [1] * 5
Insert at the end of the list: list. append (4)
Remove the 0th values and return the value of The 0th values: list. pop (0)
Remove the 0th values but no value is returned: del (list [0])
Remove a specific value: list. remove (35)

Function:
No parameter: def function ():
One parameter: def function (x ):
Two parameters: def function (y ):
Any parameter: def add_function (* args ):

Function range:
One parameter: range (n) starts from 0th digits and counts n digits
Two parameters: range (m, n) starts from the MTH bit to the n-1 bit, and the increment interval is 1.
Three parameters: range (m, n, I) starts from the MTH bit to the n-1 bit, and the incremental interval is
For item in list: equivalent to for I in range (len (list ):

Use separator as the interval output for the elements in the list: print separator. join (list)
For example: list = ['A', 'B', 'C', 'D'] General print list will output: ['A', 'B ', 'C', 'D'].
Print "". join (list) will output: a B c d (it must be double quotation marks, and single double quotation marks do not work)

Accept keyboard input:
Guess_row = int (raw_input ("Guess Row :"))

The following is a self-written small program: generates a matrix and a random position, which is used by players to guess where the generated position is.
Copy codeThe Code is as follows:
From random import randint
Def creat_board (length ):
Board = []
For I in range (length ):
Board. append (['O'] * length)
Return board
Def print_board (x ):
For row in x:
Print "". join (row)
Def random_row (board ):
Return randint (0, len (board)-1)

Def random_col (board ):
Return randint (0, len (board [0])-1)

Length = int (raw_input ("Enter board's length you :"))
Board = creat_board (length)
Print_board (board)
Turns = int (raw_input ("Enter turns you want to play :"))
For turn in range (turns ):
Ship_row = random_row (board)
Ship_col = random_col (board)
Print "This is" + str (turn + 1) + "th time to guess :"
Guess_row = int (raw_input ("Enter the row you guess :"))
Guess_col = int (raw_input ("Enter the col you guess :"))

If guess_row = ship_row and guess_col = ship_col:
Print "You win! "
Break
Else:
If (guess_row <0 or guess_row> len (board)-1) or (guess_col <0 or guess_col> len (board)-1 ):
Print "Incorrect input! "
If turn = turns-1:
Print "Turns out! "
Elif board [guess_row] [guess_col] = 'X ':
Print "You have guessed it already! "
If turn = turns-1:
Print "Turns out! "
Else:
Print "You guess wrong! "
Board [guess_row] [guess_col] = 'X'
Print_board (board)
If turn = turns-1:
Print "Turns out! "

Previous mistakes:
1. When I create a board function, I forget to return a board. Therefore, it is always empty, and subsequent operations are out of bounds;
2. when a random position is generated, the row and col names are always the same as the generated function names (random_row = random_row (board), resulting in TypeError: 'int' object is not callable error.

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.