diff --git a/chatgpt-test.py b/chatgpt-test.py index ef0af7b..50e00fb 100644 --- a/chatgpt-test.py +++ b/chatgpt-test.py @@ -1,14 +1,22 @@ -# This is a placeholder for AI experiments -print("Hello AI! This is my first AI script.") +""" +Python Tip: Using enumerate() for index and value in loops +""" -### Description +print("Hello AI! This is my first AI script.\n") -# In several Python examples within the project, loops manually track indexes using counters. -# Python provides a built-in function `enumerate()` that simplifies this pattern and improves readability. +# Example list +items = ['apple', 'banana', 'cherry'] -### Current pattern +print("Without enumerate():") +for i in range(len(items)): + print(i, items[i]) -items = ['apple', 'banana', 'cherry'] +print("\n Using enumerate():") +for index, item in enumerate(items): + print(index, item) + +print("\n Using enumerate() starting from 1:") +for index, item in enumerate(items, start=1): + print(index, item) -for i, item in enumerate(items): - print(i, item) \ No newline at end of file +# enumerate() makes code cleaner and more readable