Manipulation the Dictionary

d={10: 'iprg' , 22: 'Nan', 33:'Kool',8: 'Jool','y': 89,'tt':'toy',7:90 }
for i in d:                                
  print i,d[i]    # printing all the values in Dictionary          
print '\n'
print d[8]  #printing values of the individual keys
print '\n'
print d['y']  #printing values of the individual keys
print '\n'
print d[7]  #printing values of the individual keys
print '\n'
d[33]='hello world'  #updating the key 33 with new value
d[22]='pythonforengineers'   #updating the key 22 with new value
for i in d:                                
  print i,d[i]    # printing all the updated values in Dictionary    
del d[10]     # deleting key:value pair using key value -> 10
print'\n Dictionary after deleting a value:\n', d
d.clear()   #clearing all values
print '\n Dictionary after clearing all values:', d
del d   # Removing the Dictionary
try:
  print d
except:
  print 'Some error has occurred'
 

Creating a dictionary and traversing

d={10: 'iprg' , 22: 'Nan', 33:'Kool',8: 'Jool' } # Creating a dictionary with key: value pair

for i in d:                                     # Here key and the vaule can be any type.
  print i,d[i]                                  # Accessing elements in the dictionary

# Order in which they display is also different in output

print d.keys()  # print all keys in a dictionary

print d.values()  # print all values in a dictionary


Tuples Manipulation

try:
      l=['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
      print 'List :',l
      tup1 = tuple(l) #Converts a list into tuple
      tup2=(1,2,3,4,5,6,7)
      #Adding two Tuples
      tup=tup1+tup2
      print 'New added tuple :',tup
      ntup=tup2*2
      print 'New multiplyed tuple:',ntup
      p=max(tup1)
      print 'The tuple with max value:',p
      p1=min(tup1)
      print 'The tuple with min value:',p1
      #Compares elements of both tuples and same return 0 otherwise -1
      k=cmp(tup2,ntup)
      print k
      #Delete Tuple
      del tup
      print tup
except:
      print 'Tuple does not exist'

Creation and accessing values in Tuples

t=(22,33,44,55,66,77,88,99) #A tuple is a sequence of immutable Python
print 'Tuple elements : ',t
#Accessing Values in Tuples
print 'Tuple first element :',t[0]
print 'tuple last element :',t[len(t)-1]
print '\n'
for i in t:
      print 'Elements in tuple: ',i
print '\n'
for i in range(len(t)):
      print 'Index :',i,'Vaule:',t[i]
   






Different indexing in string

fruit='apples'
l=len(fruit)
print 'Length of string:',l
print  fruit[0:4]    #[m:n]  m is starting index and n will upto n-1 index
print  fruit[-4]      # Index starting from last element as -1, second last element as -2 so on
print  fruit[::-1]   # Reverse of a string
print fruit[:2]
print fruit[2:]

try:
      print  fruit[6]
except:
      print 'index error'

for i in fruit:
      print '\nChar in the string: ',i

for j in range(l):
      print  '\nIndex and Char in the string: ',j,fruit[j]


Compare the two string based on ascii values

str1=raw_input('Enter the string 1:  ')
str2=raw_input('Enter the string 2:  ')
if str1==str2:
      print 'String are equal'  # It is working based on ascii value  refer http://www.ascii-code.com/
elif str1<str2:
      print 'Str2 is greater than str1'
elif str1>str2:
        print 'Str1 is greater than str2'
elif str1<=str2:
        print 'Str1 is greater than or equal to str2'
elif str1>=str2:
        print 'Str1 is greater than or equal to str2'
elif str1!=str2:
        print 'Str1 is not equal to str2'

Converting a for loop to corresponding while loop

for count in range(0,100,2):
   print 'For:',count

count1 =0
while(count1<100):
  print 'While:',count1
  count1=count1+2

Evaluating expression in Python

x=2
y=3
z=2

print x**y**z  #Answer 512

print (x**y)**z  #Answer 64

print x**(y**z)  #Answer 512


Perimeter and area of a circle and finding Natural Exponential Function

import math
r=int(raw_input('Enter the radius of circle:'))
perimeter=2*math.pi*r
print 'Perimeter of a circle :',perimeter
area=math.pi*(r**2)
print 'Area of acircle :',area

x=int(raw_input('Enter the value of x in y=e**x:'))
y=math.e**x
print 'Natural Exponential Function value :',y

Sorting the numbers in the List

L=int(raw_input("Enter the length of list: "))
p=list()
for i in range(L):
      num=int(raw_input("Enter the elements : "))
      p.append(num)
print '\nUnsorted list is: ',p
for i in range (len(p)):
 for j in range (i+1,len(p)):
  if p[i]>p[j]:
   t=p[i]
   p[i]=p[j]
   p[j]=t
else:
 print '\nSorted List is:' ,p