Enumeration is one of the concepts in python. It deals with collection of things grouped under a common name. It follows an order.
Syntax:
enumerate(iterable,
StartIndex)
where iterable
– it is the object.
StartIndex – it is the beginning
of list. Default value is 0.
Eg:
#Creating a
fruit list
Fruitslist = ['apple', 'banana', 'orange', 'grapes']
#Making the
list as a enumerated list
F_list = enumerate(Fruitslist)
#printing
the enumerated list
print(list(F_list))
Executing
this, the following output is shown in the output screen.
When the startindex value is mentioned in the code, it becomes…
Fruitslist = ['apple' , 'banana' , 'orange'
, 'grapes']
F_list = enumerate(Fruitslist,2)
print(list(F_list))
just
execute this code,you will get the below output.
Python program
to print the items in the list using enumeration:
Let us
create list of vehicles and print it using enumerated list.
#First,create
a list (v_list)
V_list = ['car' , 'bus', 'van' ,'truck']
#create a
loop and make the list as enumerated and print the value until the last element.
for i in enumerate(V_list):
print(i)
# start the
index as 4 and print the list.
print("Using startindex as 4")
for i in enumerate(V_list ,4):
print(i)
Here is the output.
Enumeration : tuple
Tuple is a
set of objects. It can be separated by a comma. Let us create a enumerated tuple.
#create a v_tuple
and print with for loop.
V_tuple = ['car' , 'bus', 'van' ,'truck']
for i in enumerate(V_tuple):
print(i)
The output
is
Enumeration: Strings
String is a collection of
characters. Enumeration in string is given below.
msg = "Welcome to the python
world"
for i in enumerate(msg):
print(i)
while
running this program, it lists all the characters with index.
Enumeration : Dictionary:
Dictionary
is a collection of data with meaning. Let us create a dictionary for vehicles.
#create a
dictionary v_dict and print the elements using for loop.
v_dict = { "car" : "4
wheels" , "bike" : "2 wheels" , "auto" :
"3 wheels" , "truck" : "6 wheels" }
for i in enumerate(v_dict):
print(i)
The output
of the program is:
These are
the some coding samples using enumeration.
No comments:
Post a Comment