In this article, we are going to discuss how for loop is used to iterate over a sequence inPython.
Python programming is very simple as it provides various methods and keywords that help programmers to implement the logic of code in fewer lines. Usingfor loop we can iterate a sequence of elements over an iterable like a tuple, list, dictionary, set, String, etc. A sequence consists of multiple items and this item can be iterated usingin keyword andrange keyword in for loop.
Syntax of for loop:
for variable in Sequence:
#....code...
Syntax of range method
- range(a) -> Generates a sequence of items from 0th index to a-1 index.
- range(a, b) -> Generates a sequence of items from ath index to b-1 index.
- range(a, b, step) -> Generates a sequence started from a, end at b-1 and c is difference.
Note:Range method is not used when any type of sequence won't support indexing.
Example 1: Python For Loop using List
A list is used to store the multiple values of the different types under a single variable. It is mutable i.e., items in the list can be changed after creation. The list is created by enclosing the data items with square brackets.
Python3# list creationli=["Geeks","for","Geeks"]foriinli:print(i)print('-----')foriinrange(len(li)):print(li[i])
OutputGeeksforGeeks-----GeeksforGeeks
Time complexity:O(n) where n is the length of the list.
Auxiliary space:O(1) as no extra space is used.
Example 2: Python For Loop using Tuple
A tuple is used to store the multiple values of the different types under a single variable. It is immutable i.e., items in a tuple cannot be changed after creation. A tuple is created by enclosing the data items with round brackets.
Python3# tuple creationseq=("Geeks","for","Geeks","GFG","Learning","Portal")foriinseq:print(i)print('-----')foriinrange(3,len(seq)):print(seq[i])
OutputGeeksforGeeksGFGLearningPortal-----GFGLearningPortal
Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(1), as we are not using any additional data structure to store the elements of the tuple.
Example 3: Python For Loop using Dictionary
Dictionary is an unordered collection of items where data is stored in key-value pairs. Unlike other data types like list, set and tuple it holds data as key: value pair. for loop uses in keyword to iterate over each value in a dictionary.
Python3# dictionary creationdic={1:"a",2:"b",3:"c",4:"d"}foriindic:print(i)
Example 4: Python For Loop using Set
A set is an unordered, unindexed, and immutable datatype that holds multiple values under a single variable. It can be created by surrounding the data items around flower braces or a set method. As the set is unindexed range method is not used.
Python3# set creationsetSeq={"unordered","unindexed","immutable"}foriinsetSeq:print(i)
Outputunorderedunindexedimmutable
Example 5: Python For Loop using String
Here we passed the step variable as2in the range method. So we got alternate characters in a string.
Python3# string creationstr="GFG Learning-Portal"foriinstr:print(i,end="")print()foriinrange(0,len(str),2):print(str[i],end="_")
OutputGFG Learning-PortalG_G_L_a_n_n_-_o_t_l_