Home > Article > Backend Development > python list syntax learning
Create: list = [5,7,9]
Value and change: list[1] = list[1] * 5
Insert at the end of the list: list.append(4)
Remove the 0th value and return the value of the 0th value: list.pop(0)
Remove the 0th value but not return the value: del(list[0])
Remove a specific value: list .remove(35)
Function function:
No parameters: def function():
One parameter: def function(x):
2 parameters: def function(y):
Any number of parameters: def add_function(*args):
Function range:
One parameter: range(n) Counting n digits starting from digit 0
Two parameters: range (m,n) Starting from the mth position to the n-1th position, the increment interval is 1
Three parameters: range(m,n,i) Starting from the mth position to the n-1th position, the incrementing interval For i
for item in list: and for i in range(len(list)): Equivalent
Use separator as the interval output for the elements in the list: print separator.join(list)
For example: list = ['a','b','c','d'] A general print list will output: ['a','b','c','d'].
And print " ".join(list) will output: a b c d (must be double double quotes, single double quotes will not work)
Accept keyboard input:
guess_row = int(raw_input ("Guess Row:"))
The following is a small program I wrote: generate a square matrix and a random position, and ask the player to guess where the generated position is
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!"
Once committed Errors:
1. The board creation function forgot to return a board, so it was always empty, causing subsequent operations to go out of bounds;
2. When generating random positions, the naming of the position row and col always followed The generated function names are the same (random_row=random_row(board)), resulting in TypeError: 'int' object is not callable error.
For more articles related to python list syntax learning, please pay attention to the PHP Chinese website!