You are on page 1of 3

Python For Loop - Basic Examples

Python For Loop

For Loop is used to loop or iterate through a list of data.


The List of data can be anything.
Example of Lists:
List of Heart Surgeons in mumbai
List of Commerce Colleges in Mumbai

Example of Python For Loop:

countries = [

'India',

'Russia',

'China',

'America',

'Canada'

print(countries[0])

for index in range(len(countries)):

print(countries[index])

Advanced Example of For Loop

countries = [

'India',

'Russia',

'China',

'America',

'Canada'

print(countries[0])
x = range(len(countries))

for i in x:

print(countries[i])

Python Collections: Python List

Python List a type of variable which can store multiple values.


Unlike normal variables which can store a single value.
Python List can store multiple values.
Means a single variable can have multiples values.
Example: Difference between a Normal Variable and a List Variable

# PYTHON VARIABLES STORE INDIVIDUAL DATA

student_name = 'Aagam Shah'

# PYTHON LISTs STORES MULTIPLE DATA

students = ['Aagam Shah','Afiyaa','Annette Ronnie']

Operations on Python Lists:

# create a list

countries = ['india','china','russia','germany']

# insert in a list

countries.append('canada')

# insert at specific index

countries[1] = 'taiwan'

# sort the list in ascending order

countries.sort()
# sort the list in descending order

countries.sort(reverse=True)

# copy the list

nations = countries.copy()

You might also like