In this article, we are going to discuss how for loop is used to iterate over a sequence in Python.
Python programming is very simple as it provides various methods and keywords that help programmers to implement the logic of code in fewer lines. Using for 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 using in keyword and range 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 creation li = [ "Geeks" , "for" , "Geeks" ] for i in li: print (i) print ( '-----' ) for i in range ( len (li)): print (li[i]) |
Geeks for Geeks ----- Geeks for Geeks
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 creation seq = ( "Geeks" , "for" , "Geeks" , "GFG" , "Learning" , "Portal" ) for i in seq: print (i) print ( '-----' ) for i in range ( 3 , len (seq)): print (seq[i]) |
Geeks for Geeks GFG Learning Portal ----- GFG Learning Portal
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 creation dic = { 1 : "a" , 2 : "b" , 3 : "c" , 4 : "d" } for i in dic: print (i) |
1 2 3 4
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 creation setSeq = { "unordered" , "unindexed" , "immutable" } for i in setSeq: print (i) |
unordered unindexed immutable
Example 5: Python For Loop using String
Here we passed the step variable as 2 in the range method. So we got alternate characters in a string.
Python3
# string creation str = "GFG Learning-Portal" for i in str : print (i, end = "") print () for i in range ( 0 , len ( str ), 2 ): print ( str [i], end = "_" ) |
GFG Learning-Portal G_G_L_a_n_n_-_o_t_l_