if-else Statement

if-else Statement

Python లో ఒక condition ఆధారంగా రెండు అవకాశాలలో ఒకదాన్ని ఎంచుకోవడానికి if-else statement ఉపయోగిస్తాము.

Syntax

if condition:
    statement_if_true
else:
    statement_if_false

Condition True అయితే if block అమలు అవుతుంది. Condition False అయితే else block అమలు అవుతుంది. ఒకేసారి if block లేదా else block లో ఒకటే అమలు అవుతుంది.

ఎందుకు ఉపయోగిస్తాము?

ఒక నిర్ణయం తీసుకోవాల్సినప్పుడు if-else ఉపయోగకరంగా ఉంటుంది. ఉదాహరణకు, వయస్సు 18 లేదా అంతకంటే ఎక్కువ ఉంటే ఓటు వేయవచ్చు. లేకపోతే ఓటు వేయలేరు.

ముఖ్యమైన నియమాలు

  • if మరియు else తర్వాత colon : ఉపయోగించాలి.
  • if మరియు else లోని statements indentation తో రాయాలి.
  • else కి condition రాయరు.
  • else, అదే if statement కు చెందినదిగా ఉండాలి.

Simple Explanation

if-else ను ఒక ప్రశ్నకు రెండు సమాధానాలుగా ఊహించుకోవచ్చు. Condition నిజమైతే if లోని code నడుస్తుంది. Condition నిజం కాకపోతే else లోని code నడుస్తుంది.

ఉదాహరణకు age >= 18 అనే condition తీసుకుందాం. ఇది True అయితే Adult అని print అవుతుంది. ఇది False అయితే Minor అని print అవుతుంది.

Python ముందుగా condition ను check చేస్తుంది. తరువాత True లేదా False ఫలితాన్ని బట్టి సరైన block ను మాత్రమే execute చేస్తుంది.

Real-Life Example

Real-Life Example

వర్షం పడుతుందా అని చూసి నిర్ణయం తీసుకోవడం ఒక ఉదాహరణ. వర్షం పడితే గొడుగు తీసుకెళ్తాము. వర్షం పడకపోతే గొడుగు తీసుకెళ్లము.

ఇక్కడ వర్షం పడుతుందా అనే విషయం condition. వర్షం పడితే ఒక పని, లేకపోతే మరో పని జరుగుతుంది. Python లో ఇదే నిర్ణయాన్ని if-else statement తో రాస్తాము.

Code Examples

వయస్సు ఆధారంగా నిర్ణయం

age = 20

if age >= 18:
    print("You can vote")
else:
    print("You cannot vote")

ఈ code లో age విలువ 20. 20, 18 కంటే ఎక్కువ కాబట్టి condition True అవుతుంది. అందువల్ల if block లోని message print అవుతుంది. else block అమలు కాదు.

You can vote

User input తో if-else

age = int(input("Enter your age: "))

if age >= 18:
    print("You are an adult")
else:
    print("You are a minor")

ఈ code user నుండి age తీసుకుంటుంది. input() ద్వారా వచ్చిన text ను int() తో integer గా మార్చి condition check చేస్తుంది. Age 18 లేదా అంతకంటే ఎక్కువైతే adult message, లేకపోతే minor message కనిపిస్తుంది.

Enter your age: 16
You are a minor

Line-by-Line Explanation

age = 20
ఈ line లో age అనే variable లో 20 విలువ నిల్వ అవుతుంది. తరువాత condition check చేయడానికి ఈ విలువను Python ఉపయోగిస్తుంది.
if age >= 18:
ఇక్కడ age విలువ 18 కంటే ఎక్కువా లేదా సమానమా అని Python check చేస్తుంది. Condition True అయితే తరువాతి indented line అమలు అవుతుంది. Colon ఈ if block ప్రారంభాన్ని సూచిస్తుంది.
print(“You can vote”)
Condition True అయినప్పుడు ఈ line execute అవుతుంది. User కు ఓటు వేయవచ్చని message చూపిస్తుంది. Indentation వల్ల ఈ line if block లో భాగమని తెలుస్తుంది.
else:
if condition False అయినప్పుడు ఏ code అమలు చేయాలో else సూచిస్తుంది. else కి ప్రత్యేక condition అవసరం లేదు.
print(“You cannot vote”)
age 18 కంటే తక్కువగా ఉన్నప్పుడు ఈ line execute అవుతుంది. User కు ఓటు వేయలేరని message చూపిస్తుంది.
age = int(input(“Enter your age: “))
input() user నుండి age తీసుకుంటుంది. input() సాధారణంగా text ఇస్తుంది కాబట్టి int() ఉపయోగించి దాన్ని integer గా మార్చి age variable లో నిల్వ చేస్తాము.
if age >= 18:
User ఇచ్చిన age 18 కంటే ఎక్కువా లేదా సమానమా అని Python check చేస్తుంది. True అయితే adult message ఉన్న block అమలు అవుతుంది.
print(“You are an adult”)
Condition True అయినప్పుడు ఈ line adult message ను print చేస్తుంది.
else:
Age condition False అయినప్పుడు else block ను అమలు చేయమని Python కు చెబుతుంది.
print(“You are a minor”)
User ఇచ్చిన age 18 కంటే తక్కువగా ఉంటే ఈ line minor message ను print చేస్తుంది.

Try It

Try this Python code:

Example

number = 7

if number % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

Try It Yourself »

Practice

Practice: User నుండి temperature ను తీసుకుని, temperature 30 లేదా అంతకంటే ఎక్కువ ఉంటే “It is hot” అని, లేకపోతే “It is pleasant” అని print చేయండి.

Hint: temperature >= 30 అనే condition ను if లో ఉపయోగించండి. User input ను integer గా మార్చడానికి int() ఉపయోగించండి.

Practice: ఒక number ను తీసుకుని, అది 100 లేదా అంతకంటే ఎక్కువ అయితే “Large number” అని, లేకపోతే “Small number” అని print చేయండి.

Hint: number >= 100 అనే condition ను ఉపయోగించండి.

Common Mistakes

if condition తర్వాత colon : మర్చిపోవడం.
ప్రతి if మరియు else statement చివర colon ఉంచాలి. ఉదాహరణ: if age >= 18:
if లేదా else block లో indentation ఇవ్వకపోవడం.
Block లోని code ను సాధారణంగా నాలుగు spaces indentation తో రాయాలి.
else తర్వాత condition రాయడం.
else కి condition ఉండదు. సరైన syntax else:
Comparison కోసం = ఉపయోగించడం.
విలువలు compare చేయడానికి == ఉపయోగించాలి. Variable కి విలువ ఇవ్వడానికి మాత్రమే = ఉపయోగించాలి.
if మరియు else blocks రెండూ execute అవుతాయని అనుకోవడం.
ఒక condition ఆధారంగా if లేదా else లో ఒక block మాత్రమే execute అవుతుంది.

Interview Questions

if-else statement అంటే ఏమిటి?

Answer: ఒక condition True అయితే ఒక block, False అయితే మరో block అమలు చేయడానికి ఉపయోగించే conditional statement ను if-else statement అంటారు.

else కి condition ఎందుకు ఉండదు?

Answer: if condition False అయినప్పుడు మిగిలిన ప్రత్యామ్నాయాన్ని else సూచిస్తుంది. అందువల్ల else కి ప్రత్యేక condition అవసరం లేదు.

if-else లో ఎన్ని blocks execute అవుతాయి?

Answer: Condition ఫలితాన్ని బట్టి if block లేదా else block లో ఒకటి మాత్రమే execute అవుతుంది.

Python లో indentation ఎందుకు ముఖ్యమైనది?

Answer: ఏ statements if లేదా else block లో భాగమో indentation ద్వారా Python గుర్తిస్తుంది.

Exam Questions

క్రింది code యొక్క output ఏమిటి?

score = 40
if score >= 35:
print(“Pass”)
else:
print(“Fail”)

Answer:

Pass

if-else statement యొక్క basic syntax రాయండి.

Answer:

if condition:
statement_if_true
else:
statement_if_false

క్రింది code లో తప్పు ఏమిటి?

age = 20
if age >= 18
print(“Adult”)

Answer:

if condition చివర colon లేదు. సరైన code: if age >= 18:

ఒక సంఖ్య positive అయితే “Positive” లేకపోతే “Not positive” అని చూపించే logic రాయండి.

Answer:

number = 5
if number > 0:
print(“Positive”)
else:
print(“Not positive”)

Quick Revision

  • if-else రెండు అవకాశాలలో ఒకదాన్ని ఎంచుకోవడానికి ఉపయోగిస్తాము.
  • Condition True అయితే if block execute అవుతుంది.
  • Condition False అయితే else block execute అవుతుంది.
  • if మరియు else తర్వాత colon తప్పనిసరిగా ఉండాలి.
  • if మరియు else blocks లో indentation తప్పనిసరి.
  • else కి ప్రత్యేక condition ఉండదు.
  • Comparison కోసం ==, >=, <= వంటి operators ఉపయోగించవచ్చు.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top