Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Loops in Python - For, While and Nested Loops
Next article icon

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])

Output
GeeksforGeeks-----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])

Output
GeeksforGeeksGFGLearningPortal-----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)

Output
1234

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)

Output
unorderedunindexedimmutable

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="_")

Output
GFG Learning-PortalG_G_L_a_n_n_-_o_t_l_

Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp