Skip to playerSkip to main contentSkip to footer
  • 5/20/2025
An iterable in Python is an object capable of returning its members one at a time, permitting it to be iterated over in a for-loop. It represents a sequence of elements that can be traversed. Common examples of iterables include lists, tuples, strings, dictionaries, and sets.

Category

📚
Learning
Transcript
00:00Now, we have to learn about the for loop.
00:02For loop to which way to use.
00:04However, many of these courses and instructors
00:08have to start with the for loop.
00:10But I want to show you what the concept is basically.
00:16For loop, you can use your coding career.
00:20So, I think it is definitely very important
00:24that you should know what the mechanism is.
00:28So let's imagine, I'll give you an example, you have a new shop, okay?
00:33And the shop's special thing is that you have some items in which you have some apples, bananas, fruits, eggs and milk.
00:41Like apple, banana, cherry, egg milk.
00:43This is your new shop, okay?
00:45So I'll explain my list.
00:47Now, my question is that you have all the items in your shop, which is my list,
00:55this is the price tag.
00:57String concatenation, plus price tag, apple price tag, banana price tag,
01:04and this is the price tag.
01:07So how do you do it?
01:09Now, we have to do my list,
01:13and say index zero plus,
01:16this is the price tag,
01:20price tag,
01:24right?
01:25Yag, not tag.
01:26As you can see, my English is very fast.
01:28Okay?
01:29And now print it.
01:30Now, what do we do?
01:31Now, we will do this.
01:33Look at this.
01:34I will execute it.
01:35And I will do the first element to your apple price tag.
01:37Okay?
01:38Now, if you want to do it, how do we do it?
01:39So you can do it.
01:40Now, let's do it.
01:41Let's do it.
01:42Let's do it.
01:43Let's do it.
01:44Let's do it.
01:45Let's do it.
01:46Let's do it.
01:47Let's do it.
01:48Now, let's do it.
01:49Let's do it.
01:50Let's do it.
01:51Let's do it.
01:52Let's do it.
01:53Make sense.
01:54Okay?
01:55Okay?
01:56So, you have to price tag all the items.
01:58Now, my question is to you.
02:01Is it an easy way to do it?
02:04Imagine that you have 100,000 items.
02:07100,000.
02:08And I am not joking.
02:09Literally.
02:10100,000.
02:11Then what do we do?
02:12Then we will do it?
02:13Of course, not.
02:14So, you need a robust way to do it.
02:16Which can automatically return these items.
02:20This is the example of how you like that you need.
02:22One servant, one helper,
02:24one who gives you a item and gives you a price tag.
02:28And it will be a price tag.
02:34However,
02:35that the item,
02:37which you have to repair each item,
02:39you need to serve your fellow,
02:40the servant,
02:42which you have to do in the shop,
02:44all those items,
02:46to return all this items,
02:48You need to keep an arrangement in order to be returned.
02:50That's it.
02:52If an item is in such a configuration,
02:56you can return to be returned.
02:58That's it.
03:00If an item is in single condition,
03:02if an item is in container,
03:04you can return to be returned.
03:06This means that
03:08the item by itself
03:10to be returned,
03:12it needs to be specific characteristics.
03:16It needs to be inherent capability,
03:20which you want to build a list of together,
03:22which is one by one return
03:25to be returned.
03:26For example,
03:28the people will be returned to be returned.
03:30It is not that people will be returned to be returned.
03:32The people will be returned to be returned to be returned to be returned.
03:34This is why I have said this.
03:36Basically,
03:40when I tell you that
03:42the last thing is,
03:44the fact that the thing is,
03:46This is an easy way to do that if I call next function in my list and print it, then I need the first element to give me the first element.
04:04Now I can put a simple price tag on this.
04:08However, this is a problem.
04:10This is not iterator.
04:18This is not iterator.
04:20Basically, this is not iterator.
04:24However, the problem is that my list is iterator.
04:30My list is not iterator.
04:33My list is inherent capability.
04:36It is iterator.
04:40It is iterator.
04:42It is iterator.
04:44It is iterator.
04:46It is iterator.
04:50It is iterator.
04:52It is iterator.
04:54It is iterator.
04:56It is iterator.
05:06It is iterator цar-
05:08It is iterator.
05:09I will call the iterator and call the value of the new list.
05:17Now my new list is a iterator.
05:21Now I can call the next function.
05:24It is simple enough.
05:26I will execute the price tag.
05:29Now you can see the first solution.
05:32The number of items is more than the number of items.
05:38So the number of items can be fixed.
05:41But with this reason it will be simple.
05:43We need the next call to place
05:45and have well done.
05:48Excellent!
05:50Or maybe not.
05:54So how do I do it?
05:56I have done it.
05:58Then add and add.
06:01It will be milk price tag.
06:04However,
06:05which I will do next because milk is the last item and I will execute so it will give me a error and break it down
06:13Interesting
06:15What kind of method is that it can be more robust and versatile?
06:23Automatic error handling which can be given to the object in the iterator
06:30Then I will put the next function on it
06:33What can I do?
06:34Automatically
06:35And I will not write so long code
06:38So the answer is for loop
06:41For loop basically is what I have done
06:46Now look
06:47For loop define for for
06:50And you can write a variable in which you can store a value
06:54This is a new list
06:57Okay?
06:58So you can write value
06:59You can write my list
07:01And you can convert it
07:03And you can convert individual elements
07:05And you can assign the new list
07:08Okay?
07:09So I will say for new list in my list
07:12Make sense?
07:14So now this is the iterator
07:15So this is the first element
07:17New list
07:19And you can close it
07:20And you can close it
07:22Very simple right?
07:25If you wanna use the code
07:28Then you can run
07:30You can write the block
07:34Basically
07:37You can write a block
07:39I will write a block
07:40How to print it
07:41She will say
07:42If you have a new list
07:43If you have a new list
07:44You will write a price tag
07:45These are the old name
07:47Then
07:48Is the price tag
07:49Ctrl C
07:50Plus price tag
07:52That's it.
07:54I will execute it.
07:56Kaboom, the result is the same.
07:58But it is important enough,
08:00that the next error was given,
08:02it was given.
08:04Now, I will do one thing.
08:06I will beautify it.
08:08I will do X and multiply by 20.
08:10Run.
08:12This is the result of the for loop.
08:14The result of the for loop.
08:16The result of the next function.
08:18Again,
08:20the result of the for loop.
08:22Now, the code of robustness
08:26is not only for error handling.
08:30It will not break.
08:32The item milk is finished.
08:34The price tag is left.
08:36The price tag is left.
08:38The item milk is not finished.
08:40The price tag is left.
08:42The price tag is left.
08:44The two lines are left.
08:46However,
08:48کہ آپ نے iterator لگایا next next کرتے گئے
08:50لیکن جب last آیا تو وہ break ہو گیا
08:52code
08:52تو یہ for loop basically under the hood
08:55اس طریقے سے کام کرتا ہے
08:57کہ جو بھی آپ اس کو object دیتے ہیں
08:59اس کو وہ ایک iterator میں convert کرتا ہے
09:01اور next next کا function اس کے اوپر لگاتا جاتا ہے
09:03اور جو بھی block of code
09:05یا جو بھی functionality آپ perform کرنا چاہتے ہیں
09:07اتنی دفعہ
09:08یا اس object کے اوپر
09:10اتنی دفعہ
09:11یا اس object کے اوپر
09:13وہ functionality run کرتا ہے
09:14ابھی میں نے اس object کے اوپر ایک functionality run کی
09:16اور اگر یہ بھی نہیں کرنا چاہتا
09:18تو میں دیکھئے کہتا ہوں
09:20print let's say
09:22new list
09:24basically new list جو ہے
09:28آپ دیکھیں گے کہ سارے item print ہو جائیں گے
09:30میں execute کرتا ہوں
09:31سارے item print ہوئے
09:33اتنی دفعہ بھی ہوا
09:34اور ہم نے اس کے اوپر function بھی perform کیے
09:36یہ آپ کو پتہ چل رہا ہے
09:371,2,3,4,5
09:391,2,3,4,5
09:425 times یہ execute ہوا code
09:44اور 5 times اس کو price tag بھی لگی
09:46make sense
09:47تو اس کو summarize کرتا ہوں
09:49میں آپ نے دیکھا
09:50کہ شروع شروع میں
09:50ہم نے ایک دکان کھولی
09:52اور دکان پر ہمارے پاس
09:54مسئلہ یہ تھا
09:54کہ ہمارے پاس بڑے بڑے بہت سارے items تھے
09:56اب میں نے کہا
09:58کہ اس طرح کرتے ہیں
09:58کہ ہر item کے اوپر
09:59price tag تو لگانی ہے
10:00customer آئے گا
10:01کل price دیکھے گا
10:02اس کے اوپر تو وہ price tag لگانی ہے
10:03price tag اچھا
10:04تو ایک تو طریقہ یہ تھا
10:05کہ price tag یہ کرتے ہیں
10:07کہ ہمارے دکان کے اندر
10:07جتنے بھائے item پڑے ہوئے
10:08ان کو index دے دیتے ہیں
10:09اور اس کو اوپر price tag
10:10concatenate کر دیتے ہیں
10:13لیکن یہ robust نہیں تھا
10:14ہمارے پاس
10:15let's say
10:15hundred thousand
10:16جو چیزیں پڑی ہوئی تھیں
10:18اور ان کے اوپر
10:19اس طرح لگانا
10:20was not cumbersome
10:20تو میں نے کہا
10:21کہ اس طرح کرتے ہیں
10:22اس کو iterator میں
10:22convert کرتے ہیں
10:23next next function
10:24call کرتے جاتے ہیں
10:24کام ہو جائے گی
10:25لیکن اس میں بھی problem تھا
10:26ایک تو code بڑا ہے
10:27دوسرا یہ کہ
10:28code break ہو جاتا ہے
10:29جب item ختم ہوتا ہے
10:30تو پھر
10:31اس کا solution
10:31یہ نکلا کے
10:32ہم for loop
10:32use کر سکتے ہیں
10:33جو کہ بہت ہی
10:34robust ہے
10:34اور automatically
10:35ہمارے لیے
10:37وہی کام
10:37وہی functionality
10:38perform
10:39تو ایک simple
10:41summary میں
10:42اس طرح سے
10:42for loop کو
10:43represent کروں گا
10:43کہ for loop
10:44ایک servant کی طرح ہے
10:45جو one by one
10:47آپ کو item
10:48return کرتا ہے
10:49one by one
10:49item return کرتا ہے
10:51اب آپ
10:52اتنی دفعہ
10:53کوئی اور
10:53code perform
10:54کر سکتے ہیں
10:55جتنی دفعہ
10:55وہ one
10:55code آپ کو
10:56return کر رہا ہے
10:57item یا پھر اس
10:58item کے اوپر
10:59کوئی functionality
11:00perform کر سکتے ہیں
11:01اب جو بھی
11:02code آپ لکھتے ہیں
11:02وہ for loop
11:04کے اندر
11:05یہ دیکھئے
11:05یہ جو بھی
11:06code for loop
11:07کے اندر آئے گا
11:08وہ for loop
11:09کی اندر
11:10اندر run
11:12ہوگا
11:12make sense
11:13اوکے جی
11:14excellent
11:15تو میں اس کو
11:15یہاں پہ کرتا ہوں
11:16close
11:16to see you in the next one

Recommended