Hướng dẫn dấu khác trong python
Toán tử là các biểu tượng cụ thể mà thực hiện một số hoạt động trên một số giá trị và cho ra một kết quả. Ví dụ biểu thức 4 + 5 = 9, trong đó 4 và 5 được gọi là các toán hạng và dấu + được gọi là toán tử. Show Nội dung chính
Python hỗ trợ các loại toán tử sau:
1. Toán tử số học trong PythonGiả sử biến a = 10 và b = 20, ta có:
Ví dụ: #!/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c = a - b print "Line 2 - Value of c is ", c c = a * b print "Line 3 - Value of c is ", c c = a / b print "Line 4 - Value of c is ", c c = a % b print "Line 5 - Value of c is ", c a = 2 b = 3 c = a**b print "Line 6 - Value of c is ", c a = 10 b = 5 c = a//b print "Line 7 - Value of c is ", c Kết quả: Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 8 Line 7 - Value of c is 2 2. Toán tử quan hệ trong PythonPython hỗ trợ các toán tử quan hệ (toán tử so sánh) sau: Giả sử biến a = 10 và b = 20, ta có:
Ví dụ: a = 21 b = 10 c = 0 if ( a == b ): print "Line 1 - a is equal to b" else: print "Line 1 - a is not equal to b" if ( a != b ): print "Line 2 - a is not equal to b" else: print "Line 2 - a is equal to b" if ( a <> b ): print "Line 3 - a is not equal to b" else: print "Line 3 - a is equal to b" if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b" if ( a > b ): print "Line 5 - a is greater than b" else: print "Line 5 - a is not greater than b" a = 5; b = 20; if ( a <= b ): print "Line 6 - a is either less than or equal to b" else: print "Line 6 - a is neither less than nor equal to b" if ( b >= a ): print "Line 7 - b is either greater than or equal to b" else: print "Line 7 - b is neither greater than nor equal to b" Kết quả: Line 1 - a is not equal to b Line 2 - a is not equal to b Line 3 - a is not equal to b Line 4 - a is not less than b Line 5 - a is greater than b Line 6 - a is either less than or equal to b Line 7 - b is either greater than or equal to b 3. Toán tử gán trong PythonPython cung cấp các toán tử gán như sau:
Ví dụ: #!/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2 c %= a print "Line 5 - Value of c is ", c c **= a print "Line 6 - Value of c is ", c c //= a print "Line 7 - Value of c is ", c Kết quả: Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864 4. Toán tử logic trong PythonPython cung cấp các toán tử gán như sau. Giả sử a = 10 và b = 20, ta có:
5. Toán tử membership trong PythonPython cung cấp 2 toán tử membership sau, giả sử x = 10 và y = [10, 20, 30], ta có:
Ví dụ: #!/usr/bin/python a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "Line 1 - a is available in the given list" else: print "Line 1 - a is not available in the given list" if ( b not in list ): print "Line 2 - b is not available in the given list" else: print "Line 2 - b is available in the given list" a = 2 if ( a in list ): print "Line 3 - a is available in the given list" else: print "Line 3 - a is not available in the given list" Kết quả: Line 1 - a is not available in the given list Line 2 - b is not available in the given list Line 3 - a is available in the given list 6. Toán tử nhận dạng (identify) trong PythonToán tử nhận dạng so sánh vị trí bộ nhớ của hai đối tượng. Có hai toán tử nhận dạng được giải thích bên dưới:
Ví dụ: #!/usr/bin/python a = 20 b = 20 if ( a is b ): print "Line 1 - a and b have same identity" else: print "Line 1 - a and b do not have same identity" if ( id(a) == id(b) ): print "Line 2 - a and b have same identity" else: print "Line 2 - a and b do not have same identity" b = 30 if ( a is b ): print "Line 3 - a and b have same identity" else: print "Line 3 - a and b do not have same identity" if ( a is not b ): print "Line 4 - a and b do not have same identity" else: print "Line 4 - a and b have same identity" Kết quả: Line 1 - a and b have same identity Line 2 - a and b have same identity Line 3 - a and b do not have same identity Line 4 - a and b do not have same identity 7. Toán tử thao tác bit trong PythonToán tử thao tác bit làm việc trên các bit và thực hiện các hoạt động theo từng bit. Giả sử a = 60 và b = 13 thì định dạng nhị phân của chúng lần lượt là: a = 0011 1100 b = 0000 1101 ----------------- a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011 Python hỗ trợ các toán tử thao tác bit sau:
Ví dụ: #!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101 print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001 print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011 print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000 print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111 print "Line 6 - Value of c is ", c Kết quả: Line 1 - Value of c is 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is -61 Line 5 - Value of c is 240 Line 6 - Value of c is 15 8. Thứ tự ưu tiên của các toán tử trong PythonBạn cần chú ý thứ tự ưu tiên của các toán tử để mang lại kết quả như mong muốn trong quá trình làm việc. Bảng dưới đây liệt kê tất cả các toán tử trong Python với thứ tự ưu tiên từ cao xuống thấp.
#!/usr/bin/python a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print "Value of (a + b) * c / d is ", e e = ((a + b) * c) / d # (30 * 15 ) / 5 print "Value of ((a + b) * c) / d is ", e e = (a + b) * (c / d); # (30) * (15/5) print "Value of (a + b) * (c / d) is ", e e = a + (b * c) / d; # 20 + (150/5) print "Value of a + (b * c) / d is ", e Kết quả: Value of (a + b) * c / d is 90 Value of ((a + b) * c) / d is 90 Value of (a + b) * (c / d) is 90 Value of a + (b * c) / d is 50 Link tham khảo: https://www.tutorialspoint.com/python/python_basic_operators.htm |