Home arrow Python arrow Page 5 - Python Conditionals, Lists, Dictionaries, and Operators

Dictionaries - Python

In our last article we learned how to get input from the user, store data in a variable, and work with some basic operators to manipulate that data. In this article we will learn to use Conditional Statements and possibly create functions. So wipe that mustard off your chin, clean the dishes, and let's get to work.

TABLE OF CONTENTS:
  1. Python Conditionals, Lists, Dictionaries, and Operators
  2. Elif
  3. Lists
  4. Tuples
  5. Dictionaries
  6. Operators
By: James Payne
Rating: starstarstarstarstar / 5
November 26, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Another way to store multiple values is with a dictionary. The difference between a List and a dictionary is that dictionaries are unordered and do not use indexes. The best way to understand them is to see them at work:


#!/usr/local/bin/python


mydiction = {'FirstName':'John', 'LastName':'Cleese'}

print mydiction[FirstName]

The above code will result in the printout:

  John

This is because the value to the left of the colon (:) acts as the index, while the value to the right of the colon acts as the value being referenced.

You can also add two dictionaries together:


#!/usr/local/bin/python


mydiction = {'FirstName':'John', 'LastName':'Cleese'}

abc = {'FirstName': 'Monty', 'LastName':'Python')

mydiction.update(abc)


print mydiction

This will combine the two dictionaries and result in the printout:

  John Cleese Monty Python



 
 
>>> More Python Articles          >>> More By James Payne
 

blog comments powered by Disqus
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: