list in python with examples- Python List -

list in python with examples- Python List

Hello Guys! If you are searching python list topic in details than you are on right place. In this blog you will learn list in python with examples.

Python is regarded as the language capable of handling large volume of data volume of data termed as ‘Big Data’. To handle such a large amount of data Python introduces a special category data type called ‘sequence’. This category comprises of three data types viz. List, Tuple and Dictionary. 

Python provides several functions and methods to access and process the elements of List. By the end of this chapter you will understand the processes like-traversal of list, joining and replicating the List and Slicing the list etc.

INTRODUCTION:

The LIST is one of the sequence data type supported by Python. It can be defined as a collection of values of any data type. A List is always depicted in square brackets. The following are some example of LIST in python.

[] #Empty List
[1,2,3,4,5] #List of Integers
[1.2,2.3,3.4] #List of floating point
[‘x’, ‘y’, ‘z’] #List of Characters
[“Delhi”, “Mumbai”, “Chennai”, “Kolkata”] #List of Strings
[1, “Rajesh”, 12.5,2 “Kiran”, 45.7] #List of Mixed values

One of the most important features of Python List is that it is one of the mutable sequence data type i.e once a List is created and values are stored in it, we can change any of its element in place.

Creating a List:




In Python, the sequence data type List can be created in three ways

1. By Assigning Values
2. Through other sequence
3. Dynamically via Keyboard

1. Creating List by assigning values:

To create a List by assignment method, put a number of data values in square bracket and assign it to a variable.
The data values in square bracket must be separated by commas and the variable to which it is assigned becomes a Python List.

Syntax : <list name>= [<comma separated list of values>]
Example : num=[23,65,42,82,27,19]

print() statement can be used to print the whole List of values, as

Syntax : print(<list name>)
Example : print(num)

 Write a program to create a list of given marks of 10 students in a subject. Also print the List Marks are-
41,39,44,21,32,47,37,28,48,30.

marks= [41,39,44,21,32,47,37,28,48,30]
print(marks)

2. Creating List through other sequences:

We can also create List using any other sequence data type such as string,tuple etc. with the help
of built in Python function list(). The general format is a follows

Syntax : <List name>= list(<any sequence>)

Example : grade= list(“ABCF”)
print(grade)
Output-[‘A’,’B’,’C’,’F’]

In the above example we have created a list ‘grade’ using a string “ABCF”. The list() function splits the string into characters and uses
each character as element to creates a List. Following is another example of creating a python List using Python tuple

t= (21.5,23.0,23.8,24.2,25.7)
temp=list(t)
print(temp)

Output-

[21.5,23.0,23.8,24.2,25.7]

In the ablove example , we have created a tuple ‘t’ with temperature of a city in past 5 days. Next, we used list() function to create a list ‘temp’
with the values of tuple. Finally print() statement is used to print the List.

Write a program to create a List using a tuple containing price of an item in 5 cities. Also print the list on screen.

p=(110.0,98.50.105.50,101.70,108.50)

price=list(p)
print(price)

3. Creating List dynamically via keyboard:

Using input() statement within eval() function we can create a List dynamically. The input()
statement will take input from the user as a string and eval() function will separate values as element of Python List. Finally list()
function will convert will these values in a List. In this case user has to input values separating with comma.

Syntax : <variable> = eval(input(“<any message>”))
<list name> = list(<variable>)

Example : n=eval(input(“Enter three number :”))

nlst=list(n)

Q) Write a program to input 5 subject marks and create a list using them.

m=eval(input(“Enter 5 subject marks: “))
mlist=list(m)
print(mlist)




Accessing the List:

Python List is a mutable sequence data type in which each element can be edited in its place in the List.For editing element we need to access individual element; and Python allow us to access list in both the ways: Whole List at once and Individual element one by one.

Accessing whole List: Python List is a collection of values of several data types stored with a single name.
All the elements of List can do displayed on screen by using List name with print() statement.

Syntax-1 : print(<list name>)

Example : marks= (43,34,27,31,22)
print(marks)

Output : [43,34,27,31,22]

Explanation : in the above example we have created a list of five subject marks with the name ‘marks’. When we print the list name,
all the element gets printed on screen.

Syntax-2 : print(*<list name>)

Example : marks= [43,34,27,31,22]

print(*marks)

Output : 43 34 27 31 22

Explanation : in the above example we have created a list of five subject marks with the name ‘marks’. When we print the list name with asterisk sign (*), only the elements get printed on screen without square bracket and comma.

Q) Write a program to create a list of roll no, name and marks obtained of three students and print all the elements on screen.

student=[1,’Naman’, 42,2, ‘Saksham’,45,3, ‘juhi’, 43]
print(student)
print()
print(*student)

Accessing individual element: Once a List is created and elements are stored in it, the position of each element in the List is
assigned a number. The position of element in the List is called Index and index numbering begins with 0. The diagram below
shows the index position of elements in a List.

num=[11,22,33,44,55,66,77,88]

To access elements of a List individually we write List name followed by index of the that element in square bracket in the following
format:

Syntax : <List name>[<index of element>]
Example : marks[0]
the above example will display first element of List ‘marks’. ie 11

Q) Write a program to create a list of numbers and print all the numbers separately. Also calculate and print their total.
tot=0
num=[12,65,42,75,26,78,52,48,50,26]

print(“Forward printing……”)
for a in range(10):

               print(num[a], end=” “)

tot=tot+num[a]

For More Details Watch our class on Youtube by click on the below link

https://youtu.be/rEAVBhM86OY

Traversing the List:




Accesing and processing each and very element of the List is called Traversing the List.For traversal of the List, rather than accessing elements using index number we for() loop
to access elements from the List.

Syntax : for<variable>in<list name>:

body of loop

Example : for a in marks:

print(a)

Q) Write a program to create a List of 10 numbers. While traversing the List print all its elements and count how many of them are positive, negative and zero.

p=n=z=0
num=[5,-8,2,0,6,-2,4,0,6,-1]
print(“List elements….”)
for a in num:
print(a,” “,end=””)
if(a>0):
p=p+1
elif a<0:
n=n+1
else:
z=z+1
print(“\n\nNo.of positives=”, p)
print(“No. of negatives=”, n)
print(“No. of zeros=”, z)

For More Classes Subscribe Our Youtube channel

https://www.youtube.com/@olevelguruji

Also Check 

python for loop syntax – Looping Statement

Control statements in Python-Conditional Statement

Leave a Comment