python means

Python Dictionary

We also have a Python dictionary that you may use since once you master it, a whole universe of ultra-cool will be at your disposal. The dictionary is the greatest storage device ever. In Python, they are referred to as “dicts.” In other languages, they are known as “hashes.” It makes no difference whatever name I prefer to use. What counts is how they perform when measured against lists. See, a list enables you to perform the following:

>>> things = ['a', 'b', 'c', 'd']
>>> print things[1]
b
>>> things[1] = 'z'
>>> print things[1]
z
>>> print things
['a', 'z', 'c', 'd']
>>>

Numbers can be used to “index” into a list, or to determine what is on a given list. By now, you ought to be aware of the fact that you may only remove items from a list using their respective numbers. You can utilize anything, not just numbers, thanks to dictionaries. Yes, a dictionary will always relate one item to another. Look at this:

>>> stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2}
>>> print stuff['name']
Zed
>>> print stuff['age']
36
>>> print stuff['height']
74
>>> stuff['city'] = "San Francisco"
>>> print stuff['city']
San Francisco
>>>

Python dictionary

You’ll see that we’re utilizing strings rather than just integers to specify what we want from the things dictionary. With strings, we can add new terms to the dictionary. However, it doesn’t have to be strings. Additionally, we can:

>>> stuff[1] = "Wow"
>>> stuff[2] = "Neato"
>>> print stuff[1]
Wow
>>> print stuff[2]
Neato
>>> print stuff
{'city': 'San Francisco', 2: 'Neato',
'name': 'Zed', 1: 'Wow', 'age': 36,
'height': 74}
>>>

In this code I used numbers, and then you can see there are numbers and strings as keys in the python dictionary when I print it. I could use anything—well, almost, but just pretend you can use anything for now.


Of course, a dictionary that you can only put things, so here’s how you delete things, with the del keyword:

>>> del stuff['city']
>>> del stuff[1]
>>> del stuff[2]
>>> stuff
{'name': 'Zed', 'age': 36, 'height': 74}
>>>

We’ll now perform a task that requires thorough consideration. Please enter this exercise and make an effort to comprehend what is happening. Note the procedures I use here, when I put things in a dict, when I get from them, etc.

# create a mapping of state to abbreviation
states = [
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
 ]

# create a basic set of states and some cities in them
cities = [
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
]

# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
print out some cities
print '- ' * 10
print "NY State has: ", cities['NY']
print "OR State has: ", cities['OR']
# print some states
print '- ' * 10
print "Michigan's abbreviation is: ", states['Michigan']
print "Florida has: ", cities[states['Florida']]

# do it by using the state then cities dict
print '- ' * 10
print "Michigan has: ", cities[states['Michigan']]
print "Florida has: ", cities[states['Florida']]

# print every state abbreviation
print '- ' * 10
for state, abbrev in states.items():
print "%s is abbreviated %s" % (state, abbrev)

# print every city in state
print '- ' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev, city)

# now do both at the same time
print '- ' * 10
for state, abbrev in states.items():
print "%s state is abbreviated %s and has city %s" % (
state, abbrev, cities[abbrev])

print '- ' * 10
# safely get an abbreviation by state that might not be there
state = states.get('Texas', None)

if not state:
print "Sorry, no Texas."

# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city

I will be adding more posts in Python Topic, so please bookmark the post for future reference too.

Online Training Tutorials

  • Python Tutorial – Quick Guide: Learn Python Programming OnlinePython Tutorial – Quick Guide: Learn Python Programming OnlineThis document includes a basic Python tutorial and information on learning Python, a programming language that first appeared in the late 1980s. In contrast to other widely used languages […]
  • Python loops and listsPython loops and listsWe should now be able to create some programs that are significantly more interesting thanks to Python Loops. If you've been following along, you should be aware that you can now use […]
  • Release Procedure for requisitions in PurchasingRelease Procedure for requisitions in Purchasing in MMReleasing Requisitions,This post describes the release procedure for requisitions in Purchasing. (Note that in this context, "release" means giving approval, or clearance, to go ahead with […]
  • What is SAP HANA Live?What is SAP HANA Live?SAP HANA Live (formerly SAP HANA Analytics Foundation or SHAF or SAP HANA ANALY) is solution for operational real-time reporting on HANA. HANA Live tries to bridge the gap of having […]
  • Clearing in SAPWhat is Clearing in SAP?Clearing in SAP refers to squaring-off open debit entries with that of open credit entries. Clearing is allowed in GL accounts maintained on an ‘open item’ basis and in all customer/vendor […]
  • Python Strings,Text and PrintingPython Strings,Text and PrintingDespite having experience developing Python strings, you are still unsure of their function. To help you understand what these variables are for, we've created a ton of them with intricate […]
  • Model View Controller (MVC)Model View Controller Design Pattern (MVC)) in SAP CRMBusiness Server Pages (BSPs) can be created using different programming paradigms and design patterns. The CRM WebClient UI BSP is based on the Model View Controller (MVC) paradigm. MVC is […]
  • Solution Manager keyHow to Generate Solution Manager Key in SAP System?Steps to Generate Solution Manager Key in SAP System, To Generate Solution Manager Key Execute T-code SMSY in Solution Manager System, You need to do the following steps: 1), To […]