Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

Python | Data types, Data Structures, Tuples, Dictionaries, Container

This python programming tutorial covers Data types, Data Structures, Tuples,Dictionaries, Container etc.

Refer following pages to learn complete Python language tutorial.
Python tutorial page1
Python tutorial page2
Python tutorial page3
Python tutorial page4
Python tutorial page5
Python tutorial page6

Working with Data

•  Most programs work with data
•  In this section, we look at how Python programmers represent and work with data
•  Common programming idioms
Primitive Datatypes
Python has a few primitive types of data Integers
Floating point numbers
Strings (text)
Obviously, all programs use these
None type
Nothing, nil, null
logfile = None
This is often used as a placeholder for optional program settings or values
if logfile:
logfile.write("Some message")
If you don't assign logfile to something, the above code would crash (undefined variable) Strings (text)

Data Structures

Real programs have more complex data
Example: A holding of stock
100 shares of GOOG at $490.10
An "object" with three parts
Name ("GOOG", a string)
Number of shares (100, an integer)
Price (490.10, a float)


A collection of values grouped together,Tuples are usually used to represent simple records and data structures.
s = ('GOOG',100,490.10)

Tuple contents are ordered (like an array) s = ('GOOG', 100, 490.10)

Tuples are really focused on packing and unpacking data into variables , not storing distinct items in a list Packing multiple values into a tuple s = ('GOOG', 100, 490.10)

To use the tuple elsewhere, you typically unpack its parts into variables
Unpacking values from a tuple
(name, shares, price) = s


A hash table or associative array
A collection of values indexed by "keys"
The keys are like field names
s = {
'name' : 'GOOG',
'shares' : 100,
'price' : 490.10

Getting values: Just use the key names
>>> print s['name'],s['shares']
GOOG 100
Adding/modifying values : Assign to key names
>>> s['shares'] = 75
Deleting a value
>>> del s['date']

When to use a dictionary as a data structure.
Data has many different parts.
The parts will be modified/manipulated.
For example: If you were reading data from a database and each row had 50 fields,
a dictionary could store the contents of each row using descriptive field names.


Programs often have to work many objects. A portfolio of stocks,Spreadsheets and matrices.
Three choices are:
1. Lists (ordered data)
2. Dictionaries (unordered data)
3. Sets (unordered collection)

Lists as a Container

Use a list when the order of data matters.Lists can hold any kind of object.
For example: A list of tuples
portfolio = [
portfolio[0] ('GOOG',100,490.10)

Dicts as a Container

Dictionaries are useful if you want fast random lookups (by key name)
Example: A dictionary of stock prices

prices = {
'GOOG' : 513.25,
'IBM' : 87.22,
>>> prices['IBM']

Dicts : Looking For Items

To test for existence of a key
if key in d:
# Yes
# No
Looking up a value that might not exist
name = d.get(key,default)
>>> prices.get('IBM',0.0)

Dicts and Lists

dict() promotes a list of pairs to a dictionary
prices = dict(pricelist)


a = set([2,3,4])
Holds collection of unordered items
No duplicates, support common set ops
>>> a = set([2,3,4])


Share this page

Translate this page