# Python dictionary basics operations
data={'Name':'Viv','id':'yom439','Major':'CS','Degree':'MS'}
print (data['Name'])
print (data['Major'])
print ('\n Before adding new key to dictionary');
print("\n",data)
data['Sem']=3 # Adding new key to dictionary
print ('\n After adding new key to dictionary');
print("\n",data)
#Notes: data.clear() clears all entries in dictionary
#Notes: del data; deletes dictionary
# Converting lists into Dictionaries
print '\n Converting lists into Dictionaries'
states={}
numofstates=int(raw_input('\n Enter number of states'))
for i in range(0, numofstates):
statename=raw_input('\n Enter the state:')
states[statename]={}
capitals={}
for j in range(0,numofstates):
capitalname=raw_input('\n Enter the capital name:')
capitals[capitalname]={}
# Step 1: Convert the lists which you want to make dictionary into tuple format using zip()
state_capital=zip(states,capitals)
print '\n'
print(state_capital)
# Step 2: Now convert this tuple into dictionary using dict()
states_and_capitals=dict(state_capital)
print '\n'
print(states_and_capitals)
data={'Name':'Viv','id':'yom439','Major':'CS','Degree':'MS'}
print (data['Name'])
print (data['Major'])
print ('\n Before adding new key to dictionary');
print("\n",data)
data['Sem']=3 # Adding new key to dictionary
print ('\n After adding new key to dictionary');
print("\n",data)
#Notes: data.clear() clears all entries in dictionary
#Notes: del data; deletes dictionary
# Converting lists into Dictionaries
print '\n Converting lists into Dictionaries'
numofstates=int(raw_input('\n Enter number of states'))
for i in range(0, numofstates):
statename=raw_input('\n Enter the state:')
states[statename]={}
capitals={}
for j in range(0,numofstates):
capitalname=raw_input('\n Enter the capital name:')
capitals[capitalname]={}
# Step 1: Convert the lists which you want to make dictionary into tuple format using zip()
state_capital=zip(states,capitals)
print '\n'
print(state_capital)
# Step 2: Now convert this tuple into dictionary using dict()
states_and_capitals=dict(state_capital)
print '\n'
print(states_and_capitals)
Labels: Python Programming
# This program holds basic operations performed by list such as 'Adding new entry', 'Inserting list element', 'Extending list'
# List operations
stud_names=[]
stud_id=[]
cousins=['Vivek','Veerain','Priyanka','Shirisha','Shravya']
friends=['Abhi','pri','Sammmmmy','Sai','Shaaaa','hemu','adii','sowji','murali','venky','Sanndy']
names=[]
print '***** What action you would like to do ***** \n'
print '1. Add New Student name & id\n'
print '2. Insert student details \n'
print '3. Extend lists \n'
operation=input('Enter the operation you would like to perform: ')
if operation==1:
# Use of 'append' keyword
sname=raw_input('Enter the student name: ')
stud_names.append(sname)
cousins.append(sname)
sid=input('Enter student id: ')
stud_id.append(sid)
print('Students:',stud_names)
print('Student Id:',stud_id)
elif operation==2:
# Use of 'insert' keyword
sindex=input('Enter the index position you want to insert the name: ')
sname=raw_input('Enter the student name: ')
stud_names.insert(sindex,sname);
cousins.insert(sindex,sname) # This is just another example
print(cousins)
elif operation==3:
print '**************** Full list of cousins and friends would be ************** \n'
# Use of 'extend' keyword
names.extend(cousins)
names.extend(friends)
print(names)
print('\n sorted names list: ',names.sort())
print('\n Length of names list is :',len(names)) # Use of 'len' function
search=raw_input('Enter the name you would like to Search: ')
# Use of 'in'& 'not in' keyword
if search in names:
print 'Search Successful'
elif search not in names:
print 'Search unsuccessful'
else:
Labels: Python Programming
# Programming illustrating how we can use 'list' with iterations using for loop
num1=int(raw_input("Enter first number"));
num2=int(raw_input("Enter second number"));
num3=int(raw_input("Enter third number"));
num4=int(raw_input("Enter fourth number"));
num5=int(raw_input("Enter fifth number"));
num6=int(raw_input("Enter sixth number"));
number=[num1, num2, num3, num4, num5, num6]; #this list(number) holds values provided by users and form an input to for loop.
# these empty lists will hold even and odd number provided by user in their lists
even_num=[]
odd_num=[]
print ("The provided numbers are:",number);
for value in number:
if value%2==0:
even_num.append(value);
elif value%2!=0:
odd_num.append(value);
else:
print("Value is neither even nor odd");
print('\n List of Even numbers are:',even_num);
print('\n List of odd number are:',odd_num);
Labels: Python Programming
Subscribe to:
Posts (Atom)