程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Typical cases of Python lists

編輯:Python

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 .


One 、 background

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 .


Two 、 Clean Calculator Beta

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 .

3、 ... and 、 Calculate new matrix

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 .


Four 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


5、 ... and 、 summary

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved