Làm thế nào để bạn thực hiện phép cộng và phép trừ trong python?
Chào mừng bạn đến với hướng dẫn này. Sau đây chúng ta sẽ tìm hiểu cách thực hiện phép cộng và phép trừ Vector trong Python Show Vectơ trong thuật ngữ lập trình dùng để chỉ mảng một chiều. Mảng là một trong những cấu trúc dữ liệu lưu trữ các phần tử tương tự i. các phần tử có cùng kiểu dữ liệu.
Phép cộng và phép trừ vectơ trong PythonBây giờ, hãy tìm hiểu cách thực hiện các phép toán cơ bản như cộng và trừ trên mảng trong Python Để thực hiện tác vụ này, chúng ta cần biết về mô-đun Numpy trong Python. Numpy là Python số có một số phương thức sẵn có sẽ giúp nhiệm vụ của chúng ta dễ dàng hơn Cách dễ nhất và đơn giản nhất để tạo một mảng trong Python là thêm các ký tự được phân tách bằng dấu phẩy trong dấu ngoặc vuông phù hợp. Ví dụ Để sử dụng mô-đun tuyệt vời này, chúng ta cần nhập nó. import numpy as NP A = [4, 8, 7] B = [5, -4, 8] print("The input arrays are :\n","A:",A ,"\n","B:",B) Res= NP.add(A,B) print("After addition the resulting array is :",Res) Vì vậy, trong đoạn mã trên, các biến A và B được sử dụng để lưu trữ các phần tử mảng. Để thực hiện phép cộng, chúng ta cần gọi phương thức add() của mô-đun NumPy là NP. cộng(). Ở đây, chúng tôi đã đặt bí danh cho NumPy là NP, điều này không cần thiết, chúng tôi có thể viết trực tiếp là NumPy. cộng() Để thực hiện phép trừ trên cùng các phần tử mảng, chúng ta chỉ cần viết một dòng mã khác gọi phương thức trừ i. e NP. phép trừ() và in kết quả thu được sau phép trừ Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]2 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]0 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]1 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]2 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]3 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]4 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]5 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]6 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]3 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]8 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]9 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]3 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]11 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]12 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]0 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]1_______18 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]3 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]17 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]5 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]19 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]3 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]41 Printing elements of first matrix [[1 2] [3 4]] Printing elements of second matrix [[4 5] [6 7]] Addition of two matrix [[ 5 7] [ 9 11]]9 Có chức năng trừ trong Python không?Như bạn mong đợi, Hàm trừ Numpy thực hiện phép trừ với mảng Numpy và các đối tượng Python khác.
Biểu tượng trừ trong Python là gì?Toán tử số học Python |