hey guys welcome to the next blog on python tutorial for beginners in this blog we are going to see how to use for loops in Python so first of all what are for loops so a for loop is used to iterate over a sequence and that sequence can be a list or a tuple or a dictionary or a set or a string so for that I'm going to declare some variables and the first variable is a which is a list second variable is B which is a tuple third is C which is a set fourth is a string and fifth variable is e which is a dictionary now before seeing the syntax of for loop I'm going to show you how to use in operator in Python because it's used in for loop so I'm going to first of all use print and then I'm going to use 0 and then use in operator and then I'm going to use this first variable which is a which is a list right and let's see what happens when I try to run this code so it prints true so in operator will give you true or false depending upon whether this value is present in your sequence or not so for example when I write hundred here which is not present in the list it's going to return us false so this you can use with the couple also let me just use it with tupple and let's use one here and I'm going to run the program it prints true once again also we can use in operator with strings so I can write D here but for that I need to convert this one into a string so now when I run the code it will give me true now let's see how to use a for a loop in Python so for loop as I said we can use with some sequence so let's use it with a which is a list so I can write x in a and a is a list in this case and then I can just print the value of x and let's see what happens first of all when we just write this kind of a for loop so we are going to run the program and what it does is it prints the values from 0 to 5 which is the element present inside the list so for loop is going to iterate over your list one by one and this in operator is going to check whether this value is there in your sequence or not in this case in our list or not so first of all when for loop start it will transfer the first value which is 0 into X and then it checks whether X is in this list or not and then it's going to print the value of x which is 0 and once again programs sequence schools here and once again the next value is transferred to X which is 1 and once again this condition is validated if one is in this list a or not and then the value of x is printed which is 1 and this iteration goes on and on until 5 is reached which is the last value in the list so 5 is printed and after that there is nothing inside X and then the program flow comes out of this loop you can also use this for loop with the couple also so let's try it with couple and let's see what happens it's going to print the same values also with the set let's see what happens so I'm going to run the program once again and it prints 0 to 5 once again because these are the content of the set also once again let's try to use the for loop with the string and let's see what happens and it's going to print out the content of this string which is 0 1 2 3 4 5 and it prints them one by one now with the dictionary we use this for loop in a little bit different way so for example I will write the variable name which is a dictionary and then let's say I want to just print all the keys inside this dictionary then I can write e dot keys and then we have all the keys using this function and when I run the code it's going to print all the keys inside your dictionary also when you use the function called dot values it's going to print out all the values inside your dictionary you can see max and the age 20 now let's say you want to print out all the key value pairs which are there in your dictionary you can use a function called items and this is going to give you keys and values right so I can just write key comma value here and then I can print the value of key first of all key and let's provide some space here and then let's print out the variable value here okay and when I run the code it's going to give you first of all key and then the value once again key and then the value so in order to print out the keys and values from a dictionary you can use this kind of notation now you can also use a function called range so let me show you how to use this function which is range which returns us the values starting from 0 so there is a function called range here and in here you can provide a number and this range is going to return the number starting from 0 to 5 so whatever number you write here the range ray will be returned from 0 until that number except whatever number you write here ok so let me just try to print out the values which is there in the X and when I try to print this it's going to return me from 0 - five and you can see here six is not printed because the range will give us the value except that value which you write here you can also provide the start value here so let's say I want to start from two and I want to go until five I can write something like this and now you can see now it will start printing from two to five also you can give a third parameter here which is a step parameter so I can give three here for example and let's say I want to write thirty here and let's run the code and now what is going to return is it starts from two because we have given the start value to here and it's going to go on till twenty nine because we have given 30 value here right and this is the step so every third value will be printed after two here so five will be printed then eight then 11 14 17 20 23 26 and 29 every third value right if you write here 2 then we will increment in the step of 2 so this is how you can use range function with your for loop also as I said in the last blog you can use else statement with your loops so I can also write else here and then after the colon in the next line I can print something or I can execute some statement so for now I will just print finished and then I'm going to run the code and once the loop is finished you can see this else statement is executed which prints finished so else statement will be executed once your for loop is finished so in this way you can use for loops in Python I hope you've enjoyed this blog and I will see you in the next blog
Post a Comment