List in Python...|empty list | retrieve list.

List:

In python a list is a sequence of values called items or elements. The elements can be any datatype like integer,string or float values. The items or elements are placed between the '[]',separated with ','.

Creating List:

The list class define List. A programmer can use list constructor to create a list.

l1=list() l2=list(['b',1,2,3,4,'A']) l3=list([1,2,3,4,5,6.06]) print(l1) print(l2) print(l3)

Output:

[] ['b', 1, 2, 3, 4, 'A'] [1, 2, 3, 4, 5, 6.06]

Creating List without using Constructor:

l1=[] l2=['b',1,2,3,4,'A'] l3=[1,2,3,4,5,6.06] print(l1) print(l2) print(l3)

Output:

[] ['b', 1, 2, 3, 4, 'A'] [1, 2, 3, 4, 5, 6.06]

Retrieve the elemements of List :

The elements of list are access by index operator.

Syntax:

name_of_list_variable [index]

Example:

l1=["Python","Code",2020] print(l1[0]) print(l1[1]) print(l1[2])

Output:

Python Code 2020

A python program to create empty list and add item according to user need and display them.

l1=[] items=int(input("Enter Numuber of elements to add in list:")) for i in range(0,items): l1.append(input("Enter element to add in list:")) print("The elements are:") print(l1)

Output:

Enter Num. of elements to add:4 Enter element to add:python Enter element to add:panda Enter element to add:jupiter Enter element to add:pycharm The elements are: ['python', 'panda', 'jupiter', 'pycharm']

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.