Python

Using for loop through a list in Python

We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

Looping in Python provides a feasible way to deal with each element of an object for particular operations using just a single block of statements. A for loop handling a list in Python creates a temporary variable first, then do a series expected operation with the block. Although the temporary variable name can be chosen with any string just like naming any other objects in Python, it is often identified with a single variable with respect to a list with chosen plural names. The following example shows each member of a list ‘member’ is printed out with a for loop.

members = ['Shirley', 'Wilson', 'Dudu','Maomao','Mico','Mia','Miaomiao']
for member in members:
    print(member)     
#output
Shirley
Wilson
Dudu
Maomao
Mico
Mia
Miaomiao

Just note that Python uses 4 space indentation for a loop, similar as for function definition.

With a for loop, we can also do a little more on elements of list. For example, each member’s name title form is concatenated with extra strings into a sentence, before they are printed out.

members = ['Shirley', 'Wilson', 'Dudu','Maomao','Mico','Mia','Miaomiao']
for member in members:
    print(member.title() + ", you are welcome")  
    print("I can't wait to see your combing back, " + member.title() + ".\n")     
    
print("Thank you everyone, that was a great meeting!")
#output
Shirley, you are welcome
I can't wait to see your combing back, Shirley.

Wilson, you are welcome
I can't wait to see your combing back, Wilson.

Dudu, you are welcome
I can't wait to see your combing back, Dudu.

Maomao, you are welcome
I can't wait to see your combing back, Maomao.

Mico, you are welcome
I can't wait to see your combing back, Mico.

Mia, you are welcome
I can't wait to see your combing back, Mia.

Miaomiao, you are welcome
I can't wait to see your combing back, Miaomiao.

Thank you everyone, that was a great meeting!

And you can see that the last statement has no indentation on the left side, so it does not belong to the for loop block. This sentence is printed out only once.

For more tutorials of learning Python, you can watch Python full video in 10 hours from our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago