for Loop

for Loop అంటే ఏమిటి?

Pythonలో ఒక group of items పై ఒక్కొక్కటిగా పని చేయడానికి for loop ఉపయోగిస్తాము. ఒక listలో ఉన్న ప్రతి itemను తీసుకుని, దానిపై మనం ఇచ్చిన codeను అమలు చేస్తుంది.

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

ఒకే codeను మళ్లీ మళ్లీ రాయకుండా, అనేక itemsపై ఒకే పని చేయడానికి for loop ఉపయోగిస్తాము. ఉదాహరణకు, ఒక listలోని ప్రతి పేరు print చేయడం, ప్రతి సంఖ్యను పరిశీలించడం లేదా ప్రతి itemపై calculation చేయడం వంటి పనులకు ఇది ఉపయోగపడుతుంది.

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

  1. Python list లేదా ఇతర collectionలోని మొదటి itemను తీసుకుంటుంది.
  2. ఆ itemను loop variableలో ఉంచుతుంది.
  3. Indented blockలోని codeను అమలు చేస్తుంది.
  4. తర్వాతి itemను తీసుకుని అదే పని మళ్లీ చేస్తుంది.
  5. అన్ని items పూర్తయ్యే వరకు ఈ ప్రక్రియ కొనసాగుతుంది.

Syntax

for variable in collection: అనే రూపాన్ని ఉపయోగిస్తాము. Colon తర్వాత వచ్చే statements తప్పనిసరిగా indentationతో ఉండాలి.

ఎక్కడ ఉపయోగపడుతుంది?

  • Listలోని ప్రతి itemను print చేయడానికి
  • చాలా valuesపై ఒకే operation చేయడానికి
  • Names, products లేదా marks వంటి dataను ఒక్కొక్కటిగా process చేయడానికి

Simple Explanation

ఒక basketలో మూడు fruits ఉన్నాయని అనుకుందాం. ప్రతి fruitను ఒక్కొక్కటిగా తీసుకుని దాని పేరు చెప్పాలి. Pythonలో ఆ పని కోసం for loop ఉపయోగిస్తాము.

for fruit in fruits: అని రాస్తే, fruits listలోని ప్రతి item వరుసగా fruit variableలోకి వస్తుంది. Indented print(fruit) statement ప్రతి item కోసం ఒకసారి అమలవుతుంది.

for loopలో variable పేరు మనం అవసరానికి అనుగుణంగా పెట్టవచ్చు. ఉదాహరణకు item, name లేదా number. అయితే variable పేరు ఆ itemను స్పష్టంగా అర్థం చేసుకునేలా ఉండటం మంచిది.

Real-Life Example

ఒక teacher దగ్గర students పేర్ల list ఉందని అనుకుందాం. ప్రతి student పేరును attendance కోసం ఒక్కొక్కటిగా పిలవాలి. ప్రతి పేరును manually రాయకుండా, for loop listలోని ప్రతి పేరును తీసుకుని print చేయగలదు.

Code Examples

Listలోని ప్రతి itemను print చేయడం

fruits = ["apple", "banana", "mango"]

for fruit in fruits:
    print(fruit)

ఇక్కడ fruits అనే listలో మూడు items ఉన్నాయి. for loop ప్రతి itemను ఒక్కొక్కటిగా fruit variableలో ఉంచుతుంది. తర్వాత print() ఆ itemను చూపిస్తుంది.

apple
banana
mango

Line-by-Line Explanation

fruits = [“apple”, “banana”, “mango”]
ఈ lineలో fruits అనే list తయారవుతుంది. ఇందులో మూడు fruit names నిల్వ ఉంటాయి. Python ఈ listను తరువాత ఒక్కొక్క itemగా చదువుతుంది.
for fruit in fruits:
ఈ line for loopను ప్రారంభిస్తుంది. fruits listలోని ప్రతి item వరుసగా fruit variableలోకి వస్తుంది. చివరలో ఉన్న colon loop body ప్రారంభమవుతుందని సూచిస్తుంది.
print(fruit)
ఈ indented line ప్రతి loop iterationలో అమలవుతుంది. మొదట apple, తరువాత banana, చివరగా mango fruit variableలో ఉండగా print() వాటిని outputగా చూపిస్తుంది.

Try It

Try this Python code:

Example

animals = ["cat", "dog", "bird"]

for animal in animals:
    print(animal)

Try It Yourself »

Practice

Practice: marks = [75, 80, 90] అనే listలోని ప్రతి markను కొత్త lineలో print చేయడానికి for loop రాయండి.

Hint: for mark in marks: అనే patternను ఉపయోగించి, indented lineలో print(mark) రాయండి.

Practice: colors = [“red”, “green”, “blue”] అనే listలోని ప్రతి colorను print చేయండి.

Hint: color అనే loop variableను ఉపయోగించండి.

Common Mistakes

for statement చివర colon పెట్టకపోవడం. ఉదాహరణకు for item in items అని మాత్రమే రాయడం.
for loop statement చివర తప్పనిసరిగా colon పెట్టాలి: for item in items:
Loop bodyకి indentation ఇవ్వకపోవడం.
Loopలో అమలు కావాల్సిన statementsను సాధారణంగా నాలుగు spaces indentationతో రాయాలి.
Loopలో list పేరు బదులు తప్పు variable లేదా తప్పు collection ఉపయోగించడం.
for తర్వాత ఉన్న variable మరియు in తర్వాత ఉన్న list పేరును సరిగ్గా రాయండి. ఉదాహరణకు for fruit in fruits:
Loop variableను print చేయకుండా list మొత్తాన్ని ప్రతి iterationలో print చేయడం.
ప్రతి itemను చూపించడానికి print(fruit) వంటి loop variableను print చేయండి.

Interview Questions

Pythonలో for loop ఎందుకు ఉపయోగిస్తారు?

Answer: ఒక collectionలోని ప్రతి itemపై ఒకే codeను అమలు చేయడానికి for loop ఉపయోగిస్తారు.

for loopలో in keyword పని ఏమిటి?

Answer: in keyword ఏ collectionలోని itemsపై loop నడవాలో సూచిస్తుంది. ఉదాహరణకు, for item in items.

for loopలో indentation ఎందుకు అవసరం?

Answer: Loopలో భాగమైన statementsను Python indentation ద్వారా గుర్తిస్తుంది. అందుకే loop bodyకి indentation అవసరం.

for loopలో loop variable అంటే ఏమిటి?

Answer: ప్రతి iterationలో collection నుంచి వచ్చే ప్రస్తుత itemను తాత్కాలికంగా నిల్వ చేసే variableను loop variable అంటారు.

Exam Questions

క్రింది code output ఏమిటి? items = [“A”, “B”] ; for item in items: print(item)

Answer:

Output: A తరువాత B. ప్రతి item కొత్త lineలో print అవుతుంది.

for loop యొక్క సాధారణ syntax రాయండి.

Answer:

for variable in collection: తరువాత indented statements రాయాలి.

for loopలో colon మర్చిపోతే ఏమవుతుంది?

Answer:

Python SyntaxError చూపిస్తుంది, ఎందుకంటే colon loop block ప్రారంభాన్ని సూచిస్తుంది.

Quick Revision

  • for loop collectionలోని itemsను ఒక్కొక్కటిగా process చేస్తుంది.
  • సాధారణ రూపం: for variable in collection:
  • Loop bodyకి indentation తప్పనిసరి.
  • ప్రతి iterationలో loop variableకు ఒక item value వస్తుంది.
  • Listలోని ప్రతి itemను print చేయడానికి for loop ఉపయోగించవచ్చు.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top