লজিকাল অপারেটরস

অপারেটরস এবং এক্সপ্রেশন - পাইথন প্রোগ্রামিং (Python Programming) - Computer Programming

452

পাইথনে লজিকাল অপারেটর ব্যবহার করে বিভিন্ন শর্ত একত্রে যাচাই করা যায়। লজিকাল অপারেটরগুলো সাধারণত শর্তভিত্তিক স্টেটমেন্ট যেমন if এবং while-এ ব্যবহৃত হয়। পাইথনে তিনটি লজিকাল অপারেটর রয়েছে: and, or, এবং not। নিচে প্রতিটি লজিকাল অপারেটর এবং তাদের উদাহরণ দেওয়া হলো।


১. and অপারেটর

and অপারেটর ব্যবহার করে দুটি শর্তকে একসাথে যাচাই করা হয়। যদি উভয় শর্তই সত্য হয়, তাহলে এটি True রিটার্ন করে; অন্যথায় False

উদাহরণ:

x = 5
y = 10
z = 15

if x < y and y < z:
    print("Both conditions are true.")
# আউটপুট: Both conditions are true.

২. or অপারেটর

or অপারেটর ব্যবহার করে দুটি শর্তের যেকোনো একটি সত্য হলেই True রিটার্ন করে। উভয় শর্তই মিথ্যা হলে এটি False রিটার্ন করে।

উদাহরণ:

x = 5
y = 10
z = 15

if x > y or y < z:
    print("At least one condition is true.")
# আউটপুট: At least one condition is true.

৩. not অপারেটর

not অপারেটর একটি শর্তের বিপরীত মান প্রদান করে। যদি শর্ত True হয়, তাহলে not সেটিকে False করে এবং যদি শর্ত False হয়, তাহলে সেটিকে True করে।

উদাহরণ:

x = 5
y = 10

if not x > y:
    print("Condition is false, but `not` makes it true.")
# আউটপুট: Condition is false, but `not` makes it true.

উদাহরণ সমন্বিত কোড

age = 20
is_student = True

# and অপারেটর
if age > 18 and is_student:
    print("The person is an adult student.")
# আউটপুট: The person is an adult student.

# or অপারেটর
if age > 18 or is_student:
    print("The person is either an adult or a student.")
# আউটপুট: The person is either an adult or a student.

# not অপারেটর
if not is_student:
    print("The person is not a student.")
else:
    print("The person is a student.")
# আউটপুট: The person is a student.

সারসংক্ষেপ

লজিকাল অপারেটর and, or, এবং not পাইথনে বিভিন্ন শর্ত একত্রে যাচাই করতে এবং আরও জটিল লজিক তৈরি করতে সহায়ক।

Content added By
Promotion

Are you sure to start over?

Loading...