Saturday, April 14, 2018

Sample Program – Python Dictionary



pythonDictionary.py

# Dictionary - Python
from pip.utils.hashes import FAVORITE_HASH
from email.policy import default
studentInfo = {'rollNo':123,'name':'Avinash','age':8,'class':'III'}

print('*****Student Info*****')
print('Roll No: ',studentInfo['rollNo'],'\nName: ',studentInfo['name'],
      '\nAge; ',studentInfo['age'],'\nClass: ',studentInfo['class'])

fav = {'color':'Violet','fruit':'apple','animal':'tiger'}
print('\nFavourites: ',fav)

#dictionary functions
print('String representation: ',str(fav))
print('Length: ',len(fav))
print('Type: ',type(fav))

#copying a dictionary
fav_2 = dict.copy(fav)
print('fav_2 - copy of fav: ',fav_2)

#access the value by its key
print('Value of color: ',fav.get('color'))

#check if the key exits in the dictionary
print("Is the key fruit exists:"'fruit' in fav)

#print the list of values in a dictionary
print('Values of fav: ',fav.values())

#print the list of key in a dictionary
print("Keys of fav: ",fav.keys())

#print the list of items in a dictionary
print("Items of fav: ",fav.items())

#create a dictionary from keys from another dictionary with default value none
fav_3 = dict.fromkeys(fav)
print("fav_3: ",fav_3)

#clear a dictionary
dict.clear(fav_3)
print('fav_3 after clear: ',fav_3)

#update a dictionary with another dictionary value pair
fav_3.update(fav_2)
print('fav_3 after updating with fav_2: ',fav_3)

#adds a key if not present
fav_3.setdefault('place')
print('fav_3 after setdefault key: ',fav_3)


output:
*****Student Info*****
Roll No:  123
Name:  Avinash
Age;  8
Class:  III

Favourites:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
String representation:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
Length:  3
Type:  <class 'dict'>
fav_2 - copy of fav:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
Value of color:  Violet
Is the key fruit exists: True
Values of fav:  dict_values(['Violet', 'apple', 'tiger'])
Keys of fav:  dict_keys(['color', 'fruit', 'animal'])
Items of fav:  dict_items([('color', 'Violet'), ('fruit', 'apple'), ('animal', 'tiger')])
fav_3:  {'color': None, 'fruit': None, 'animal': None}
fav_3 after clear:  {}
fav_3 after updating with fav_2:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
fav_3 after setdefault key:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger', 'place': None}




Sample Program – Python Tuple



pythonTuple.py

custInfo = ('CusId','CusName','City','Mobile')
custData = (1001,'Anitha','Pondicherry',876712349)

print("*****Cust Info*****")
print(custInfo[0],':',custData[0])
print(custInfo[1],':',custData[1])
print(custInfo[2],':',custData[2])

tuple_1,tuple_2 = (1,2,3,4),(1,7,8)
empty_tuple = ()

print('\nTuple 1: ',tuple_1,'\nTuple 1 count: ',len(tuple_1),'\nMax Value: ',max(tuple_1),'\nMin Value: ',min(tuple_1))
print('Tuple 2: ',tuple_2 )

#print empty tuple
print("Empty Tuple: ",empty_tuple)

#Assign value to empty tuple
empty_tuple = tuple_1 + tuple_2
print("Empty Tuple after assigning values: ",empty_tuple)

#converting list to a tuple
list_1 = ['Grapes','Apple','Orange']
list2Tuple = tuple(list_1)
print("List after converting it to tuple: ",list2Tuple)

#displaying a range from a tuple
print("Tuple_2 with first 2 values: ",tuple_2[0:2])
print("Tuple_2 with from 2nd index: ",tuple_2[1:])


output:
*****Cust Info*****
CusId : 1001
CusName : Anitha
City : Pondicherry

Tuple 1:  (1, 2, 3, 4)
Tuple 1 count:  4
Max Value:  4
Min Value:  1
Tuple 2:  (1, 7, 8)
Empty Tuple:  ()
Empty Tuple after assigning values:  (1, 2, 3, 4, 1, 7, 8)
List after converting it to tuple:  ('Grapes', 'Apple', 'Orange')
Tuple_2 with first 2 values:  (1, 7)
Tuple_2 with from 2nd index:  (7, 8)


Sample Program – Python List



pythonList.py

subjects = ['Maths','Physics','Chemistry']
marks = [190,178,185]

print("*****Score Details*****")
print(subjects[0],':',marks[0])
print(subjects[1],':',marks[1])
print(subjects[2],':',marks[2])

list_1,list_2 = [1,2,3,4],[1,7,8]

print('\nList 1: ',list_1,'\nList count: ',len(list_1),'\nMax Value: ',max(list_1),'\nMin Value: ',min(list_1))
print('List 2: ',list_2 )

#inserting into list_2
list_2.append(10)
print('List 2 after append: ',list_2 )

#extending list_2 with sequence of values
list_2.extend([9,11,12])
print('List 2 after extend: ',list_2 )

#reversing the list
list_2.reverse()
print("Reverse list: ",list_2)

#finding the index of object in list
print('Index of 9 in list 2: ',list_2.index(9))

#sorting the list desc
list_2.sort(reverse= True)
print("List after sorting: ",list_2)

#removing object from the list
list_2.remove(11)
print("List after removing 11: ",list_2)

#updating object in the list
list_2[0]=13
print("List after updating index 0 with 13: ",list_2)

#displaying a range from a list
print("List with first 5 values: ",list_2[0:5])
print("List from 2nd index: ",list_2[1:])



output:
*****Score Details*****
Maths : 190
Physics : 178
Chemistry : 185

List 1:  [1, 2, 3, 4]
List count:  4
Max Value:  4
Min Value:  1
List 2:  [1, 7, 8]
List 2 after append:  [1, 7, 8, 10]
List 2 after extend:  [1, 7, 8, 10, 9, 11, 12]
Reverse list:  [12, 11, 9, 10, 8, 7, 1]
Index of 9 in list 2:  2
List after sorting:  [12, 11, 10, 9, 8, 7, 1]
List after removing 11:  [12, 10, 9, 8, 7, 1]
List after updating index 0 with 13:  [13, 10, 9, 8, 7, 1]
List with first 5 values:  [13, 10, 9, 8, 7]
List from 2nd index:  [10, 9, 8, 7, 1]



Sample program – Python Strings



pythonStrings.py
#Python strings
string_1 = 'Welcome to python strings' #strings assigned within single quotes
string_2 = "Python strings introduction" #strings assigned within double quotes
print ("String_1: ",string_1,"\nString_2: ",string_2)
print ("String from a given index: " , string_1[0])
print("Substring from given range: ",string_1[0:7])
print ("String Concatenation: ",string_1+"."+  string_2)
print ("String repetition: ",string_1[0] * 3)
print("Check the occurrence of a character in a string: ",'W' in string_1)
print("Check the non-occurrence of a character in a string: ",'W' not in string_1)
print("\nParagraph with triple single/double quotes: " , '''In python \t
multiple line of strings can be represented within triple quotes
''')

output:
String_1:  Welcome to python strings
String_2:  Python strings introduction
String from a given index:  W
Substring from given range:  Welcome
String Concatenation:  Welcome to python strings.Python strings introduction
String repetition:  WWW
Check the occurrence of a character in a string:  True
Check the non-occurrence of a character in a string:  False

Paragraph with triple single/double quotes: In python  
multiple line of strings can be represented within triple quotes



Sample program – Python Numbers



pythonNumbers.py
#Python Numbers
num_1 = num_2 = -123  #assigning same integer value to 2 variables
num_3 = 123.00  #assigning float value to variable
num_4 = 12.6j   #assigning complex number to variable
num_5 = 127 * -254 * 2.3j
print ("num_1 :", num_1, "\nnum_2 :" , num_2, "Type :" , type(num_1))
print ("num_3 :", num_3, "Type :" , type(num_3))
print ("num_4 :", num_4 , "Type :" , type(num_4))
print ("num_5 :" , num_5, "Type :" , type(num_5))

# deleting an object
del num_5


Output:
num_1 : -123
num_2 : -123 Type : <class 'int'>
num_3 : 123.0 Type : <class 'float'>
num_4 : 12.6j Type : <class 'complex'>
num_5 : (-0-74193.4j) Type : <class 'complex'>