Monday, 3 December 2018

Python List : Adding element to List

Adding an element to the list

We can add elements to the list using append, insert and extend functions.

#Initializing empty list
alist = [] 

Append function

It is used to add elements at the end of the list.
Append function takes only one argument at a time i.e using append function we can add only one element at a time.

#Adding element in the list using append function
alist.append(1)
alist.append(3)
alist.append(5)
print('List after addition of 3 elements :  ', alist ) 

Output :



Insert function

It is used to add an element at the specified index position of the list.
#Adding an element at a specific index using insert function. 
alist.insert(1,2)
print('List after performing insert operation :  ' , alist)
#Here we are adding element 2 at index position 1

Output :



Extend function

It is used to add a sequence of elements to the list.
This function adds elements at the end of a list.
Extend function takes a list of elements as the argument.
#Adding multiple elements to the list
alist.extend( [ 4 , 'python' , 8.97 , (91,90,89) ] )
print('List after performing extend operation :  ' , alist)
#Here we are adding elements 4 , ' python ',8.97 and tuple (91,90,89)

Output :



Python Lists : Creating List

List


The list is  simplest data structure in python which is used to store a collection of data.
The list is a collection of items like String, Numbers, Lists, Tuples, Dictionaries.
Lists are enclosed in square brackets [] .
Each value in the list is assigned to the index value.
An index value of the list is start from 0 .
Each item in list is separated by comma(,) .
List is Mutable data structure which means we can add , remove ,  replace , append the elements of list whenever we want.

Example : alist = [ 2 , 3 , 4 , 'a' , 7.67 , 'python' ]

How to create List...?

#Creating empty list
#Creating empty list means creating object of list data stucture with no values.
alist = []
print('Initialize empty list :  ' , alist )

#Creating a list of strings
alist = ['hii','this','is','Python']
print('List with string values :  ' , alist )

#Creating a list of integers
alist = [ 1 ,3, 5 , 7]
print('List with integer values :  ' , alist )
#You can also create a list with float , long , complex type integers

#Creating list having mixed type of values(stings , numbers)
alist = [ 345 , 'python' , 6.9836 , 218748682686836753656 , 'program' , 6+8j ]
print('List with mixed type of values :  ' , alist )

#Creating list with list type data structure
#List with lst type of items also called as nested lists or multidimensional list.
alist = [[3,4,5],['a','b','c'],[3.67,9,'hello']]
print('Multi-Dimensional List :  ' , alist )

#Creating list with the use of tuples
alist = [(4,5,6),('a','v','d'),(3,7,1)]
print('List with the use of tuples :  ' , alist )

#Creating list with the use od dictionaries
alist = [{1:'Python' , 2 :'Programming'},{'a' : 97 , 'b' :98 }]
print('List with the use of dictionaries :  ' , alist )

Output :


A list may contain duplicate values with their distinct positions .
List store its element according to the index positions so we can store the same element multiple index positions, this can duplicate that element in a list.
If the list contains duplicate elements then that duplicate elements have different index positions.

#Creating a list with duplicate values
alist = [ 'a', 1 ,3 , 3 , 7 ,'a' , 5 , 7]
print('List with duplicate values :  ' , alist )

Output :

In above example ,three elements of list have duplicates
element 'a' have two index positions i.e. 0 and 5 .
element 3 have two index positions i.e. 2 and 3 .
element 7 also havetwo index positions i.e. 4 and 7 .
Because of different index positioning these elements considered as distinct elements not as same element .

Saturday, 13 May 2017

Greatest number among 3 numbers which are user entered

#Greatest number among 3 numbers which are user entered

#Accepting 3 numbers from user
a=int(input('enter number:'))
b=int(input('enter number:'))
c=int(input('enter number:'))

# find which is greatest number
if a>b:
    if a>c:
        print(a,'is greatest number')
    else:
        print(c,'is greatest number')
else:
    if b>c:
        print(b,'is greatest number')
    else:
        print(c,'is greatest number')

OUTPUT:

Fibonacci series using for loop

#Fibonacci series using for loop

#Asking user that how much terms he want in the fibonacci series
n=int(input("How much terms you want:"))

#displaying fibonacci terms
a,b=0,1
print("fib series=\n",1)
for i in range(1,n):
    s=a+b
    print(s)
    a,b=b,s

OUTPUT:

Dictionary of Days and their Temperature

#Dictionary of Days and their Temperature

#defining an empty dictionary to add days and their temperature
d={}

#defining a list of name of days
days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

#take entry of temperature from user
for i in days:      
    temp=int(input("enter temperature of "+i+" :"))
    d[i]=temp

#to display the day and its temperature in the form of dictionary
print('DAY & TEMPERATURE')
print(d)

#find maximum temperature
tmax=max(d.values())
print('maximum temperature' , tmax)

OUTPUT:


Monday, 9 January 2017

Linear search example

Linear search example

The linear search is used to find an item in a list.
The items do not have to be in order.
To search for an item, start at the beginning of the list and continue searching until either the end of the list is reached or the item is found.

#simple program to explain linear search by using funcion


#defining the function
def linearsearch(list,s_element):
    for i in range(0,len(list)):
        if list[i]==s_element:
            return i
    return -1

#defining any list with elements
alist=[12,5,3,0,8]

#displaying the list
print(alist)

#accepting element from user to search
se=int(input("enter element to search:"))

#defining variable to store the result of linear search
loc=linearsearch(alist,se)

#displaying the result
if loc==-1:
    print("element is not in a list")
else:
    print("index of element in list:",loc)

OUTPUT :





Sunday, 8 January 2017

basic numeric functions performing on list



#defining list
l=[]

#storing flag in one variable
ch='y'

#giving condition
while ch!='n':
    n=int(input('enter number:'))  #accepting values from user
    ch=input("do u want to continue:") #taking choice from user
    l.append(n)  #appending the values in list

#displaying list
print("list of values:" ,l)
print("length of list:",len(l))
print("sum of numbers from list:",sum(l))
print("minimum number from list:",min(l))
print("maximum number from list:",max(l))
print("average of numbers from list:",sum(l)/len(l))