if Statement

if Statement అంటే ఏమిటి?

Pythonలో ఒక condition నిజమైనప్పుడు మాత్రమే కొన్ని statements‌ను execute చేయడానికి if statement ఉపయోగిస్తాం.

Condition నిజమైతే, if లోపల ఉన్న code అమలవుతుంది. Condition తప్పైతే, ఆ code అమలు కాదు.

if Statement Syntax

if condition:
    statement

if తర్వాత condition రాయాలి. Condition చివరలో colon : తప్పనిసరిగా ఉండాలి. దాని కింద రాసే statement‌లు indentationతో ఉండాలి.

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

  • ఒక పరిస్థితి నిజమైనప్పుడు మాత్రమే పని చేయడానికి.
  • వయస్సు, మార్కులు, ఉష్ణోగ్రత వంటి విలువలను పరీక్షించడానికి.
  • Programలో decision తీసుకోవడానికి.

ఇది ఎలా పనిచేస్తుంది?

  1. Python ముందుగా condition‌ను check చేస్తుంది.
  2. Condition True అయితే indented code execute అవుతుంది.
  3. Condition False అయితే indented code skip అవుతుంది.

Pythonలో comparison కోసం >, <, >=, <=, == వంటి operators ఉపయోగించవచ్చు.

Simple Explanation

if statementను ఒక decision maker‌లా ఊహించుకోవచ్చు. “ఈ condition నిజమైతే ఈ పని చేయి” అని Pythonకు చెప్పడానికి ఇది ఉపయోగపడుతుంది.

ఉదాహరణకు, ఒక వ్యక్తి వయస్సు 18 లేదా అంతకంటే ఎక్కువైతే ఓటు వేయవచ్చు అని చూపించవచ్చు. వయస్సు 18 కంటే తక్కువైతే message కనిపించదు, ఎందుకంటే if block అమలు కాదు.

గమనించాల్సిన ముఖ్యమైన విషయం: if blockలోని code indentationతో ఉండాలి.

Real-Life Example

మీరు ఇంటి నుంచి బయటకు వెళ్లేటప్పుడు వర్షం పడుతుందా అని చూస్తారు. వర్షం పడుతుంటే గొడుగు తీసుకుంటారు. ఇదే విధంగా Pythonలో condition నిజమైతే ఒక పని చేయడానికి if statement ఉపయోగిస్తాం.

Code Examples

వయస్సు ఆధారంగా if Statement

age = 18

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

ఈ programలో age విలువ 18గా ఉంది. age >= 18 condition True అవుతుంది. అందువల్ల if blockలోని print() statement execute అవుతుంది.

You can vote.

Line-by-Line Explanation

age = 18

ఈ lineలో age అనే variableలో 18 విలువను store చేస్తున్నాం. Python తర్వాత ఈ విలువను conditionలో ఉపయోగిస్తుంది.

if age >= 18:

ఈ lineలో age విలువ 18 కంటే ఎక్కువ లేదా సమానమా అని Python check చేస్తుంది. Condition True అయితే కింద indentationతో ఉన్న code execute అవుతుంది. చివరలోని colon : if block ప్రారంభమవుతుందని సూచిస్తుంది.

print(“You can vote.”)

ఈ line if blockలో ఉంది. Condition True అయినందువల్ల Python ఈ messageను screenపై print చేస్తుంది. ముందు ఉన్న spaces indentationను సూచిస్తాయి.

Try It

Try this Python code:

Example

temperature = 35

if temperature > 30:
    print("It is hot.")

Try It Yourself »

Practice

Practice: ఒక number variableలో 10 అనే విలువను ఉంచండి. ఆ number 5 కంటే ఎక్కువ అయితే Number is greater than 5. అని print చేయండి.

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

Practice: ఒక marks variableలో 75 అనే విలువను ఉంచండి. మార్కులు 50 లేదా అంతకంటే ఎక్కువ అయితే Pass అని print చేయండి.

Hint: >= comparison operatorను ఉపయోగించండి.

Common Mistakes

if condition తర్వాత colon : పెట్టకపోవడం.

ప్రతి if condition చివరలో colon పెట్టాలి. ఉదాహరణ: if age >= 18:

if blockలోని statementకు indentation ఇవ్వకపోవడం.

if blockలోని ప్రతి statementను సాధారణంగా నాలుగు spaces indentationతో రాయాలి.

సమానతను check చేయడానికి ఒకే = ఉపయోగించడం.

విలువలను compare చేయడానికి == ఉపయోగించాలి. ఒకే = assignment కోసం ఉపయోగిస్తాం.

Condition False అయినప్పుడు కూడా if block execute అవుతుందని అనుకోవడం.

if blockలోని code condition True అయినప్పుడే execute అవుతుంది. False అయితే అది skip అవుతుంది.

Interview Questions

Pythonలో if statement అంటే ఏమిటి?

Answer:

ఒక condition True అయినప్పుడు మాత్రమే codeను execute చేయడానికి ఉపయోగించే conditional statementను if statement అంటారు.

if condition తర్వాత colon ఎందుకు ఉపయోగిస్తారు?

Answer:

if statementకు సంబంధించిన code block ప్రారంభమవుతుందని Pythonకు తెలియజేయడానికి colon : ఉపయోగిస్తారు.

if condition False అయితే ఏమి జరుగుతుంది?

Answer:

if blockలోని statements execute కావు. Python వాటిని skip చేసి తరువాతి codeకు వెళుతుంది.

if blockలో indentation ఎందుకు అవసరం?

Answer:

ఏ statements if statementకు చెందినవో Python గుర్తించడానికి indentation అవసరం.

Exam Questions

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

number = 10
if number > 5:
print(“Greater”)

Answer:

Greater

if statement యొక్క సాధారణ syntax రాయండి.

Answer:

if condition:
    statement

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

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

Answer:

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

if statementలో condition True అయినప్పుడు ఏమి జరుగుతుంది?

Answer:

if blockలో indentationతో ఉన్న statements execute అవుతాయి.

Quick Revision

  • if statement condition True అయినప్పుడు codeను execute చేస్తుంది.
  • if condition చివరలో colon : తప్పనిసరి.
  • if blockలోని code indentationతో ఉండాలి.
  • Condition False అయితే if block skip అవుతుంది.
  • Comparison కోసం >, <, >=, <=, == వంటి operators ఉపయోగించవచ్చు.
  • if statement decision making కోసం ఉపయోగపడుతుంది.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top