# Nakafa Framework: LLM URL: /en/subject/university/bachelor/ai-ds/ai-programming/iterable Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/iterable/en.mdx Output docs content for large language models. --- export const metadata = { title: "Iterables", description: "Learn iterable concepts in Python, in/not in operators, any/all functions, and unpacking operators with practical examples and real implementations.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/18/2025", subject: "AI Programming", }; ## Basic Iterable Concepts In Python, an iterable is an object that can be iterated or traversed one by one. Think of it like a book that you can read page by page, iterables allow you to access elements within them sequentially. Iterables have two main characteristics that make them different from regular objects. First, iterables are ordered sequences of arranged items. Second, each item in an iterable can be taken one by one in a process called iteration. ### Types of Iterables Python provides various types of iterables that you commonly use in everyday programming. In the string example, each individual character becomes an item that can be accessed. For lists and tuples, each element stored within them becomes an item that can be iterated. ## Membership Operators Python provides special operators to check whether an item exists in an iterable or not. The `in` and `not in` operators allow you to perform membership checks easily. ### Using in and not in Operators These operators work by checking each element in the iterable until they find a match or until all elements have been checked. ### Special Cases in Strings Membership operators on strings have special behavior that you need to understand. When using `in` on strings, Python not only searches for individual characters, but also substrings or parts of strings. ## any and all Functions Python provides two built-in functions that are very useful for checking conditions on all elements of an iterable. The `any()` and `all()` functions help you make decisions based on boolean values of elements in the iterable. ### any() Function The `any()` function returns `True` if at least one element in the iterable is `True`. If all elements are `False` or the iterable is empty, this function returns `False`. 70 for grade in grades] print("High grades exist:", any(high_grades)) # Output: High grades exist: True # Example with direct boolean conditions = [False, False, True, False] print("True condition exists:", any(conditions)) # Output: True condition exists: True # Example with empty list empty_list = [] print("Any on empty list:", any(empty_list)) # Output: Any on empty list: False # Practical example numbers = [0, 0, 5, 0] if any(numbers): print("Non-zero numbers exist in list") # Output: Non-zero numbers exist in list` } ]} /> ### all() Function The `all()` function returns `True` only if all elements in the iterable are `True`. If there is even one element that is `False`, this function returns `False`. For empty iterables, `all()` returns `True`. = 70 for score in scores] print("All grades pass:", all(passing_grades)) # Output: All grades pass: True # Example with direct boolean conditions = [True, True, True, True] print("All conditions true:", all(conditions)) # Output: All conditions true: True # Example with one false condition mixed_conditions = [True, True, False, True] print("All conditions true:", all(mixed_conditions)) # Output: All conditions true: False # Practical example ages = [18, 21, 25, 30] if all(age >= 18 for age in ages): print("All participants are adults") # Output: All participants are adults` } ]} /> ## Unpacking Operator The unpacking operator uses an asterisk (`*`) to unpack elements in an iterable into separate arguments. This is very useful when you need to call functions with arguments that come from lists or tuples. ### Problems Without Unpacking Before understanding the unpacking solution, let's look at problems that often arise when you try to call functions with arguments from iterables. ### Solution with Unpacking Operator The unpacking operator solves this problem in an elegant and readable way. By adding a `*` sign before the iterable name, Python will unpack all elements into separate arguments. ### Practical Uses of Unpacking The unpacking operator has many practical uses in everyday Python programming, especially when working with functions that require variable numbers of arguments.