Hướng dẫn what do slashes mean in python? - dấu gạch chéo có nghĩa là gì trong python?

Một dấu gạch chéo ngược ở cuối một dòng bảo Python mở rộng dòng logic hiện tại qua dòng vật lý tiếp theo. Xem phần Cấu trúc dòng của tài liệu tham khảo Python:

2.1.5. Tham gia dòng rõ ràng

Hai hoặc nhiều dòng vật lý có thể được nối thành các dòng logic bằng cách sử dụng các ký tự dấu gạch chéo ngược (\), như sau: Khi một dòng vật lý kết thúc trong một dấu gạch chéo ngược không phải là một phần của chuỗi theo nghĩa đen hoặc nhận xét, nó được nối với hình thành một logic sau đây Dòng, xóa dấu gạch chéo ngược và ký tự kết thúc sau. Ví dụ:

if 1900 < year < 2100 and 1 <= month <= 12 \
   and 1 <= day <= 31 and 0 <= hour < 24 \
   and 0 <= minute < 60 and 0 <= second < 60:   # Looks like a valid date
        return 1

Ngoài ra còn có tùy chọn sử dụng kết nối dòng ngầm, bằng cách sử dụng dấu ngoặc đơn hoặc dấu ngoặc hoặc niềng răng xoăn; Python sẽ không kết thúc đường logic cho đến khi nó tìm thấy khung đóng hoặc nẹp phù hợp cho mỗi khung mở hoặc nẹp. Đây là kiểu mã được đề xuất, mẫu bạn tìm thấy thực sự nên được viết là:

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue

Xem Hướng dẫn theo phong cách Python (PEP 8) (nhưng lưu ý ngoại lệ; một số câu lệnh Python không hỗ trợ dấu ngoặc đơn để các dấu gạch chéo ngược được chấp nhận ở đó).

Lưu ý rằng Python không phải là ngôn ngữ lập trình duy nhất sử dụng dấu gạch chéo ngược để tiếp tục dòng; Bash, C và C ++ PROUNSOR Cú pháp, Falcon, Mathicala và Ruby cũng sử dụng cú pháp này để mở rộng các dòng; Xem Wikipedia.

Hướng dẫn what do slashes mean in python? - dấu gạch chéo có nghĩa là gì trong python?

Đây là một ví dụ:

Bạn có thể thấy rằng

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
7 làm điều tương tự như toán tử
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1.

Khi bạn sử dụng toán tử

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 để chia hai số, phương thức được gọi là phía sau hậu trường là
num1 = 12
num2 = 5
num3 = num1 // num2

print("floor division of", num1, "by", num2, "=", num3)
# Output: floor division of 12 by 5 = 2
5.

Bạn cũng có thể sử dụng phương thức num1 = 12 num2 = 5 num3 = num1 // num2 print("floor division of", num1, "by", num2, "=", num3) # Output: floor division of 12 by 5 = 2 5 này trực tiếp thay cho toán tử if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and words_and_emoticons[i+1].lower() == "of") or item.lower() in BOOSTER_DICT): sentiments.append(valence) continue 1:

  • Cú pháp cơ bản của toán tử
    if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
            words_and_emoticons[i+1].lower() == "of") or
            item.lower() in BOOSTER_DICT):
        sentiments.append(valence)
        continue
    
    1
  • Ví dụ về phân chia sàn
  • Nhà điều hành chém đôi
    if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
            words_and_emoticons[i+1].lower() == "of") or
            item.lower() in BOOSTER_DICT):
        sentiments.append(valence)
        continue
    
    1 hoạt động như
    if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
            words_and_emoticons[i+1].lower() == "of") or
            item.lower() in BOOSTER_DICT):
        sentiments.append(valence)
        continue
    
    7
  • Cách thức toán tử đôi
    if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
            words_and_emoticons[i+1].lower() == "of") or
            item.lower() in BOOSTER_DICT):
        sentiments.append(valence)
        continue
    
    1 hoạt động đằng sau hậu trường
  • Sự kết luận

Cú pháp cơ bản của toán tử if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and words_and_emoticons[i+1].lower() == "of") or item.lower() in BOOSTER_DICT): sentiments.append(valence) continue 1

Để sử dụng nhà điều hành Double Slash

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1, bạn làm những việc gần như trong phân chia thông thường. Sự khác biệt duy nhất là thay vì một dấu gạch chéo
firstNum // secondNum
1, bạn sử dụng chém đôi
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1:

firstNum // secondNum

Ví dụ về phân chia sàn

Nhà điều hành chém đôi

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 hoạt động như
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
7

num1 = 12
num2 = 5
num3 = num1 // num2

print("floor division of", num1, "by", num2, "=", num3)
# Output: floor division of 12 by 5 = 2

Cách thức toán tử đôi

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 hoạt động đằng sau hậu trường

num2 = 5
num3 = num1 / num2

print("normal division of", num1, "by", num2, "=", num3)
# Output: normal division of 12 by 5 = 2.4

Sự kết luận

Để sử dụng nhà điều hành Double Slash

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1, bạn làm những việc gần như trong phân chia thông thường. Sự khác biệt duy nhất là thay vì một dấu gạch chéo
firstNum // secondNum
1, bạn sử dụng chém đôi
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1:

num1 = 29 
num2 = 10 
num3 = num1 / num2
num4 = num1 // num2

print("normal division of", num1, "by", num2, "=", num3)
print("but floor division of", num1, "by", num2, "=", num4)

"""
Output:
normal division of 29 by 10 = 2.9
but floor division of 29 by 10 = 2
"""

Và nếu bạn thực hiện phân chia sàn với số âm, kết quả vẫn sẽ được làm tròn.

Để chuẩn bị cho tâm trí của bạn cho kết quả, làm tròn một số âm có nghĩa là đi từ 0. Vì vậy, -12 chia cho 5 kết quả trong -3. Don Tiết bị nhầm lẫn - mặc dù thoạt nhìn có vẻ như Nubmer đang ngày càng "lớn hơn", nhưng nó thực sự ngày càng nhỏ hơn (xa hơn từ số không/số âm lớn hơn).

num1 = -12
num2 = 5
num3 = num1 // num2

print("floor division of", num1, "by", num2, "=", num3)

# floor division of -12 by 5 = -3

Nhà điều hành chém đôi if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and words_and_emoticons[i+1].lower() == "of") or item.lower() in BOOSTER_DICT): sentiments.append(valence) continue 1 hoạt động như if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and words_and_emoticons[i+1].lower() == "of") or item.lower() in BOOSTER_DICT): sentiments.append(valence) continue 7

Trong Python,

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
7 làm tròn một số cho số nguyên gần nhất, giống như toán tử chém đôi
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1.

Vì vậy,

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
7 là một thay thế cho nhà điều hành
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 vì họ làm điều tương tự đằng sau hậu trường.

Đây là một ví dụ:

import math

num1 = 12
num2 = 5
num3 = num1 // num2
num4 = math.floor(num1 / num2)

print("floor division of", num1, "by", num2, "=", num3)
print("math.floor of", num1, "divided by", num2, "=", num4)

"""
Output:
floor division of 12 by 5 = 2
math.floor of 12 divided by 5 = 2
"""

Bạn có thể thấy rằng

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
7 làm điều tương tự như toán tử
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1.

Cách thức toán tử đôi if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and words_and_emoticons[i+1].lower() == "of") or item.lower() in BOOSTER_DICT): sentiments.append(valence) continue 1 hoạt động đằng sau hậu trường

Khi bạn sử dụng toán tử

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 để chia hai số, phương thức được gọi là phía sau hậu trường là
num1 = 12
num2 = 5
num3 = num1 // num2

print("floor division of", num1, "by", num2, "=", num3)
# Output: floor division of 12 by 5 = 2
5.

Bạn cũng có thể sử dụng phương thức

num1 = 12
num2 = 5
num3 = num1 // num2

print("floor division of", num1, "by", num2, "=", num3)
# Output: floor division of 12 by 5 = 2
5 này trực tiếp thay cho toán tử
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1:

num1 = 12
num2 = 5
num3 = num1 // num2
num4 = num1.__floordiv__(num2)

print("floor division of", num1, "by", num2, "=", num3)
print("using the floordiv method gets us the same value of", num4)

"""
Output:
floor division of 12 by 5 = 2
using the floordiv method gets us the same value of 2
"""

Sự kết luận

Trong bài viết này, bạn đã học được cách bạn có thể sử dụng nhà điều hành Double Slash

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 và cách nó hoạt động đằng sau hậu trường.

Ngoài ra, bạn đã tìm hiểu về hai lựa chọn thay thế của toán tử

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 -
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
7 và phương pháp
num1 = 12
num2 = 5
num3 = num1 // num2

print("floor division of", num1, "by", num2, "=", num3)
# Output: floor division of 12 by 5 = 2
5.

Don Tiết bị nhầm lẫn về việc sử dụng. Ba cách bạn có thể thực hiện phân chia sàn làm việc theo cùng một cách. Nhưng tôi sẽ khuyên bạn nên sử dụng nhà điều hành Double Slash

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue
1 vì bạn có thể gõ ít hơn với nó.

Cảm ơn bạn đã đọc.



Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

'/' Có nghĩa là gì trong chữ ký Python?

Trong trường hợp của __eq __ (tự, giá trị, /), chém ở cuối, điều đó có nghĩa là tất cả các đối số chỉ được đánh dấu là chỉ trong trường hợp của __init__ của bạn, tức là không có gì, chỉ là vị trí.all arguments are marked as positional only while in the case of your __init__ only self, i.e. nothing, is positional only.

Slash về phía trước làm gì trong Python?

Ngôn ngữ lập trình, chẳng hạn như Python, coi một dấu gạch chéo ngược (\) là một nhân vật thoát.Chẳng hạn, \ n đại diện cho nguồn cấp dữ liệu và \ t đại diện cho một tab.Khi chỉ định một đường dẫn, một dấu gạch chéo về phía trước (/) có thể được sử dụng thay cho một dấu gạch chéo ngược.Hai dấu gạch chéo ngược có thể được sử dụng thay vì một để tránh lỗi cú pháp.When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error.