Hướng dẫn arithmetic operations in python using function - các phép toán số học trong python sử dụng hàm

Về phòng thí nghiệm thực hành này

Trong phòng thí nghiệm thực hành này, chúng tôi sẽ cố gắng viết bốn chức năng sẽ thực hiện các hoạt động số học khác nhau: bổ sung, trừ, nhân và chia.

Mục tiêu học tập

Hoàn thành thành công phòng thí nghiệm này bằng cách đạt được các mục tiêu học tập sau:

Tạo một tệp và làm cho nó thực thi
  1. Tạo tệp:

    touch myFile.py
  2. Làm cho nó thực thi:

    chmod +x myFile.py
Viết ba chức năng có thể thêm, trừ và nhân hai số
  1. Mở tập tin:

    vim myFile.py
  2. Thêm phần sau vào tệp:

    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
Viết một hàm để chia hai số và thực hiện kiểm tra phân chia cho 0
  1. Thêm phần sau vào tệp:

    def div(n1, n2):
        if n2 == 0:
            return "ERROR: Cannot divide by 0. Second parameter cannot be a 0!"
        else:
          return n1/n2
Viết một hàm để chia hai số và thực hiện kiểm tra phân chia cho 0
  1. Thêm phần sau vào tệp:

    n1 = 10
    n2 = 11
    
    print(add(n1, n2))
    print(sub(n1, n2))
    print(multi(n1, n2))
    print(div(n1, n2))
    n2 = 0
    print(div(n1, n2))
  2. Viết một hàm để chia hai số và thực hiện kiểm tra phân chia cho 0Escape followed by

    chmod +x myFile.py
    9.

  3. Chạy các bài kiểm tra và gọi tất cả bốn chức năng

    ./myFile.py

Lưu và thoát tệp bằng cách nhấn Escape theo sau là chmod +x myFile.py9.

Gọi tất cả bốn chức năng:

Tài nguyên bổ sung

  1. Có nhiều cách thực hiện phòng thí nghiệm này. Bạn có thể sử dụng bảng điều khiển tương tác Python hoặc ghi mã của bạn vào một tệp.

    touch myFile.py
  2. Trong trường hợp bạn quyết định viết mã của mình vào tệp, bạn sẽ cần phải làm như sau.

    chmod +x myFile.py
  3. Tạo tệp của bạn (bạn có thể thay đổi tên tệp nếu bạn muốn):

    ./myFile.py

Thay đổi quyền tệp để thực thi nó:

chmod +x myFile.py
0

Khi bạn điền vào tệp bằng mã của mình, hãy thực hiện nó bằng:

Trong trường hợp bạn quyết định sử dụng bảng điều khiển tương tác Python, hãy chạy như sau:

chmod +x myFile.py
1

Hướng dẫn arithmetic operations in python using function - các phép toán số học trong python sử dụng hàm

Từ đó, bạn có thể tiếp tục viết mã của bạn.

Con đường Python là:

Mô đun: Trả về phần còn lại khi toán hạng đầu tiên được chia cho lần thứ hai

x % y

**

  • Sức mạnh: Trả về lần đầu tiên được nâng lên để tăng sức mạnh
  • x ** y
  • Mô đun: Trả về phần còn lại khi toán hạng đầu tiên được chia cho lần thứ hai

    x % y

    **

    Sức mạnh: Trả về lần đầu tiên được nâng lên để tăng sức mạnh are used to perform mathematical operations like addition, subtraction, multiplication and division.

    x ** y

    1. Các toán tử số học trong Python giải thích với ví dụ là gì?
    2. Các toán tử số học Python.
    3. 5 toán tử số học là gì?
    4. Các toán tử số học thực hiện các hoạt động bổ sung, trừ, nhân, chia, số mũ và mô đun.
    5. Các chức năng Python là gì?
    6. Một hàm là một khối mã chỉ chạy khi nó được gọi. Bạn có thể truyền dữ liệu, được gọi là tham số, thành một hàm.
    7. Xem thảo luận

    Cải thiện bài viếtIn Python, + is the addition operator. It is used to add 2 values.
    Example :

    vim myFile.py
    0
    vim myFile.py
    1
    vim myFile.py
    2

    vim myFile.py
    3
    vim myFile.py
    1
    vim myFile.py
    5

    Lưu bài viết

    Đọc

    Đầu ra:

    chmod +x myFile.py
    2

    Bàn luậnIn Python, is the subtraction operator. It is used to subtract the second value from the first value.
    Example :

    vim myFile.py
    0
    vim myFile.py
    1
    vim myFile.py
    2

    vim myFile.py
    3
    vim myFile.py
    1
    vim myFile.py
    5

    vim myFile.py
    6
    vim myFile.py
    1
    vim myFile.py
    0
    def div(n1, n2):
        if n2 == 0:
            return "ERROR: Cannot divide by 0. Second parameter cannot be a 0!"
        else:
          return n1/n2
    2
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    0

    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    1
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    2

    Đầu ra:

    chmod +x myFile.py
    3

    3. Toán tử nhân: Trong Python, * là toán tử nhân. Nó được sử dụng để tìm sản phẩm của 2 giá trị. Ví dụ:In Python, * is the multiplication operator. It is used to find the product of 2 values.
    Example :

    vim myFile.py
    0
    vim myFile.py
    1
    vim myFile.py
    2

    vim myFile.py
    3
    vim myFile.py
    1
    vim myFile.py
    5

    vim myFile.py
    6
    vim myFile.py
    1
    vim myFile.py
    0
    n1 = 10
    n2 = 11
    
    print(add(n1, n2))
    print(sub(n1, n2))
    print(multi(n1, n2))
    print(div(n1, n2))
    n2 = 0
    print(div(n1, n2))
    5
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    0

    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    1
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    2

    Đầu ra:

    chmod +x myFile.py
    4

    3. Toán tử nhân: Trong Python, * là toán tử nhân. Nó được sử dụng để tìm sản phẩm của 2 giá trị. Ví dụ:In Python, / is the division operator. It is used to find the quotient when first operand is divided by the second.
    Example :

    vim myFile.py
    0
    vim myFile.py
    1
    vim myFile.py
    2

    vim myFile.py
    3
    vim myFile.py
    1
    vim myFile.py
    5

    vim myFile.py
    6
    vim myFile.py
    1
    vim myFile.py
    0
    n1 = 10
    n2 = 11
    
    print(add(n1, n2))
    print(sub(n1, n2))
    print(multi(n1, n2))
    print(div(n1, n2))
    n2 = 0
    print(div(n1, n2))
    5
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    0

    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    1
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    2

    Đầu ra:

    chmod +x myFile.py
    5

    3. Toán tử nhân: Trong Python, * là toán tử nhân. Nó được sử dụng để tìm sản phẩm của 2 giá trị. Ví dụ:In Python, % is the modulus operator. It is used to find the remainder when first operand is divided by the second.
    Example :

    vim myFile.py
    0
    vim myFile.py
    1
    vim myFile.py
    2

    vim myFile.py
    3
    vim myFile.py
    1
    vim myFile.py
    5

    vim myFile.py
    6
    vim myFile.py
    1
    vim myFile.py
    0
    n1 = 10
    n2 = 11
    
    print(add(n1, n2))
    print(sub(n1, n2))
    print(multi(n1, n2))
    print(div(n1, n2))
    n2 = 0
    print(div(n1, n2))
    5
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    0

    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    1
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    2

    Đầu ra:

    chmod +x myFile.py
    6

    4. Nhà điều hành bộ phận: Trong Python, / là nhà điều hành bộ phận. Nó được sử dụng để tìm thương số khi toán hạng đầu tiên được chia cho thứ hai.example:In Python, ** is the exponentiation operator. It is used to raise the first operand to power of second.
    Example :

    vim myFile.py
    0
    vim myFile.py
    1
    vim myFile.py
    2

    vim myFile.py
    3
    vim myFile.py
    1
    vim myFile.py
    5

    vim myFile.py
    6
    vim myFile.py
    1
    vim myFile.py
    0
    n1 = 10
    n2 = 11
    
    print(add(n1, n2))
    print(sub(n1, n2))
    print(multi(n1, n2))
    print(div(n1, n2))
    n2 = 0
    print(div(n1, n2))
    5
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    0

    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    1
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    2

    Đầu ra:

    chmod +x myFile.py
    7

    7. Phân chia sàn: Trong Python, // được sử dụng để tiến hành phân chia sàn. Nó được sử dụng để tìm sàn của thương số khi toán hạng đầu tiên được chia cho thứ hai.example:In Python, // is used to conduct the floor division. It is used to find the floor of the quotient when first operand is divided by the second.
    Example :

    vim myFile.py
    0
    vim myFile.py
    1
    vim myFile.py
    5

    vim myFile.py
    3
    vim myFile.py
    1
    vim myFile.py
    2

    vim myFile.py
    6
    vim myFile.py
    1
    vim myFile.py
    0
    ./myFile.py
    8
    ./myFile.py
    8
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    0

    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    1
    #!/usr/bin/python
    
    def add(n1, n2):
        return n1+n2
    def sub(n1, n2):
        return n1-n2
    def multi(n1, n2):
        return n1*n2
    2

    Đầu ra:

    chmod +x myFile.py
    6

    Dưới đây là bản tóm tắt của tất cả 7 toán tử:

    Nhà điều hànhSự mô tảCú pháp
    +Bổ sung: Thêm hai toán hạngx + y
    -Phép trừ: trừ hai toán hạngx - y
    *Phép nhân: nhân hai toán hạngx * y
    /Phân chia (FLOAT): Chia toán hạng đầu tiên cho phần thứ haix / y
    //Phân chia (Tầng): Chia toán hạng đầu tiên cho lần thứ haix // y
    Phần trămMô đun: Trả về phần còn lại khi toán hạng đầu tiên được chia cho lần thứ haix % y
    **Sức mạnh: Trả về lần đầu tiên được nâng lên để tăng sức mạnhx ** y

    Các toán tử số học trong Python giải thích với ví dụ là gì?

    Các toán tử số học Python.

    5 toán tử số học là gì?

    Các toán tử số học thực hiện các hoạt động bổ sung, trừ, nhân, chia, số mũ và mô đun.addition, subtraction, multiplication, division, exponentiation, and modulus operations.

    Các chức năng Python là gì?

    Một hàm là một khối mã chỉ chạy khi nó được gọi.Bạn có thể truyền dữ liệu, được gọi là tham số, thành một hàm.a block of code which only runs when it is called. You can pass data, known as parameters, into a function.