Python-এ if, elif ('else and if'-এর সংক্ষিপ্ত রূপ) এবং else স্টেটমেন্ট দিয়ে প্রোগ্রামের এক্সিকিউশনের প্রবাহ নিয়ন্ত্রণ করা যায় এবং সিদ্ধান্ত নেওয়া যায়।
অন্য অনেক প্রোগ্রামিং ভাষার বিপরীতে, Python 3.9 এবং তার নিচের সংস্করণগুলোতে আনুষ্ঠানিক কোনো কেস-সুইচ স্টেটমেন্ট নেই; বরং একই ধরনের কাজ করতে একাধিক elif স্টেটমেন্ট ব্যবহার করা হয়।
Python 3.10-এ pattern matching নামে এক ধরনের ভিন্ন কেস-সুইচ স্টেটমেন্ট আসে, যা অন্য একটি কনসেপ্টে আলাদাভাবে আলোচনা করা হবে।
কন্ডিশনাল স্টেটমেন্টে এমন এক্সপ্রেশন ব্যবহার করা হয় যাদের ফল অবশ্যই True বা False হতে হবে। সেটি হতে পারে সরাসরি একটি bool রিটার্ন করে, অথবা "ট্রুথি" বা "ফলসি" মূল্যায়ন করে।
x = 10
y = 5
# The comparison '>' returns the bool 'True',
# so the statement is printed.
if x > y:
print("x is greater than y")
...
>>> x is greater than y
if-এর সাথে জুড়ে দিলে, মূল if শর্তটি False-তে মূল্যায়িত হলে একটি ঐচ্ছিক else কোড ব্লক রান হয়:
x = 5
y = 10
# The comparison '>' here returns the bool False,
# so the 'else' block is executed instead of the 'if' block.
if x > y:
print("x is greater than y")
else:
print("y is greater than x")
...
>>> y is greater than x
elif একাধিক মূল্যায়ন বা শাখার সুযোগ দেয়।
x = 5
y = 10
z = 20
# The elif statement allows for the checking of more conditions.
if x > y > z:
print("x is greater than y and z")
elif y > x > z:
print("y is greater than x and z")
else:
print("z is greater than x and y")
...
>>> z is greater than x and y
বুলিয়ান অপারেশন আর তুলনা কন্ডিশনাল স্টেটমেন্টের সাথে মিলিয়ে আরও জটিল পরীক্ষা করা যায়:
>>> def classic_fizzbuzz(number):
... if number % 3 == 0 and number % 5 == 0:
... say = 'FizzBuzz!'
... elif number % 5 == 0:
... say = 'Buzz!'
... elif number % 3 == 0:
... say = 'Fizz!'
... else:
... say = str(number)
...
... return say
>>> classic_fizzbuzz(15)
'FizzBuzz!'
>>> classic_fizzbuzz(13)
'13'
বিকল্প হিসেবে, উপরের উদাহরণটি শুধু if স্টেটমেন্ট আর returns দিয়ে আবার লেখা যেতে পারে। তবে এভাবে আবার লিখলে বোঝা কঠিন হয়ে যেতে পারে যে শর্তগুলো পরস্পর একচেটিয়া হওয়ার কথা, আর এতে ভবিষ্যতে bug বা রক্ষণাবেক্ষণের সমস্যাও আসতে পারে।
>>> def classic_fizzbuzz(number):
... if number % 3 == 0 and number % 5 == 0:
... return 'FizzBuzz!'
... if number % 5 == 0:
... return 'Buzz!'
... if number % 3 == 0:
... return 'Fizz!'
...
... return str(number)
>>> classic_fizzbuzz(15)
'FizzBuzz!'
>>> classic_fizzbuzz(13)
'13'
কন্ডিশনালগুলো নেস্টেডও হতে পারে।
>>> def driving_status(driver_age, test_score):
... if test_score >= 80:
... if 18 > driver_age >= 16:
... status = "Student driver, needs supervision."
... elif driver_age == 18:
... status = "Permitted driver, on probation."
... elif driver_age > 18:
... status = "Fully licensed driver."
... else:
... status = "Unlicensed!"
...
... return status
>>> driving_status(63, 78)
'Unlicensed!'
>>> driving_status(16, 81)
'Student driver, needs supervision.'
>>> driving_status(23, 80)
'Fully licensed driver.'
Python-এ আলাদা কোনো ? টার্নারি অপারেটর না থাকলেও, এক লাইনের conditional expressions লেখা সম্ভব।
এর রূপ হলো <value if True> if <conditional test> else <value if False>।
যেহেতু এই এক্সপ্রেশনগুলো পড়তে কঠিন হয়ে যেতে পারে, তাই এক লাইনের এই রূপটি কেবল তখনই ব্যবহার করার পরামর্শ দেওয়া হয়, যখন এতে code ছোট হয় এবং পড়তে সুবিধা হয়।
>>> def just_the_buzz(number):
... return 'Buzz!' if number % 5 == 0 else str(number)
>>> just_the_buzz(15)
'Buzz!'
>>> just_the_buzz(7)
'7'
Python-এ যেকোনো অবজেক্টের truth value পরীক্ষা করা যায়, তাই এটি কন্ডিশনাল, তুলনা বা বুলিয়ান অপারেশনের সাথে ব্যবহার করা যায়।
এভাবে মূল্যায়িত অবজেক্টগুলোকে "ট্রুথি" বা "ফলসি" ধরা হয় এবং boolean context-এ ব্যবহার করা হয়।
>>> def truthy_test(thing):
... if thing:
... print('This is Truthy.')
... else:
... print("Nope. It's Falsy.")
# Empty container objects are considered Falsy.
>>> truthy_test([])
"Nope. It's Falsy."
>>> truthy_test(['bear', 'pig', 'giraffe'])
'This is Truthy.'
# Empty strings are considered Falsy.
>>> truthy_test('')
"Nope. It's Falsy."
>>> truthy_test('yes')
'This is Truthy.'
# 0 is also considered Falsy.
>>> truthy_test(0)
"Nope. It's Falsy."