Tiếp theo phần trước,

Bài này cafedev sẽ đề cập thêm về các hàm toán tử thường được sử dụng trong ngôn ngữ lập trình Python.

1. Hàm setitem(ob, pos, val): Hàm này được sử dụng để gán giá trị cho một phần tử nằm tại vị trí cụ thể bên trong mảng.

Phép gán được thực hiện: ob[pos] = val

2. Hàm delitem(ob, pos): Hàm này được sử dụng để xóa đi giá trị của một phần tử nằm tại vị trí cụ thể bên trong mảng.

Phép xóa giá trị được thực hiện: del ob[pos]

3. Hàm getitem(ob, pos): Hàm này được sử dụng để truy cập tới giá trị của một phần tử nằm tại vị trí cụ thể bên trong mảng.

Phép lấy giá trị được thực hiện: ob[pos]

# -----------------------------------------------------------
#Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
#@author cafedevn
#Contact: cafedevn@gmail.com
#Fanpage: https://www.facebook.com/cafedevn
#Instagram: https://instagram.com/cafedevn
#Twitter: https://twitter.com/CafedeVn
#Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
# -----------------------------------------------------------

# Python code to demonstrate working of  
# setitem(), delitem() and getitem() 
  
# importing operator module  
import operator 
  
# Initializing list 
li = [1, 5, 6, 7, 8] 
  
# printing original list  
print ("The original list is : ",end="") 
for i in range(0,len(li)): 
    print (li[i],end=" ") 
  
print ("\r") 
  
# using setitem() to assign 3 at 4th position 
operator.setitem(li,3,3) 
  
# printing modified list after setitem() 
print ("The modified list after setitem() is : ",end="") 
for i in range(0,len(li)): 
    print (li[i],end=" ") 
  
print ("\r") 
  
# using delitem() to delete value at 2nd index 
operator.delitem(li,1) 
  
# printing modified list after delitem() 
print ("The modified list after delitem() is : ",end="") 
for i in range(0,len(li)): 
    print (li[i],end=" ") 
  
print ("\r") 
  
# using getitem() to access 4th element 
print ("The 4th element of list is : ",end="") 
print (operator.getitem(li,3)) 

Kết quả in ra là:

The original list is : 1 5 6 7 8 
The modified list after setitem() is : 1 5 6 3 8 
The modified list after delitem() is : 1 6 3 8 
The 4th element of list is : 8

4. Hàm setitem(ob, slice(a, b), vals): Hàm này được sử dụng để gán các giá trị cho các phần tử nằm trong một khoảng vị trí cụ thể của mảng.

Phép gán giá trị trong khoảng vị trí được thực hiện: obj[a:b] = vals

5. Hàm delitem(ob, slice(a, b)): Hàm này được sử dụng để xóa đi các giá trị của các phần tử nằm trong một khoảng vị trí cụ thể của mảng.

Phép xóa các giá trị trong khoảng vị trí được thực hiện: del obj[a:b]

6. Hàm getitem(ob, slice(a, b)): Hàm này được sử dụng để truy cập tới các giá trị của các phần tử nằm trong một khoảng vị trí cụ thể của mảng.

Phép lấy các giá trị trong khoảng vị trí được thực hiện: obj[a:b]

# -----------------------------------------------------------
#Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
#@author cafedevn
#Contact: cafedevn@gmail.com
#Fanpage: https://www.facebook.com/cafedevn
#Instagram: https://instagram.com/cafedevn
#Twitter: https://twitter.com/CafedeVn
#Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
# -----------------------------------------------------------

# Python code to demonstrate working of  
# setitem(), delitem() and getitem() 
  
# importing operator module  
import operator 
  
# Initializing list 
li = [1, 5, 6, 7, 8] 
  
# printing original list  
print ("The original list is : ",end="") 
for i in range(0,len(li)): 
    print (li[i],end=" ") 
  
print ("\r") 
  
# using setitem() to assign 2,3,4 at 2nd,3rd and 4th index 
operator.setitem(li,slice(1,4),[2,3,4]) 
  
# printing modified list after setitem() 
print ("The modified list after setitem() is : ",end="") 
for i in range(0,len(li)): 
    print (li[i],end=" ") 
  
print ("\r") 
  
# using delitem() to delete value at 3rd and 4th index 
operator.delitem(li,slice(2,4)) 
  
# printing modified list after delitem() 
print ("The modified list after delitem() is : ",end="") 
for i in range(0,len(li)): 
    print (li[i],end=" ") 
  
print ("\r") 
  
# using getitem() to access 1st and 2nd element 
print ("The 1st and 2nd element of list is : ",end="") 
print (operator.getitem(li,slice(0,2))) 

Kết quả in ra là:

The original list is : 1 5 6 7 8 
The modified list after setitem() is : 1 2 3 4 8 
The modified list after delitem() is : 1 2 8 
The 1st and 2nd element of list is : [1, 2]

7. Hàm concat(obj1, obj2): Hàm này được sử dụng để nối hai giá trị của hai biến lại với nhau.

Phép nối hai giá trị được thực hiện: obj1 + obj2.

8. contains(obj1, obj2): Hàm này được sử dụng để kiểm tra xem liệu rằng obj2 có hiện diện bên trong obj1 hay không?

Phép kiểm tra sự có mặt của một đối tượng cụ thể được thực hiện: obj2 in obj1


# Python code to demonstrate working of  
# concat() and contains() 
  
# importing operator module  
import operator 
  
# Initializing string 1 
s1 = "geeksfor"
  
# Initializing string 2 
s2 = "geeks"
  
# using concat() to concatenate two strings 
print ("The concatenated string is : ",end="") 
print (operator.concat(s1,s2)) 
  
# using contains() to check if s1 contains s2 
if (operator.contains(s1,s2)): 
       print ("geeksfor contains geeks") 
else : print ("geeksfor does not contain geeks") 

Kết quả in ra là:

The concatenated string is : geeksforgeeks
geeksfor contains geeks

9. and_(a, b): Hàm này được sử dụng để tính kết quả phép logic AND trên các bits của hai đối số được truyền vào hàm.

Phép toán AND được thực hiện: a & b

10. or_(a, b): Hàm này được sử dụng để tính kết quả của phép logic OR trên các bits của hai đối số được truyền vào hàm.

Phép toán OR được thực hiện: a | b

11. xor(a, b): Hàm này được sử dụng để tính kết quả của phép logic XOR trên các bits của hai đối số được truyền vào hàm.

Phép toán XOR được thực hiện: a ^ b

12. invert(a): Hàm này được sử dụng để tính kết quả của phép nghịch đảo các bits của một đối số cụ thể được truyền vào hàm.

Phép tính nghịch đảo bits được thực hiện: ~ a

Đoạn code mô tả cách thức hoạt động của hàm and_(), hàm or_(), hàm xor() và hàm invert()

# -----------------------------------------------------------
#Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
#@author cafedevn
#Contact: cafedevn@gmail.com
#Fanpage: https://www.facebook.com/cafedevn
#Instagram: https://instagram.com/cafedevn
#Twitter: https://twitter.com/CafedeVn
#Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
# -----------------------------------------------------------


# Python code to demonstrate working of  
# and_(), or_(), xor(), invert() 
  
# importing operator module  
import operator 
  
# Initializing a and b 
  
a = 1
  
b = 0
  
# using and_() to display bitwise and operation 
print ("The bitwise and of a and b is : ",end="") 
print (operator.and_(a,b)) 
  
# using or_() to display bitwise or operation 
print ("The bitwise or of a and b is : ",end="") 
print (operator.or_(a,b)) 
  
# using xor() to display bitwise exclusive or operation 
print ("The bitwise xor of a and b is : ",end="") 
print (operator.xor(a,b)) 
  
# using invert() to invert value of a 
operator.invert(a) 
  
# printing modified value 
print ("The inverted value of a is : ",end="") 
print (a) 

Kết quả in ra là:

The bitwise and of a and b is : 0
The bitwise or of a and b is : 1
The bitwise xor of a and b is : 1
The inverted value of a is : 1

Nguồn và Tài liệu tiếng anh tham khảo:

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

Chào thân ái và quyết thắng!

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!