លំហសិក្សាធិការកម្ពុជា លំហសិក្សាធិការកម្ពុជា V1.0
ចូល ចុះឈ្មោះ
14 Jul 2026 01:48 UTC បានកែប្រែ 28 Jul 2026 08:00 UTC 22 ដងមើល 0 មតិ personal

លំហាត់៖ ការប្រើប្រតិបត្តិករសមាជិកភាព (Membership Operators) ក្នុង Python

ប្រតិបត្តិករសមាជិកភាពក្នុង Python មាន in និង not in ដែលប្រើសម្រាប់ពិនិត្យថា តម្លៃ ធាតុ ឬពាក្យណាមួយមាន ឬមិនមាននៅក្នុង String, List, Tuple, Set ឬ Dictionary។ in ផ្ដល់លទ្ធផល True នៅពេលរកឃើញតម្លៃ ខណៈ not in ផ្ដល់លទ្ធផល True នៅពេលតម្លៃនោះមិនមាន។ នៅពេលប្រើជាមួយ Dictionary វាពិនិត្យលើ Key ជាមូលដ្ឋាន ហើយអាចប្រើ .values() ដើម្បីពិនិត្យតម្លៃ។ ប្រតិបត្តិករទាំងនេះត្រូវបានប្រើជាញឹកញាប់ជាមួយ if, else និងប្រតិបត្តិករតក្កវិទ្យា ដើម្បីពិនិត្យឈ្មោះ សិទ្ធិអ្នកប្រើ វគ្គសិក្សា និងទិន្នន័យផ្សេងៗក្នុងកម្មវិធី។

១. សេចក្ដីផ្ដើម

ក្នុងការសរសេរកម្មវិធី យើងតែងតែត្រូវពិនិត្យថា តម្លៃ ធាតុ ពាក្យ ឬទិន្នន័យណាមួយមាននៅក្នុងបណ្ដុំទិន្នន័យដែរឬទេ។ ឧទាហរណ៍ កម្មវិធីអាចពិនិត្យថា ឈ្មោះសិស្សមាននៅក្នុងបញ្ជីឈ្មោះ ពាក្យមួយមាននៅក្នុងអត្ថបទ លេខមួយមាននៅក្នុងបណ្ដុំលេខ ឬឈ្មោះអ្នកប្រើមាននៅក្នុងប្រព័ន្ធដែរឬទេ។

Python ប្រើ ប្រតិបត្តិករសមាជិកភាព (Membership Operators) ដើម្បីពិនិត្យថា តម្លៃមួយជាសមាជិក ឬមិនមែនជាសមាជិកនៃបណ្ដុំទិន្នន័យ។ ប្រតិបត្តិករសមាជិកភាពមានពីរគឺ៖

  • in
  • not in

លទ្ធផលនៃការពិនិត្យតែងតែជា TrueFalse


២. និយមន័យប្រតិបត្តិករសមាជិកភាព

ប្រតិបត្តិករសមាជិកភាព គឺជាប្រតិបត្តិករដែលប្រើសម្រាប់ពិនិត្យថា តម្លៃ ឬធាតុណាមួយមាន ឬមិនមាននៅក្នុងបណ្ដុំទិន្នន័យ ដូចជា៖

  • ខ្សែអក្សរ String
  • បញ្ជី List
  • Tuple
  • Set
  • Dictionary

ឧទាហរណ៍៖

fruits = ["apple", "banana", "mango"]

print("banana" in fruits)

កម្មវិធីពិនិត្យថា "banana" មាននៅក្នុងបញ្ជី fruits ដែរឬទេ។ ដោយសារវាមាននៅក្នុងបញ្ជី លទ្ធផលគឺ៖

True


៣. តារាងប្រតិបត្តិករសមាជិកភាព

ប្រតិបត្តិករអត្ថន័យឧទាហរណ៍លទ្ធផល
inពិនិត្យថាតម្លៃមានក្នុងបណ្ដុំ"apple" in fruitsTrue
not inពិនិត្យថាតម្លៃមិនមានក្នុងបណ្ដុំ"orange" not in fruitsTrue

៤. ប្រតិបត្តិករ in

៤.១ និយមន័យ

ប្រតិបត្តិករ in ប្រើសម្រាប់ពិនិត្យថា តម្លៃមួយមាននៅក្នុងបណ្ដុំទិន្នន័យដែរឬទេ។

  • បើមាន លទ្ធផលជា True
  • បើមិនមាន លទ្ធផលជា False

ឧទាហរណ៍៖

numbers = [10, 20, 30, 40]

print(20 in numbers)
print(50 in numbers)

លទ្ធផល៖

True
False

៥. ប្រតិបត្តិករ not in

៥.១ និយមន័យ

ប្រតិបត្តិករ not in ប្រើសម្រាប់ពិនិត្យថា តម្លៃមួយមិនមាននៅក្នុងបណ្ដុំទិន្នន័យដែរឬទេ។

  • បើមិនមាន លទ្ធផលជា True
  • បើមាន លទ្ធផលជា False

ឧទាហរណ៍៖

numbers = [10, 20, 30, 40]

print(50 not in numbers)
print(20 not in numbers)

លទ្ធផល៖

True
False

៦. ការប្រើជាមួយខ្សែអក្សរ String

ប្រតិបត្តិករសមាជិកភាពអាចប្រើសម្រាប់ពិនិត្យថា តួអក្សរ ឬពាក្យមួយមាននៅក្នុងខ្សែអក្សរដែរឬទេ។

message = "Python programming is easy"

print("Python" in message)
print("Java" in message)

លទ្ធផល៖

True
False

៦.១ ពិនិត្យតួអក្សរ

word = "Cambodia"

print("C" in word)
print("z" in word)

លទ្ធផល៖

True
False

៦.២ អក្សរធំ និងអក្សរតូច

Python គិតគូរពីភាពខុសគ្នារវាងអក្សរធំ និងអក្សរតូច។

text = "Python"

print("Python" in text)
print("python" in text)

លទ្ធផល៖

True
False

ដើម្បីមិនគិតពីអក្សរធំ និងអក្សរតូច អាចប្រើ .lower()

text = "Python Programming"

print("python" in text.lower())

លទ្ធផល៖

True

៧. ការប្រើជាមួយ List

List គឺជាបណ្ដុំទិន្នន័យដែលអាចរក្សាទុកធាតុច្រើន។

students = ["Dara", "Sokha", "Vanna", "Sreyneang"]

print("Sokha" in students)
print("Bopha" in students)

លទ្ធផល៖

True
False

៧.១ ប្រើជាមួយលក្ខខណ្ឌ

students = ["Dara", "Sokha", "Vanna", "Sreyneang"]
name = input("Enter student name: ")

if name in students:
    print("The student is registered.")
else:
    print("The student is not registered.")

កម្មវិធីទទួលឈ្មោះពីអ្នកប្រើ ហើយពិនិត្យថា ឈ្មោះនោះមាននៅក្នុងបញ្ជី students ដែរឬទេ។


៨. ការប្រើជាមួយ Tuple

Tuple ជាបណ្ដុំទិន្នន័យដែលស្រដៀងនឹង List ប៉ុន្តែតម្លៃរបស់វាមិនអាចកែប្រែដោយផ្ទាល់បាន។

colors = ("red", "green", "blue")

print("green" in colors)
print("yellow" not in colors)

លទ្ធផល៖

True
True

៩. ការប្រើជាមួយ Set

Set ជាបណ្ដុំទិន្នន័យដែលមិនអនុញ្ញាតឱ្យមានតម្លៃស្ទួន។

allowed_roles = {"admin", "teacher", "student"}

print("teacher" in allowed_roles)
print("guest" in allowed_roles)

លទ្ធផល៖

True
False

ឧទាហរណ៍ពិនិត្យសិទ្ធិអ្នកប្រើ៖

allowed_roles = {"admin", "teacher", "student"}
user_role = input("Enter your role: ").lower()

if user_role in allowed_roles:
    print("Access allowed.")
else:
    print("Access denied.")

១០. ការប្រើជាមួយ Dictionary

Dictionary រក្សាទុកទិន្នន័យជាគូ key: value

student = {
    "name": "Dara",
    "age": 20,
    "major": "Computer Science"
}

print("name" in student)
print("score" in student)

លទ្ធផល៖

True
False

១០.១ ចំណុចសំខាន់

នៅពេលប្រើ in ជាមួយ Dictionary Python ពិនិត្យលើ Key មិនមែន Value ទេ។

student = {
    "name": "Dara",
    "age": 20
}

print("name" in student)
print("Dara" in student)

លទ្ធផល៖

True
False

ដើម្បីពិនិត្យ Value ត្រូវប្រើ .values()

print("Dara" in student.values())

លទ្ធផល៖

True

ដើម្បីពិនិត្យ Key និង Value ជាគូ អាចប្រើ .items()

print(("name", "Dara") in student.items())

លទ្ធផល៖

True

១១. ការប្រើប្រតិបត្តិករសមាជិកភាពជាមួយលក្ខខណ្ឌ

ប្រតិបត្តិករសមាជិកភាពត្រូវបានប្រើជាញឹកញាប់ជាមួយ if, elif និង else

ឧទាហរណ៍៖

valid_courses = ["Python", "Java", "Database", "Networking"]

course = input("Enter a course name: ")

if course in valid_courses:
    print("The course is available.")
else:
    print("The course is not available.")

១២. កូដលំហាត់ពេញលេញ

កម្មវិធីខាងក្រោមពិនិត្យព័ត៌មានសិស្ស វគ្គសិក្សា និងសិទ្ធិអ្នកប្រើ ដោយប្រើ in និង not in

# Python Membership Operators Exercise

registered_students = [
    "Dara",
    "Sokha",
    "Vanna",
    "Sreyneang"
]

available_courses = (
    "Python",
    "Database",
    "Web Development",
    "Networking"
)

allowed_roles = {
    "admin",
    "teacher",
    "student"
}

student_information = {
    "name": "Dara",
    "age": 20,
    "major": "Computer Science"
}

student_name = input("Enter student name: ").strip()
course_name = input("Enter course name: ").strip()
user_role = input("Enter user role: ").strip().lower()

print("\n=== Membership Results ===")

student_registered = student_name in registered_students
course_available = course_name in available_courses
role_allowed = user_role in allowed_roles
has_major_field = "major" in student_information
has_score_field = "score" in student_information

print("Student registered:", student_registered)
print("Course available:", course_available)
print("Role allowed:", role_allowed)
print("Major field exists:", has_major_field)
print("Score field exists:", has_score_field)

print("\n=== Final Decisions ===")

if student_name in registered_students:
    print(student_name, "is a registered student.")
else:
    print(student_name, "is not a registered student.")

if course_name in available_courses:
    print(course_name, "is available.")
else:
    print(course_name, "is not available.")

if user_role in allowed_roles:
    print("The user role is allowed.")
else:
    print("The user role is not allowed.")

if "score" not in student_information:
    print("The score information has not been added.")

១៣. ការពន្យល់កូដតាមផ្នែក

១៣.១ បង្កើតបញ្ជីសិស្ស

registered_students = [
    "Dara",
    "Sokha",
    "Vanna",
    "Sreyneang"
]

អញ្ញតិ registered_students ជា List ដែលរក្សាទុកឈ្មោះសិស្សដែលបានចុះឈ្មោះ។


១៣.២ បង្កើតបណ្ដុំវគ្គសិក្សា

available_courses = (
    "Python",
    "Database",
    "Web Development",
    "Networking"
)

អញ្ញតិ available_courses ជា Tuple ដែលរក្សាទុកឈ្មោះវគ្គសិក្សាដែលមាន។


១៣.៣ បង្កើតបណ្ដុំតួនាទី

allowed_roles = {
    "admin",
    "teacher",
    "student"
}

អញ្ញតិ allowed_roles ជា Set ដែលរក្សាទុកតួនាទីដែលមានសិទ្ធិប្រើប្រព័ន្ធ។


១៣.៤ បង្កើត Dictionary

student_information = {
    "name": "Dara",
    "age": 20,
    "major": "Computer Science"
}

Dictionary នេះរក្សាទុកព័ត៌មានសិស្សជាគូ Key និង Value។


១៣.៥ ទទួលទិន្នន័យពីអ្នកប្រើ

student_name = input("Enter student name: ").strip()
course_name = input("Enter course name: ").strip()
user_role = input("Enter user role: ").strip().lower()
  • input() ទទួលទិន្នន័យពីអ្នកប្រើ។
  • .strip() ដកចន្លោះទទេនៅខាងដើម និងខាងចុង។
  • .lower() បម្លែងតួនាទីទៅជាអក្សរតូច។

១៣.៦ ពិនិត្យសមាជិកភាព

student_registered = student_name in registered_students

ពិនិត្យថា ឈ្មោះសិស្សមាននៅក្នុងបញ្ជីដែរឬទេ។

course_available = course_name in available_courses

ពិនិត្យថា វគ្គសិក្សាមាននៅក្នុង Tuple ដែរឬទេ។

role_allowed = user_role in allowed_roles

ពិនិត្យថា តួនាទីអ្នកប្រើមាននៅក្នុង Set ដែរឬទេ។


១៣.៧ ពិនិត្យ Key ក្នុង Dictionary

has_major_field = "major" in student_information
has_score_field = "score" in student_information

បន្ទាត់ទាំងនេះពិនិត្យថា Key "major" និង "score" មាននៅក្នុង Dictionary ដែរឬទេ។


១៣.៨ ប្រើ not in

if "score" not in student_information:
    print("The score information has not been added.")

បន្ទាត់នេះពិនិត្យថា Key "score" មិនមានក្នុង Dictionary។ បើមិនមាន កម្មវិធីបង្ហាញសារថា មិនទាន់បានបញ្ចូលព័ត៌មានពិន្ទុ។


១៤. ឧទាហរណ៍នៃការដំណើរការ

អ្នកប្រើបញ្ចូល៖

Enter student name: Dara
Enter course name: Python
Enter user role: Student

លទ្ធផល៖

=== Membership Results ===
Student registered: True
Course available: True
Role allowed: True
Major field exists: True
Score field exists: False

=== Final Decisions ===
Dara is a registered student.
Python is available.
The user role is allowed.
The score information has not been added.

១៥. ការប្រើជាមួយប្រតិបត្តិករតក្កវិទ្យា

ប្រតិបត្តិករសមាជិកភាពអាចប្រើជាមួយ and, or និង not

registered_students = ["Dara", "Sokha", "Vanna"]
available_courses = ["Python", "Database"]

student_name = "Dara"
course_name = "Python"

if student_name in registered_students and course_name in available_courses:
    print("The student can enroll in the course.")
else:
    print("Enrollment is not allowed.")

លក្ខខណ្ឌនេះពិត លុះត្រាតែ៖

  • សិស្សមានក្នុងបញ្ជី
  • និងវគ្គសិក្សាមានក្នុងបញ្ជីវគ្គដែលអាចចុះឈ្មោះបាន

១៦. ការប្រើ not in សម្រាប់ការពារទិន្នន័យស្ទួន

usernames = ["admin", "dara", "sokha"]

new_username = input("Create a username: ").lower()

if new_username not in usernames:
    usernames.append(new_username)
    print("Username created successfully.")
else:
    print("The username already exists.")

កម្មវិធីនេះបន្ថែមឈ្មោះអ្នកប្រើថ្មី លុះត្រាតែឈ្មោះនោះមិនទាន់មានក្នុងបញ្ជី។


១៧. កំហុសដែលតែងតែកើតមាន

១៧.១ ច្រឡំរវាង in និង ==

name == students

កូដនេះប្រៀបធៀបតម្លៃ name ជាមួយ List ទាំងមូល។

កូដត្រឹមត្រូវ៖

name in students

កូដនេះពិនិត្យថា name មាននៅក្នុង List ដែរឬទេ។


១៧.២ មិនគិតពីអក្សរធំ និងតូច

students = ["Dara", "Sokha"]

print("dara" in students)

លទ្ធផល៖

False

ដោយសារ "dara" និង "Dara" ខុសគ្នា។

អាចដោះស្រាយដោយ៖

students = ["dara", "sokha"]
name = input("Enter name: ").lower()

print(name in students)

១៧.៣ ពិនិត្យ Value ក្នុង Dictionary ដោយផ្ទាល់

student = {"name": "Dara"}

print("Dara" in student)

លទ្ធផល៖

False

ដោយសារ Python ពិនិត្យ Key។

កូដត្រឹមត្រូវសម្រាប់ Value៖

print("Dara" in student.values())

១៧.៤ ប្រើ in ជាមួយតម្លៃដែលមិនអាចស្វែងរកបាន

number = 100

print(1 in number)

កូដនេះបង្កើតកំហុស ព្រោះចំនួនគត់មិនមែនជាបណ្ដុំទិន្នន័យដែលអាចស្វែងរកសមាជិកបាន។


១៨. លំហាត់អនុវត្តបន្ថែម

លំហាត់ទី១៖ ពិនិត្យផ្លែឈើ

បង្កើត List ផ្លែឈើ ហើយទទួលឈ្មោះផ្លែឈើពីអ្នកប្រើ។ បង្ហាញថា ផ្លែឈើនោះមាន ឬមិនមានក្នុងបញ្ជី។

លំហាត់ទី២៖ ពិនិត្យថ្ងៃឈប់សម្រាក

បង្កើត Tuple ដែលមាន "Saturday" និង "Sunday"។ ទទួលឈ្មោះថ្ងៃ ហើយពិនិត្យថា ជាថ្ងៃឈប់សម្រាកដែរឬទេ។

លំហាត់ទី៣៖ ពិនិត្យតួនាទី

បង្កើត Set ដែលមានតួនាទី admin, teacher និង student។ ទទួលតួនាទីពីអ្នកប្រើ ហើយពិនិត្យសិទ្ធិចូលប្រព័ន្ធ។

លំហាត់ទី៤៖ ពិនិត្យ Key ក្នុង Dictionary

បង្កើត Dictionary ព័ត៌មានសិស្ស ហើយពិនិត្យថា Key "email" មាននៅក្នុង Dictionary ដែរឬទេ។

លំហាត់ទី៥៖ ការពារឈ្មោះស្ទួន

បង្កើតបញ្ជីឈ្មោះអ្នកប្រើ ហើយទទួលឈ្មោះថ្មី។ ប្រើ not in ដើម្បីអនុញ្ញាតឱ្យបន្ថែមតែឈ្មោះដែលមិនទាន់មាន។


១៩. សំណួរត្រួតពិនិត្យការយល់ដឹង

  1. តើប្រតិបត្តិករសមាជិកភាពមានតួនាទីអ្វី?
  2. តើប្រតិបត្តិករសមាជិកភាពក្នុង Python មានអ្វីខ្លះ?
  3. តើ in ផ្ដល់លទ្ធផល True នៅពេលណា?
  4. តើ not in ផ្ដល់លទ្ធផល True នៅពេលណា?
  5. តើប្រតិបត្តិករសមាជិកភាពអាចប្រើជាមួយប្រភេទទិន្នន័យអ្វីខ្លះ?
  6. តើ "a" in "Cambodia" ផ្ដល់លទ្ធផលអ្វី?
  7. តើ in ពិនិត្យអ្វី នៅពេលប្រើជាមួយ Dictionary?
  8. តើត្រូវប្រើអ្វីដើម្បីពិនិត្យ Value ក្នុង Dictionary?
  9. តើ "Python" in ["Python", "Java"] ផ្ដល់លទ្ធផលអ្វី?
  10. តើការប្រើ .lower() មានប្រយោជន៍អ្វីក្នុងការពិនិត្យសមាជិកភាព?

២០. សន្និដ្ឋាន

ប្រតិបត្តិករសមាជិកភាពក្នុង Python រួមមាន in និង not in ដែលប្រើសម្រាប់ពិនិត្យថា តម្លៃ ធាតុ ឬពាក្យណាមួយមាន ឬមិនមាននៅក្នុងខ្សែអក្សរ List, Tuple, Set ឬ Dictionary។ in ផ្ដល់លទ្ធផល True នៅពេលរកឃើញតម្លៃក្នុងបណ្ដុំ ខណៈ not in ផ្ដល់លទ្ធផល True នៅពេលតម្លៃនោះមិនមាន។ នៅពេលប្រើជាមួយ Dictionary ប្រតិបត្តិករទាំងនេះពិនិត្យលើ Key ជាមូលដ្ឋាន ហើយត្រូវប្រើ .values() ប្រសិនបើចង់ពិនិត្យតម្លៃ។ ប្រតិបត្តិករសមាជិកភាពត្រូវបានប្រើជាញឹកញាប់ជាមួយ if, else, and, or និង not ដើម្បីពិនិត្យបញ្ជីឈ្មោះ សិទ្ធិអ្នកប្រើ វគ្គសិក្សា ទិន្នន័យស្ទួន និងព័ត៌មានផ្សេងៗក្នុងកម្មវិធី។

មតិយោបល់ 0
ចូលគណនី ដើម្បីបញ្ចេញមតិ

មិនទាន់មានមតិយោបល់ — ក្លាយជាអ្នកដំបូង!