Python If ... Else
- পূর্ববর্তী পৃষ্ঠা Python অর্থগত
- পরবর্তী পৃষ্ঠা Python While লোপ
পাইথন শর্ত এবং If বিভাগ
পাইথন গণিত থেকে সাধারণ তত্ত্বগত শর্তগুলি সমর্থন করে:
- সমান হল:
a == b
- বিভিন্ন হল:
a != b
- কম হল:
a < b
- less than or equal:
a <= b
- greater than:
a > b
- greater than or equal:
a >= b
These conditions can be used in many ways, the most common are 'if statements' and loops.
if statements use if
keywords to write.
ইনস্ট্যান্স
If statement:
a = 66 b = 200 if b > a: print("b is greater than a")
In this example, we used two variablesa
and b
as part of the if statement, they are used to test if b is greater than a. Because a is 66 and b is 200, we know that 200 is greater than 66, so we print 'b is greater than a' to the screen.
Indentation
Python indentation depends, uses spaces to define the scope of the code. Other programming languages usually use braces to achieve this purpose.
ইনস্ট্যান্স
No indentation If statement (will cause an error):
a = 66 b = 200 if b > a: print("b is greater than a") # Error will occur
Elif
elif
কীভাবে Python 'যদি পূর্ববর্তী শর্তটি সঠিক নয়, তবে এই শর্তকে চেষ্টা করুন' এর বিকল্প প্রকাশ করে
ইনস্ট্যান্স
a = 66 b = 66 if b > a: print("b is greater than a") elif a == b: print("a and b are equal")
এই উদাহরণেa
সমান b
সুতরাং প্রথম শর্তটি বাস্তব নয়, কিন্তু elif
শর্তটি true, তাই 'a and b are equal' স্ক্রিনে প্রিন্ট করা হবে
Else
else কীভাবে কোনও শর্তকে জুড়ে না পায়, তা ধরে নেয়
ইনস্ট্যান্স
a = 200 b = 66 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b")
এই উদাহরণেa
বড় b
সুতরাং প্রথম শর্তটি বাস্তব নয়elif
শর্তটি সমানভাবেও বাস্তব নয়, তাই else
কোনও শর্ত না থাকলে 'a greater than b' স্ক্রিনে প্রিন্ট করা হবে
আপনি যেমন বিকল্প ব্যবহার করতে পারেন না elif
র else
:
ইনস্ট্যান্স
a = 200 b = 66 if b > a: print("b is greater than a") else: print("b is not greater than a")
If সংক্ষিপ্ত
যদি একটি বিকল্প স্টেটমেন্ট করতে হয়, তবে তা if বিকল্পের সঙ্গে একই লাইনে রাখা যায়。
ইনস্ট্যান্স
একলাইন if বিকল্প:
a = 200 b = 66 if a > b: print("a is greater than b")
If ... Else সংক্ষিপ্ত
যদি একটি বা দুটি বিকল্প স্টেটমেন্ট করতে হয়, একটি if এর জন্য এবং অন্যটি else এর জন্য, তবে তাদের সবকটি একই লাইনে রাখা যায়:
ইনস্ট্যান্স
একলাইন if else বিকল্প:
a = 200 b = 66 print("A") if a > b else print("B")
আপনি একই লাইনে একাধিক else বিকল্প ব্যবহার করতে পারেন:
ইনস্ট্যান্স
একলাইন if else বিকল্প, তিনটি শর্ত আছে:
a = 200 b = 66 যদি a > b তবে print("A") অথবা print("=") যদি a == b তবে print("B")
And
and
Keywords are a logical operator, used to combine condition statements:
ইনস্ট্যান্স
Test if a is greater than b, and if c is greater than a:
a = 200 b = 66 c = 500 if a > b and c > a: print("Both conditions are True")
Or
or
Keywords are also logical operators, used to combine condition statements:
ইনস্ট্যান্স
Test if a is greater than b, or if a is greater than c:
a = 200 b = 66 c = 500 if a > b or a > c: print("At least one of the conditions is True")
নিম্ন স্তরের If
আপনি if বিন্যাসে if বিন্যাস সম্মিলিত করতে পারেন, এটা নামে নিম্ন স্তরের if বিন্যাস
ইনস্ট্যান্স
x = 52 if x > 10: print("Above ten,") if x > 20: print("and also above 20!") else: print("but not above 20.")
pass বিন্যাস
if বিন্যাস খালি হতে পারে না, কিন্তু আপনি কোনও কারণে খালি if বিন্যাস লিখেছেন তবে, pass বিন্যাস ব্যবহার করে ত্রুটি এড়ানো হবে。
ইনস্ট্যান্স
a = 66 b = 200 if b > a: pass
- পূর্ববর্তী পৃষ্ঠা Python অর্থগত
- পরবর্তী পৃষ্ঠা Python While লোপ