Python list syntax learning (with examples)
- 2020-04-02 13:14:33
- OfStack
Create: list = [5,7,9]
Value and change: list[1] = list[1] * 5
List.append (4)
Remove the 0th value and return the value of the 0th value: list.pop(0)
Del (list[0])
List. Remove (35)
Function the function:
Def function():
Def function(x):
Def function(y):
Def add_function(*args):
Function range:
One parameter: range(n) We're going to start at the 0th place
Two parameters: range(m,n) from the m bit to the n-1 bit, with an increasing interval of 1
Three parameters: range(m,n, I) from the MTH bit to the n-1st bit, with an increasing interval of I
For the item in the list: That's the same thing as for I in range of len of list
Print: print separator. Join (list)
List = ['a','b','c','d'] A typical print list prints: ['a','b','c','d'].
Print "".join(list) prints: a, b, c, d(must be double double quotes, single double quotes will not work)
Accept keyboard input:
Guess_row = int (raw_input (" Guess the Row: "))
Here is a small program written by oneself: generate a square matrix and random position, ask the player to guess where the generated position is
Mistakes you've made:
1. The creation board function forgot to return a board, so it is always empty, resulting in the subsequent operations are out of bounds;
2. When generating random positions, location row and col are always named the same as the generated function names (random_row=random_row(board)), resulting in TypeError: 'int' object is not callable.
Value and change: list[1] = list[1] * 5
List.append (4)
Remove the 0th value and return the value of the 0th value: list.pop(0)
Del (list[0])
List. Remove (35)
Function the function:
Def function():
Def function(x):
Def function(y):
Def add_function(*args):
Function range:
One parameter: range(n) We're going to start at the 0th place
Two parameters: range(m,n) from the m bit to the n-1 bit, with an increasing interval of 1
Three parameters: range(m,n, I) from the MTH bit to the n-1st bit, with an increasing interval of I
For the item in the list: That's the same thing as for I in range of len of list
Print: print separator. Join (list)
List = ['a','b','c','d'] A typical print list prints: ['a','b','c','d'].
Print "".join(list) prints: a, b, c, d(must be double double quotes, single double quotes will not work)
Accept keyboard input:
Guess_row = int (raw_input (" Guess the Row: "))
Here is a small program written by oneself: generate a square matrix and random position, 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!"
Mistakes you've made:
1. The creation board function forgot to return a board, so it is always empty, resulting in the subsequent operations are out of bounds;
2. When generating random positions, location row and col are always named the same as the generated function names (random_row=random_row(board)), resulting in TypeError: 'int' object is not callable.