Lesson finished
Unlock all 81 lessons of Data Structures and Algorithms using PythonFull course ₹40,000 · paid
Python Conditionals, Loops and Sets
A free lesson from Skill-Lync's Data Structures and Algorithms using Python course, on Grow by Skill-Lync · Last updated 16 Nov 2026 · Session: “Week 1 Session 2 Part 2”
What will you learn in this free Data Structures and Algorithms using Python lesson?
Covers if, else and elif statements with indentation and logical operators, then while loops with break and pass. Finishes with Python sets, including update, discard, remove, pop, clear, union and intersection.
- If, else and elif with indentation
- While loops with a counter, break and pass
- Set methods: update, discard, remove, pop, clear
- Set union and intersection
What comes after lesson 3?
Later in Skill-Lync's Data Structures and Algorithms using Python course (paid): lesson 4, Week 1 Session 2 Part 3; lesson 5, Week 1 Session 3; lesson 6, Week 1 Session 4. The full course has 81 lessons (24 h 0 min of video); see the course overview or its price and syllabus.
Lesson transcript
Machine transcript of the lesson audio (lightly cleaned). Technical terms may contain errors; the video is the reference.
Now let's move to conditional statements. So conditional statements are basically used to check for a condition and then execute those set of statements or if the condition is false, then execute another set of statements, those kind. So for that we use if statements, if else statements, else if statements, etc. So the general syntax is basically to use if and let's say I would say 5 equals 2, if 5 is equal to 2, then colon and then just note this indentation which we have because Python does not have brackets to make sure that the statements are one after the other.
We basically have indentation. So if this particular condition is true, then I want something to be printed and then I would print this shouldn't be printed because the condition is not true, right? And if this doesn't happen, then let's say, you know, I would print something. Hello. Now let's try to run this. So as we would expect that, you know, this part shouldn't have been printed and that's what it didn't print. We could even add and or statements. So if I just write or 2 equals 2, then what do you think would happen?
We do have this shouldn't be printed. And the reason for that is because all statement just needs either one of the two or both to be true. So since this is true, this would be printed if I used and then this would not be printed. So that's how we basically use the logical operators to append multiple conditions within the same if statement logically. We could also use else statements. If the condition didn't work, then we could simply use else and then again colon and then, you know, again, any block of statements we want to execute.
So let's say this is correct. And now if I try to run this. Yeah, so this got printed. Now we could also have, let's say, multiple versions of else. So, for example, if I say if age is greater than zero and age is less than, let's say, 15 and basically we do not have age. So let me just put age equal to 12 for now. So now your editor is giving you options like this so that, you know, your statements are in a concise form. Basically, your age is between 0 to 15. Then this should be printed.
Or we can just write then, you know, this person is a child. Else I want to print a not child. That's it. So this is going to be my program. Basically, I want to find if this person is a child or not. And yeah, so this person is a child. That's fine. But it's not that a person is only child, right? The person can also be adult, old person, etc. So we have multiple else statements. For that, we use if, which is basically just else if statement.
So if the age is between 16 and let's say 30. Or let's just use 12 over here, 13 to 19. And then I would write teenager. And then after this, I may have another elif. And it may be from 20 to, let's say, 65 or 60. So I would print adult. And then finally, at the end, I would simply have else because anything above 60, I would write old. So yeah, let's try to run this. So we have age to be this program prints old. But this shouldn't be the case because age is 12.
So it should print. OK, so since we do not have equal sign, we should use our operators properly. So yeah, now let's try to run this because so now we have this child over here. I can try to change the age. Let's say if I do make it 60, it should print adult. So yeah, that's right. Now, this is how we could use if, else and elif statements. Now, this is good for condition checking and executing. But what if we want to execute the same line of code again and again just by incrementing, let's say, a variable or we want to traverse an entire list or a set or a dictionary or a tuple and then basically want to do something for that, we cannot keep writing the same code again and again.
For that, we really need iterative statements. And iterative statements are loop statements, which are basically used to loop through a data structure or to loop through a particular condition. And based on that condition, we are going to loop through a set of statements. So the first type of loop is going to be the for loop, which you might have studied. Let's say you do have a list one and in this you have one, two and three. That's it. And now I can use my for loop, which is going to iterate over this entire list.
So I could just write for I in list one and then use a colon and simply print I. So this is how we would use the for loop in Python. And as you can see over here, we have looped through the entire list successfully. It could also be a simple string, which I want to loop through. So this string is going to have skill link and I'm going to loop through this entire string and it's going to just print, you know, each and every character of the string.
And let me just call it str1 since it's OK. So we do have, you know, the skill link executed correctly based on the print statement. The print statement is automatically adding a new line over here. We could also actually loop through from zero to a particular number. And for that, we can just use the range function. So I can just go from one to ten and just print I. So let's try to run this. So this is going to print I, which is going to be from zero to nine.
So remember that whenever you use range, the range is a range of ten numbers, which is going to start from zero. Or if you really wanted one to something, then you should write range one comma ten. And let's now try to run this. So, yeah, we do have it from one to nine. So let's change it to 11 now. Right now we have one to ten. So this is how you could use range method to print from a particular number to a particular number or from zero to that particular number.
You could even have another type of argument over here. So let's say you do not want to increment only by one. So you do not want one, two, three, four. Let's say you want only, let's say, even numbers. So you would start from two. And then let's say you want to go till 12 and add the steps of two. So that's going to print all even numbers for us. So it's going from two to 12, all the even numbers. We could also actually go through a list just like we did earlier.
So here we have one and then we have two and then we have three. But we also, let's say, need the index. So to have the index, I is a good variable to have the index. I want it to actually go from zero to the range of length of this particular list. So whatever is the length of this list, we may not know necessarily that it has a length of three or a length of so and so number. Then we can simply use the length function in the range to actually now print the index as well as the value.
So list one, I, that's it. So let's try to run this now. So now you can see that at the index zero, we have the value one. At the index one, we have the value two. And at the index two, we have the value three. So this is how we could actually even get index in a particular list in the for loop. Now we could use else statement within our for loop as well. And this else statement is not really used in any other programming language except Python.
And Python uses us to use the else statement with this for loop, which can be executed only when all the iterations are exhausted. So if your loop ever executes a break statement, then the else will not get executed. So for example, here we do not have any break statement. And if I write else and then I print else. So now let's try to run this. So we do have this else printed over here. But let's say if I have a statement over here, if I equals two.
So for example, if I do have a third element in this particular list, then I simply want to break and break is just used to break out of the loop in the loop, wherever you write break and if break is executed, then it will break out of the loop and else will not get executed. So let's try to run this now. So we do have the print statement run. And as soon as it saw that I is equal to two in this particular case, it executed break, but it did not execute the else.
So that's how we can use else just to make sure that this is executed only once my entire loop is executed and there was no break executed in this loop. Now, another type of loop, which is available in Python is the while loop and the while loop really just allows. Out of a code to be executed until a particular given condition turns false. And it's also called as a pre-tested loop. So the simple syntax for a while loop is while and then you have your condition.
So let's say while 2 less than 5 and then you want to print something. Now what do you think would happen over here? This 2 less than 5 is always true. So this print something will always execute and your program will literally crash because you are infinitely printing something, right? So make sure that you do have something which is going to terminate this particular loop. So let's say if I just write i equal to 1 and I would want to keep executing this particular loop until i is less than 5 and I really want to increment i every time with 1.
So this is how my program would run and now something is executed 4 times because initially i is 1 and then it becomes 2 and then it becomes 3 and then it becomes 4. But as soon as it becomes 5, this condition does not hold true and the loop breaks over that point. I could also again, you know, introduce a break statement. So let's say if i equal to 3, then break. So now how many times do you think something is going to get executed? So now something got executed only twice because as soon as i became 3, the loop broke.
There's another keyword which is used with loops in Python and that is the pass statement. So the pass statement is generally used to declare an empty loop or it is used to define an empty class or function or control statement in general. So for example, I am incrementing i over here and then here I would, let's say, write pass. And let's save this and run this. So nothing happens over here. I'm just passing it back to the top. Again, with the while loop, we can use else statement.
So here, since I do not have any breaks, I can, you know, print something using the else statement. If the loop continued, you know, working properly, then my else statement will be executed. So now we do have this else statement executed just because the loop continuously worked and it terminated, you know, completely. And we did not have encountered any break statement. An opposite thing to the break statement is the continue statement. So continue word is generally used to get a program flow back to the top.
So if I just write, if i modulo 2 is not equal to 0, then print odd number. Or I would just write continue over here and here I would print even number. So let's see what would happen if this gets executed. Now it says even number twice, but it does not say anything in two situations. And the reason for that is if it is odd, then we are just continuing from here back to the top of the while loop. But if it is even, then only it is printing even number.
So you would realize that in the first case, you do have this something because i was equal to one. And then as soon as i became equal to two, then it printed even number. And as it executed, when i became equal to three, it simply continued and it did not print even number. And that's why we do not have even number written after the second something. So if you try to understand this program, you will be able to understand the use of continue statement the way I have used it.
Now what if in this place I would have just used pass? So basically I'm executing an if statement, but I'm not doing anything. So what would happen? When I try to run this now, you can see every time after something, the even number print is executed. And because pass has not done anything, pass is basically generally used to assign a statement where we do not want to assign something. So for example, I just, I don't want to do anything. I just want to, you know, increment i and that's it.
I don't want to print anything. I don't want to do anything. I just want to check if my while loop works properly. So I would just write pass over here or a better example would be if I'm looping through a particular list. So let's say for i in list one, and then I don't want to do anything. So I just want to loop through list one something, and then I would just want to check, you know, if I'm able to loop through it. So when I try to run this now, nothing happened because there was nothing in list one and there was nothing in my for loop, but pass is extremely important over here.
If I try to now remove this, now you can see there's already an error, which the IDE is showing that we should have something after this conditional statement. So that's why we generally use pass in such cases. Now, remember, you could even add values in within your string using the format method of the string. So, for example, I want to write this and this are participants. Now, what are these, these, right? So for that, I would use dot format, and then I can add whatever I want to say.
So I would just write one comma two. That's it. Now, when I run this, you would see one and two are participants. We could even assign, you know, different numbers to it. So, for example, if I write one and zero over here and try to run this now. So now it becomes two and one are participants because now index zero is coming over here and index one over here. So index zero has one, right? So one is coming over here and index one has two. So two is coming over here.
I could actually even use other ways, like I could use ABCD. So to do the same thing, I would write A and B and then assign A equals to one and B equals to two. And if I run this now, again, we have two and one are participants. So this is how I could even use format method of format function of the string. Now, we always saw lists being traversed or sliced using positive integers. But Python also gives a very flexible way of parsing list, which is using negative integers as well.
So if we had one, two, three, four and five in the list, let's say. And if I try to actually parse the list and get the minus one position, this is going to be the element from the last. So whatever is so whatever is using the minus sign is actually going to start from the back instead of the front. So that's the only difference. So if I really wanted to come from the back now from the back, we would start from the one index. All right.
So if I try to print this now, this is going to print five for us. Let's say we are trying to print it from the second last to the beginning. So this is actually going to print from the second last to the end. What if we really want to print from the second last to the beginning? For that, we really need to do from the beginning to the second last. That's how it works. So now we have from the beginning to the second last and it's going to exclude the second last value.
Now, what if I really want it to be from the fourth last to the second last? This is how it's going to work. So now I do have, you know, from the fourth last, which is two to the second last, which is going to actually exclude the second last. So this is how we could use negative integers as well to slice our list or tuple or string, whatever we want to slice using negative integers. Now, since lists are mutable, we can definitely try to add things in our list.
So let's say I want to append it in the list. Now, remember, list is an ordered collection. The order in the list has to maintain and we will never have a list output in another order just like set because list is an ordered collection. And that is why we are able to access it using the index value. And we are not able to access using the index value for, let's say, set because set is unordered. So when I try to append it, it would append at the end.
So let's append six and let's just try to print the entire list one. Now, let's see what our editor is telling us. So, yeah, it's basically telling us that instead of, you know, writing these two statements, you could do something else. Basically, you know, initialize it properly. And that's the beauty of PyCharm. But let's right now work with this and run this. Yeah, we do have the correct output the way we wanted. I could even remove things because obviously list is mutable. So I could remove dot remove.
Let's say one. And let's try to run this now. You would see that one is removed from our list. So this is how you could manipulate lists and append things, remove things from the list. In fact, you could do the same with sets. So with sets, you could use the add method to add things within the set. You could use the update method to update the set. You could also use the discard method and the remove method for doing the same thing. Also, you could pop things off from the set using the pop method. So let's try to create a set now. So I could create, let's say, a set called numbers. So I would just write num equal to and then I would use the set method. And within this, I would just type a few numbers. 1, 2, 3. So that's a simple set which I have.
Now, as I said, we could use the add method. So I can just add num.add and then I would add 4. Now let's try to print this set num. So now we have, you know, this set of 1, 2, 3, 4 numbers. Now I could actually even use the update method. And the update method is going to take another set of numbers. So let's say I can just update it with 4 and 5. And now I have 1, 2, 3, 4, 5, all of these within it. I could even discard certain items from this. So let's say I used discard and then I wrote 1 in this.
When I run this, when I run this, you can see I have discarded 1 from this. Or I could use the remove method. As you can see, even with remove, we have removed the item number 1. Now let's try to pop things off from this particular set. When I run this, now you could see that 1 is popped off. Let's try to pop another element from the set. Now when I run this, only 3 remains. So everything is getting popped off from the beginning in the set.
You could even simply clear the set. So if I just write clear over here and I run this, we have a simple set. We do not have anything basically in the set. Now discard and remove, both of them are doing the same task. But if I try to discard something which is not present in the set, it would not throw an error. But if I try to remove something which is not present in the set, then that would throw an error. So that's the difference between discard and set. Again, set is generally used for the set theory, right?
We can have union and intersection, different things of the set. So for example, I have this particular set, num, and I have another set which is called num2, and this is simply having 1 and 3. If I try to now find, let's say, the union between num and num2, what do you think is going to come up for the union? So I have to type union and within this, I have to put num2. So when I run this, now you can see 1, 2, 3 is getting printed.
Let's say if I have a 4 within my num2, and if I try to print this, now I have 1, 2, 3, 4, which is basically the union of these two sets. If I try to print the intersection, it's simply going to take intersection word, and this is going to give me the intersection, which is 1 and 3, which is the common elements of both the sets. We could also have done the same union and intersection using operators. So I have this num and num2, and between them, I could use this operator, which is going to basically print a union, and this ampersand operator, which is going to basically print intersection for me.
Again, we could have a difference operator using the minus sign or a dot difference. This is how we could have a difference between two sets, or we could even have symmetric difference, which can be used using this operator.