|
101 | 101 | time.sleep(wait_time)
|
102 | 102 | wait_time *= 2
|
103 | 103 | attempts += 1
|
| 104 | +#################################### Behind the Seen ##################################################################### |
| 105 | +# Topic: Behind the Scene - Iterable, Iterator, Iteration Tool |
| 106 | +import time |
| 107 | + |
| 108 | +print('He is my friend') |
| 109 | +string = 'haider' |
| 110 | +print(string |
| 111 | + # ----------------------------- Explanation ----------------------------- |
| 112 | + |
| 113 | +# Iteration tools: |
| 114 | +# These are constructs used to perform iteration (looping). |
| 115 | +# Examples: for loop, while loop. |
| 116 | + |
| 117 | +# Iterable objects: |
| 118 | +# These are objects that can be looped over (i.e., used with iteration tools). |
| 119 | +# Examples: list, tuple, string, dictionary, file, set. |
| 120 | +# A true iterable must have the __iter__() method defined. |
| 121 | + |
| 122 | +# Iterator: |
| 123 | +# An object with a __next__() method, which returns the next item from the iterable. |
| 124 | +# You can get an iterator from an iterable by using the iter() function. |
| 125 | + |
| 126 | +# How iteration works (step-by-step): |
| 127 | +# 1. When a for loop is run on an iterable object, Python internally calls iter(iterable). |
| 128 | +# 2. This returns an iterator object with a __next__() method. |
| 129 | +# 3. Python keeps calling next(iterator) to get the next item in the sequence. |
| 130 | +# 4. When there are no more items left, a StopIteration exception is raised to end the loop. |
| 131 | + |
| 132 | +# Key Concepts: |
| 133 | +# - The iter() function gives an iterator from an iterable object. |
| 134 | +# - The next() function fetches the next item from the iterator. |
| 135 | +# - Once all items are consumed, next() raises StopIteration. |
| 136 | +# - The iterator remembers its current position internally. |
| 137 | +# - The memory address of the iterator remains constant, but the pointer moves to the next item. |
| 138 | + |
| 139 | +# Special Note on Files: |
| 140 | +# When we open a file using open(), it returns a file object which is also an iterable. |
| 141 | +# You can use iter() and next() on it directly to read line-by-line. |
| 142 | + |
| 143 | +# Example: |
| 144 | +# my_list = [1, 2, 3] |
| 145 | +# iterator = iter(my_list) |
| 146 | +# print(next(iterator)) # Output: 1 |
| 147 | +# print(next(iterator)) # Output: 2 |
| 148 | +# print(next(iterator)) # Output: 3 |
| 149 | +# print(next(iterator)) # Raises StopIteration |
| 150 | + |
| 151 | +# ---------------------------- Notes in Urdu ---------------------------- |
| 152 | + |
| 153 | +# Iteration tools wo hoty hain jo iterate karwate hain |
| 154 | +# Example: for loop, while loop |
| 155 | + |
| 156 | +# Iterable objects wo hoty hain jin ko hum iterate kar sakte hain |
| 157 | +# Example: list, file, dictionary |
| 158 | + |
| 159 | +# Jab hum kisi iterable object ko iterate karte hain, to response milta hai |
| 160 | +# Aur ye response deta hai ek iterator object jisme __next__ method hoti hai |
| 161 | + |
| 162 | +# Iterable object memory address return karta hai jahan se iteration start hoti hai |
| 163 | + |
| 164 | +# iter() function iterable object par apply hota hai |
| 165 | +# Ye iterable object ko kehta hai: "mujhe iterate karne do" |
| 166 | +# Result me ye iterator object return karta hai (with memory address) |
| 167 | + |
| 168 | +# __next__ or next() ka use karke hum agla element get karte hain |
| 169 | +# Har bar next() call karne par agla element return hota hai |
| 170 | + |
| 171 | +# Jab sari values iterate ho jati hain, to StopIteration exception raise hoti hai |
| 172 | +# Ye indicate karta hai ke aur koi element baqi nahi hai |
| 173 | + |
| 174 | +# Important Point: |
| 175 | +# Memory address same rehta hai, lekin pointer agay move karta hai |
| 176 | + |
| 177 | +# File variable jab kisi file ko open karta hai to wo ek iterable object ban jata hai |
| 178 | + |
| 179 | +# Agar hum list ka sirf reference variable banayein (e.g., my_list = [1, 2, 3]) |
| 180 | +# To wo iterator nahi hota — iterable object hota hai, lekin iterator nahi |
| 181 | +# Iterator banane ke liye iter(my_list) use karna padta hai |
0 commit comments