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.
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 thiTrong 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:
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ạnhx ** yMô đ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
- 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.
- 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.
- 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
0vim myFile.py
1 vim myFile.py
2vim myFile.py
3vim myFile.py
1 vim myFile.py
5Lưu bài viết
Đọc
Đầu ra:
chmod +x myFile.py
2Bàn luậnIn Python, – is the subtraction operator. It is used to subtract the second value from the first value.
Example :
vim myFile.py
0vim myFile.py
1 vim myFile.py
2vim myFile.py
3vim myFile.py
1 vim myFile.py
5vim myFile.py
6vim myFile.py
1 vim myFile.py
0def 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
33. 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
0vim myFile.py
1 vim myFile.py
2vim myFile.py
3vim myFile.py
1 vim myFile.py
5vim myFile.py
6vim myFile.py
1 vim myFile.py
0n1 = 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
43. 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
0vim myFile.py
1 vim myFile.py
2vim myFile.py
3vim myFile.py
1 vim myFile.py
5vim myFile.py
6vim myFile.py
1 vim myFile.py
0n1 = 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
53. 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
0vim myFile.py
1 vim myFile.py
2vim myFile.py
3vim myFile.py
1 vim myFile.py
5vim myFile.py
6vim myFile.py
1 vim myFile.py
0n1 = 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
64. 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
0vim myFile.py
1 vim myFile.py
2vim myFile.py
3vim myFile.py
1 vim myFile.py
5vim myFile.py
6vim myFile.py
1 vim myFile.py
0n1 = 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
77. 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
0vim myFile.py
1 vim myFile.py
5vim myFile.py
3vim myFile.py
1 vim myFile.py
2vim myFile.py
6vim 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
6Dưới đây là bản tóm tắt của tất cả 7 toán tử:
Nhà điều hành | Sự mô tả | Cú pháp |
---|
+ | Bổ sung: Thêm hai toán hạng | x + y |
- | Phép trừ: trừ hai toán hạng | x - y |
* | Phép nhân: nhân hai toán hạng | x * y |
/ | Phân chia (FLOAT): Chia toán hạng đầu tiên cho phần thứ hai | x / y |
// | Phân chia (Tầng): Chia toán hạng đầu tiên cho lần thứ hai | x // y |
Phần trăm | 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 |
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.
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.