Các toán tử thành viên được sử dụng trong Python là gì?

Toán tử thành viên trong Python được dùng để kiểm tra xem một giá trị có được tìm thấy trong một chuỗi hay không. Ví dụ: bạn có thể sử dụng toán tử thành viên để kiểm tra sự hiện diện của một chuỗi con trong một chuỗi. Có hai toán tử thành viên trong Python
  • in – đánh giá là True nếu giá trị trong toán hạng bên trái xuất hiện trong chuỗi được tìm thấy trong toán hạng bên phải. Ví dụ: 'Xin chào' trong 'Xin chào thế giới' trả về True vì chuỗi con Xin chào có trong chuỗi Xin chào thế giới
  • not in – đánh giá là True nếu giá trị trong toán hạng bên trái không xuất hiện trong chuỗi được tìm thấy trong toán hạng bên phải. Ví dụ: ‘house’ trong ‘Hello world’ trả về True vì chuỗi con house không có trong chuỗi Hello world

    Python cung cấp hai toán tử thành viên để kiểm tra hoặc xác thực tư cách thành viên của một giá trị. Nó kiểm tra tư cách thành viên trong một chuỗi, chẳng hạn như chuỗi, danh sách hoặc bộ dữ liệu.  

    trong toán tử. Toán tử 'in' được sử dụng để kiểm tra xem một ký tự/chuỗi con/phần tử có tồn tại trong một chuỗi hay không. Đánh giá là Đúng nếu nó tìm thấy phần tử được chỉ định trong một chuỗi nếu không thì Sai. Ví dụ,

    'G' in 'GeeksforGeeks'   # Checking 'G' in String
    True
    'g' in 'GeeksforGeeks'   #Checking 'g' in string since Python is case-sensitive,returns False
    False
    'Geeks' in ['Geeks', 'For','Geeks']   #Checking 'Geeks' in list of strings
    True
    10 in [10000,1000,100,10]        #Checking 10 in list of integers
    True
    dict1={1:'Geeks',2:'For',3:'Geeks'}     # Checking 3 in keys of dictionary
    3 in dict1
    True

    Python3




    # Python program to illustrate

    # Finding common member in list

    # using 'in' operator

    list11_______0

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    1
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    2
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    4
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    6
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    8
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    0
    not overlapping
    1

    not overlapping
    2_______1_______0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    1
    not overlapping
    5_______1_______3
    not overlapping
    7
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    9_______1_______3
    x is NOT present in given list
    y is present in given list
    1
    not overlapping
    1

    x is NOT present in given list
    y is present in given list
    3
    x is NOT present in given list
    y is present in given list
    4
    x is NOT present in given list
    y is present in given list
    5
    x is NOT present in given list
    y is present in given list
    6

    x is NOT present in given list
    y is present in given list
    7_______22_______8
    x is NOT present in given list
    y is present in given list
    4
    x is NOT present in given list
    y is present in given list
    5
    True
    1

    True
    2_______32_______3
    True
    4
    True
    5
    True
    6

    x is NOT present in given list
    y is present in given list
    7_______32_______8
    True
    9

    True
    2_______32_______3
    True
    4
    False
    True
    False
    3
    True
    6

    Đầu ra

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping

    Ví dụ tương tự mà không sử dụng toán tử in

    Python3




    # Python program to illustrate

    # Finding common member in list

    False
    True
    False
    7

     

    False
    True
    False
    8

     

     

    False
    True
    False
    9 # Python program to illustrate0

     

    x is NOT present in given list
    y is present in given list
    7_______199_______2
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0 # Python program to illustrate4

    x is NOT present in given list
    y is present in given list
    7_______199_______6
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0 # Python program to illustrate4

    x is NOT present in given list
    y is present in given list
    7_______22_______3 # Finding common member in list1
    x is NOT present in given list
    y is present in given list
    5
    x is NOT present in given list
    y is present in given list
    6

    True
    2_______199_______2____200_______6
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    2

    x is NOT present in given list
    y is present in given list
    7_______22_______3 # Finding common member in list1
    x is NOT present in given list
    y is present in given list
    5
    True
    1

    True
    2_______199_______6# Finding common member in list6
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    2

    x is NOT present in given list
    y is present in given list
    7_______22_______3 # Finding common member in list1
    x is NOT present in given list
    y is present in given list
    5 list13
    True
    4# Python program to illustrate4list16

    True
    2_______22_______3 list19
    x is NOT present in given list
    y is present in given list
    5 list13
    True
    4# Python program to illustrate4
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    04

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    05
    x is NOT present in given list
    y is present in given list
    8
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    07
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    10

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    11
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    12
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    2

    x is NOT present in given list
    y is present in given list
    7_______1_______12 # Python program to illustrate4

     

     

    list11_______0

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    1
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    2
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    4
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    6
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    8
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    0
    not overlapping
    1

    not overlapping
    2_______1_______0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    1
    not overlapping
    5_______1_______3
    not overlapping
    7
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    9_______1_______3
    x is NOT present in given list
    y is present in given list
    1
    not overlapping
    1

    x is NOT present in given list
    y is present in given list
    8
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    42

    x is NOT present in given list
    y is present in given list
    7_______32_______3
    True
    4
    True
    5
    True
    6

    True
    8
    True
    9

    x is NOT present in given list
    y is present in given list
    7_______32_______3
    True
    4
    False
    True
    False
    3
    True
    6

    Đầu ra

    not overlapping

    Toán tử 'không có trong'- Đánh giá là đúng nếu nó không tìm thấy một biến trong chuỗi đã chỉ định và sai nếu ngược lại

    Python3




    # Python program to illustrate

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    56

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    57
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    59

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    60
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    62

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    63
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    1
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    66
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    62
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    70
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    72
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    74
    not overlapping
    1

     

    x is NOT present in given list
    y is present in given list
    8
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    77
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    78
    x is NOT present in given list
    y is present in given list
    5
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    63
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    81

    x is NOT present in given list
    y is present in given list
    7_______32_______3
    True
    4
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    85
    True
    6

    True
    8
    True
    9

    x is NOT present in given list
    y is present in given list
    7_______32_______3
    True
    4
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    92
    True
    6

     

    x is NOT present in given list
    y is present in given list
    8
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    95_______22_______5
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    63_______1_______81

    x is NOT present in given list
    y is present in given list
    7_______32_______3
    True
    4
    not overlapping
    02
    True
    6

    True
    8
    True
    9

    x is NOT present in given list
    y is present in given list
    7_______32_______3
    True
    4
    not overlapping
    09
    True
    6

    Đầu ra

    x is NOT present in given list
    y is present in given list

    Toán tử nhận dạng

    Các toán tử nhận dạng được sử dụng để so sánh các đối tượng nếu cả hai đối tượng thực sự thuộc cùng một kiểu dữ liệu và chia sẻ cùng một vị trí bộ nhớ
    Có các toán tử nhận dạng khác nhau như

    Toán tử 'is' – Đánh giá là True nếu các biến ở hai bên của toán tử trỏ đến cùng một đối tượng và sai nếu ngược lại

    Python3




    not overlapping
    11

    not overlapping
    12

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    57
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    0

    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    60
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    0
    not overlapping
    0

    True
    3
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    not overlapping
    77
    not overlapping
    21
    not overlapping
    22

    not overlapping
    23
    not overlapping
    24

    not overlapping
    23
    not overlapping
    26

    Đầu ra

    True

    Ở đây trong ví dụ đã cho, cả hai biến x và y đều được gán giá trị 5 và cả hai đều chia sẻ cùng một vị trí bộ nhớ, đó là lý do trả về True

    Toán tử thành viên trong Python là gì?

    Toán tử tư cách thành viên là toán tử được sử dụng để xác thực tư cách thành viên của một giá trị . Nó kiểm tra tư cách thành viên trong một chuỗi, chẳng hạn như chuỗi, danh sách hoặc bộ dữ liệu. trong toán tử. Toán tử 'in' được sử dụng để kiểm tra xem một giá trị có tồn tại trong một chuỗi hay không.

    Có bao nhiêu loại toán tử thành viên được sử dụng trong Python?

    Trong Python, có hai toán tử thành viên. (trong, không trong).

    4 toán tử trong Python là gì?

    Toán tử Python .
    toán tử số học
    Toán tử gán
    Toán tử so sánh
    Toán tử logic
    Toán tử nhận dạng
    nhà khai thác thành viên
    Toán tử bitwise