List concepts in python part 1

By
What is List
  • A List can store the sequence of various types of data.
  • A list is a container data type.Container is a an entity which contains multiple data items.It is also known as a collection.
  • There are some container data types in python.
  1. List
  2. Tuple
  3. Set
  4. Dictionary
  • Container data type are also known as compound data types.
  • Lists are mutable in python.It means we can change the list element after it created.
  • A list can be collection of Items with different data types(int,string,float etc.).
  • A list can contain similar and dissimilar both data types.
  • The items in the list are separated with comma(,) and enclosed with the square brackets [].
e.g.
Dissimilar items:-It contains collection of different data types.
list1=['ram',20,10.89,'cow','tiger']
Similar items:-It contains collection of same data types.
list2=[1,2,3,4,5,6,7,,8,9,10]
list1=['ram',20,10.89,'cow','tiger']
list2=[1,2,3,4,5,6,7,8,9,10]
print(type(list1))
print(type(list2))
Output:-
<class 'list'>
<class 'list'>
Properties of List:-There are some properties of list as given below.
  • Lists are mutable (changeable).
  • The Items in a list can be repeated.Means a list may contain duplicate items.
  • The lists are ordered.
  • The items in the list can be access by index.
  • A list contains similar and dissimilar  both data type items.
  • A list always uses a square brackets [] and each item in list separated by comma(,).
e.g.
list1=['ram',20,10.89,'cow','tiger']
list2=['ram',20,10.89,'tiger','cow']
list3=['ram',20,10.89,'tiger','cow']
print(list1==list2)
print(list2==list3)
Output:-
False
True
Descriptions:-
  • list1 and list2 have same elements but index value of items their items are changed.Therefore list1 and list2 are not equal.
  • list2 and list3 are ordered list because both list of items have same index value.Therefore  list2 and list3 are equal.
Accessing List Elements:-
  • A list item can be accessed using index value like string.Hence it is known as sequence types.
  • A list index value start from zero like string.
e.g.
list=[10,20,30,40,50,60,70,80]

Now access the list value:-
list[0]=10
list[1]=20
list[2]=30
list[3]=40
list[4]=50
list[5]=60
list[6]=70
list[7]=80
  • A list can be sliced like string.
e.g.
Syntax:-
list(start:stop:step)
start:-It denotes the starting index value of the list.
stop:- It denotes the last index value of the list.
step:- It denotes the increment value of the ordered list.

Access the element in forward direction:-
vowel=['a','e','i','o','u']
print(vowel[0])
print(vowel[1])
print(vowel[2])
print(vowel[3])
print(vowel[4])
#slice the elements
print("slice element values:")
print(vowel[:])
print(vowel[1:4])
print(vowel[1:])
print(vowel[1:4:1])
print(vowel[:4])
Output:-
a
e
i
o
u
slice element values:
['a', 'e', 'i', 'o', 'u']
['e', 'i', 'o']
['e', 'i', 'o', 'u']
['e', 'i', 'o']
['a', 'e', 'i', 'o']
>>> 
Access the element in backward direction:-
vowel=['a','e','i','o','u']
print(vowel[-1])
print(vowel[-2])
print(vowel[-3])
print(vowel[-4])
print(vowel[-5])
#slice the elements
print("slice element values:")
print(vowel[:])
print(vowel[:-1])
print(vowel[-3:-1])
print(vowel[-3:])
print(vowel[-1:-4:-2])
print(vowel[:-5])
Output:-
u
o
i
e
a
slice element values:
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o']
['i', 'o']
['i', 'o', 'u']
['u', 'i']
[]
  • We can print entire list by using the name of any list.
e.g.
animals=['Lion','Tiger','Horse','Cow']
print(animals)
Output:-
['Lion', 'Tiger', 'Horse', 'Cow']
  • A list of all keywords in python is also obtained as a list.
import keyword
print(keyword.kwlist)
Output:-
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Updating the List values:-
  • We know ,lists are mutable(changeable) but strings are immutable (unchangeable).
  • We can update the values in the list by using assignment and slice operator in python.
  • Python also provides some methods like append() and insert() which can be used to insert value to the list.
  • The list elements can also be deleted by using del keyword.Python provides remove() method if we don't know which element is to be deleted from the list.
e.g.
numbers=[1,2,3,4,5,6,7,8,9,10]
print(numbers)
#update the value of 3rd index in the list
numbers[3]=40
print(numbers)
#Add/update multiple values in the list
numbers[4:8]=[50,60,70,80]
print(numbers)
#Add the value at the end of the list by two method
numbers[9]=[100]
#numbers[-1]=[100]
print(numbers)
Output:-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 40, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 40, 50, 60, 70, 80, 9, 10]
[1, 2, 3, 40, 50, 60, 70, 80, 9, [100]]
>>> 
program 1: Write a python program to add items in empty list by user.
#Declaring empty list
lst=[]
num=int(input("Enter the number of element in the list:\n"))
for i in range(0,num):
    lst.append(input("Enter the Value:\n"))
#print the list items
str=input("Do you want to print list values press: yes/no \n")
if(str=='yes'):
    for j in lst:
        print(j,end=" ")
elif(str=='no'):
    print("Sorry!I am unable to print list values...")
else:
    print("Please enter valid data...")
Output:-
Enter the number of element in the list:
5
Enter the Value:
1
Enter the Value:
56
Enter the Value:
78
Enter the Value:
90
Enter the Value:
55
Do you want to print list values press: yes/no 
yes
1 56 78 90 55 

For More...
Watch complete Lecture 25 Video:-

0 comments:

Post a Comment

Powered by Blogger.