Tuple concepts in python

By // No comments:
What is Tuple:-
  • Tuple is usually a collection of heterogeneous objects enclosed within ()(small bracket).
  • A tuple is the collection of comma (,)separated values enclosed with small brackets.
  • The parentheses (small bracket) are optional in tuple
    .
e.g.
tuple1=(10,20,180,105)
tuple2=('Horse','Lion','Cow')
tuple3=("MNT LAB")
tuple4=("MNT LAB",)
print(type(tuple1))
print(type(tuple2))
print(type(tuple3))
print(type(tuple4))
Output:-
<class 'tuple'>
<class 'tuple'>
<class 'str'>
<class 'tuple'>
>>> 
Properties of Tuple:-
There are some properties of tuple as given below:-
  • An empty tuple can created as given below.
3

List concepts in python part 2

By // No comments:
There are some basic list operations in python as given below.
  • Lists are mutable but strings are immutable.
e.g.
animals=['cow','horse','lion','tiger','deer']
numbers=[1,2,3,4,5,6,7,8,9,10]
#Replace list items
animals[4]='dog'
numbers[2:5]=[30,40,50]
print(animals)
print(numbers)
#delete items from 7 to 9 in numbers list
numbers[6:9]=[]
print(numbers)
#clear all items in the numbers list
numbers[:]=[]
print(numbers)
Output:-
['cow', 'horse', 'lion', 'tiger', 'dog']
[1, 2, 30, 40, 50, 6, 7, 8, 9, 10]
[1, 2, 30, 40, 50, 6, 10]
[]
>>> 
  • The Repetition operator is used to repeat  the elements in the list multiple times as given below.
e.g.
list1=[10,20,30,40,50]
list2=list1*2
print(list2)
Output:-
[10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
>>> 
  • The concatenation operator is used to concatenate the one list with other list.
e.g.
list1=[10,20,30,40,50]
list2=['a','b','c','d','e','f']
list3=list1+list2
print(list3)
Output:-
[10, 20, 30, 40, 50, 'a', 'b', 'c', 'd', 'e', 'f']
>>> 
  • The membership operator returns True if a particular element exists in a list otherwise False.
e.g.
list=['a','e','i','o','u']
print('a' in list)
print('o' in list)
print('p' in list)
Output
True
True
False
  • A list can be iterated by using for loop as given below.
e.g.
list=['Ram',20,3.5,'lion','neha']
for i in list:
    print(i)
    #print(i,end=" ")
Output:
Ram
20
3.5
lion
neha
>>> 

There are some basic operations can be performed on a list.
  • Create a list
e.g.
list=['Ram',20,3.5,'lion','neha']
  • Concatenation (addition) of two more lists.
e.g.
list1=[10,20,30,40,50]
list2=[60,70,80,90,100]
list=list1+list2
print(list)
Output:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
>>> 
  • del() function is used to delete the elements in the list.
e.g.
list1=[10,20,30,40,50,60,70,80,90,100]
#delete the 3rd index value from the list
del(list1[3])
print(list1)
#delete the 6th index to last index from the list
del(list1[6:])
print(list1)
#delete the whole list
del(list1[:])
print(list1)
Output:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
[10, 20, 30, 50, 60, 70]
[]
>>> 
  • len() function is used to calculate the number of items in given list.
e.g.
list1=[10,20,30,40,50,60,70,80,90]
a=len(list1)
print("Lenght of given list is = ",a)
Output:
Lenght of given list is =  9
>>> 
  • list() function is used to convert the string to a list.
e.g.

str="AMERICA"
a=list(str)
print("Convert the string value to the list value: ",a)
Output:
Convert the string value to the list value:  ['A', 'M', 'E', 'R', 'I', 'C', 'A']
>>> 
  • max() function is used in python to return the maximum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Maximum value of list is=",max(str1))
print("Maximum value of list is=",max(str2))
Output:-
Maximum value of list is= R
Maximum value of list is= 8
>>> 
  • min() function is used in python to return the minimum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Minimum value of list is=",min(str1))
print("Minimum value of list is=",min(str2))
Output:-
Minimum value of list is= A
Minimum value of list is= 1
>>> 
  • sorted() function is used to return the sorted list without any change in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[70,50,60,40,20,80,30,40]
print("Sorted value of the list is=",sorted(str1))
print("Sorted value of the list is=",sorted(str2))
Output:-
Sorted value of the list is= ['A', 'A', 'C', 'E', 'I', 'M', 'R']
Sorted value of the list is= [20, 30, 40, 40, 50, 60, 70, 80]
>>> 
  • sum() function is used to return the sum of all the list values.
e.g.
str1=[70,50,60,40,20,80,30,40]
print("sum of all list value is=",sum(str1))
Output:-
sum of all list value is= 390
>>> 
  • Comparison between two list is possible.Comparison is done item by item till there is mismatch.
e.g.
a=[1,5,10,15,20]
b=[1,5,10,30,3]
if(a<b):
    print("a is less than b")
elif(a==b):
    print("a is equal to b") 
else:
     print("b is less than a")
Output:-
a is less than b
>>> 

List Methods:- 
List methods are accessed using the syntax list.function().
There are important methods used in list operations as given below:-
  • list.append():-This is used to add the new items at the end of the list.This Method takes only one argument.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.append(90)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60, 70, 90]
>>> 
  • list.remove():-This method is used to delete the item from the list.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.remove(40)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 50, 60, 70]
>>> 
  • list.pop():-This method is used to remove the last item from the list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
list.pop()
print(list)
#delete the 4th index value in the list
list.pop(4)     
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 60]
>>> 
  • list.insert():-This method is used to insert the element at the specified position in the list.This method takes two argument.first argument denote the index value and second argument denote item which you want to add in the given list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
#insert the item at the 4th index in the list
list.insert(4,122)
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 122, 50, 60, 70]
>>> 
  • list.reverse():-This is used to reverse the element in the list.
e.g.
list=[2,4,6,8,10,12,14,16]
list.reverse()
print(list)
Output:-
[16, 14, 12, 10, 8, 6, 4, 2]
>>> 
  • list.sort():-This is used to sort the items in the list.
e.g.
list=[70,20.5,40,50,60,16]
list.sort()
print(list)
Output:-
[16, 20.5, 40, 50, 60, 70]
>>>  
  • list.count():-This is used to return the number of times element appears in the list.
e.g.
list=[16,40,40,50,60,70,40,20,40]
a=list.count(40)
print("Above given number present in the list: ",a)
Output:-
Above given number present in the list:  4
>>> 
  • list.index():-This used to find the index of the particular item in the list.
e.g.
list=[16,40,50,60,70,20,40]
a=list.index(70)
print("70 is present in the list at index : ",a)
Output:-
70 is present in the list at index :  4
>>> 
List Varieties:-
There are some list varieties as given below:-
  • It is possible to create new list from another.
e.g.
list1=[16,40,50,60,70,20,40]
list2=list1
print(list1)
print(list2)
Output:-
[16, 40, 50, 60, 70, 20, 40]
[16, 40, 50, 60, 70, 20, 40]
>>> 
Note:-list1 and list2 are pointing the same list.
  • It is possible to create a list of lists.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,4,6,8,10,12,14]
c=[a,b]
print(c[0][4],c[1][6])
#c[0][4]-> 0th list(first list) of 4th index
#c[1][6]-> 1th list(second list) of 6th index
Output:-
9 14
>>>
  • It is possible that a list may be embedded in another list.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,a,4,6,8,10,12,14]
print("After embedded list is: ",b)
Output:-
After embedded list is:  [2, [1, 3, 5, 7, 9, 11, 13], 4, 6, 8, 10, 12, 14]
>>> 
  • It is possible to unpack a list within a list using the * operator.
e.g
a=[1,3,5,7,9,11,13]
b=[2,4,6,*a,8,10,12]
print("After unpacked list is: ",b)
Output:-
After unpacked list is:  [2, 4, 6, 1, 3, 5, 7, 9, 11, 13, 8, 10, 12]
>>> 
Program:1 
How to add items in empty list by user manually.
#declare the empty list
list=[]
num=int(input("Enter the number of elements in the list : "))
for i in range(0,num):
    #The input is taken by user and add to the list
    list.append(input("Enter the value:"))
#print the list
for j in list:
    print(j,end="")
Output:-
Enter the number of elements in the list : 10
Enter the value:1
Enter the value:2
Enter the value:3
Enter the value:4
Enter the value:5
Enter the value:6
Enter the value:7
Enter the value:8
Enter the value:9
Enter the value:10
12345678910
>>> 
Program:2
Write a program to remove the duplicate items of the list.
l1=[10,20,30,40,50,20,20,70,10,20,30]
#declare the empty list
l2=[]
for i in l1:
    if i not in l2:
        l2.append(i)

print("After remove duplicate item, list is= ",l2)
Output:-
After remove duplicate item, list is=  [10, 20, 30, 40, 50, 70]
>>> 
Program:3
Write a program to find the common element between two list.
l1=[1,3,5,7,9,11]
l2=[2,4,5,8,10,12,9]
for i in l1:
    for j in l2:
        if(i==j):
            print("The common element is: ",i)
Output:-
The common element is:  5
The common element is:  9
>>> 
For More..
Watch complete Lecture 25 Video:-


3

List concepts in python part 1

By // No comments:
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 [].
3

How to use break,continue and pass statements in python with examples

By // No comments:
There are three most important statements are used in python program as given below:
  1. break statement
  2. continue statement
  3. pass statement
1.) The break statement:-
  • The break is a keyword in python.It is used to terminate the loop when we use break statement in any python programs.
  • The break statement is used to abort(terminate) the current execution of the loop and the control goes to the next line after the loop.
3

How to use for loop repetition control in python with examples

By // No comments:
For loop is used to iterate over elements of a sequence such as string,tuple,list and dictionary etc.
For loops are used for sequential traversal. 
For loop has two forms:-
1.)  for var in list:
         statement 1
         statement 2
         statement 3
3

How to use while loop repetition control in python with examples

By // No comments:
There are two types of repetition control instructions in python.
  1. while loop
  2. for loop
1.) while Loop:
  • A while loop allows a part of code to be executed as long as the given condition is true.
  • The while loop is used to repeatedly execute instructions as long as expression is true.
  • A while loop is used in the case where the number of iterations is not know in advance.
3

How to use if-else, elif decision control instruction in python with examples

By // No comments:
There are two ways to control the program flow as given below:-
  1. Decision control instruction (e.g. if ,if-else,elif)
  2. Repetition control instruction (e.g. while,for)
Indentation in Python:-
  • Indentation is used to declare a block in python.If two statements are at the same indentation level,then they are the part of the same block.
  • Python doesn't allow the use of parentheses for the block of codes.
  • All the statements of one block are intended at the same level indentation.
  • In this,we will see how the actual indentation takes place in decision control instructions and others in python.
Decision control instruction:-
There are three ways for taking decisions in a program as given below.
  1. if condition
  2. if-else condition
  3. elif condition
3
Powered by Blogger.