About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python Typical cases related to the list .
example : Write a simple calculator , According to the input numbers and four operation symbols , Calculate the operation result and output .
operator_list = ['+', '-', '*', '/'] # Create a list and assign four operators
number_1 = float(input(" Please enter the first operand :")) # Get the first operand
operator = input(" Please enter operator :") # Get operator
number_2 = float(input(" Please enter the second operand :")) # Get the second operand
# Determine the operator entered by the user and calculate , Assign the result to a variable result
if operator not in operator_list: # The operator entered is not a four rule operator
print(" Wrong operator entered , Please enter four operators !") # Output prompt
else: # The operator entered belongs to the four operators
if operator == '+': # The operator is “+”
result = number_1 + number_2 # The value of the addition of two numbers is assigned to the variable result
elif operator == '-': # The operator is “-”
result = number_1 - number_2 # The value of subtracting two numbers is assigned to the variable result
elif operator == '*': # The operator is “*”
result = number_1 * number_2 # The value of multiplying two numbers is assigned to the variable result
elif operator == '/': # The operator is “/”
result = number_1 / number_2 # The value of dividing two numbers is assigned to the variable result
print(number_1, operator, number_2, "=", result) # Output the result of two operands
give the result as follows .
Two are known 3 That's ok 3 Columns of the matrix , Add the data of its corresponding position , And return a new matrix .
X = [[12, 7, 3], [4, 5, 6], [7, 8, 9]] # Define the matrix X
Y = [[5, 8, 1], [6, 7, 3], [4, 5, 9]] # Define the matrix Y
result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # Define new matrix
for i in range(len(X)): # Loop control line
for j in range(len(X[0])): # Loop control column
result[i][j] = X[i][j] + Y[i][j] # Calculate the element values in the new matrix
for r in result: # Traverse the elements in the output new matrix
print(r)
give the result as follows .
1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial
The above is about python List relevant knowledge of typical cases , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .