python-basic

⌘K
  1. Home
  2. Docs
  3. python-basic
  4. enumerate() ফাংশন এবং for লুপ

enumerate() ফাংশন এবং for লুপ

enumerate() ফাংশন ব্যবহার করে আপনি আইটেমের সাথে একটি সিরিয়াল নম্বর পেতে পারেন:

fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
    print("Index:", index, "Fruit:", fruit)

Output

Index: 0 Fruit: apple
Index: 1 Fruit: banana
Index: 2 Fruit: cherry

অনেক সময় আমাদের লুপের ইনডেক্স নাম্বার অনুযায়ী কাজ করতে হয় যেমন পাঁচটা আইটেম আছে প্রথম তিনটা প্রিন্ট করবো।

fruits = ["apple", "banana", "cherry",'orange','mango']
for index, fruit in enumerate(fruits):
    print("Index:", index, "Fruit:", fruit)
    if index == 2:
        break

Output

Index: 0 Fruit: apple
Index: 1 Fruit: banana
Index: 2 Fruit: cherry

How can we help?