My Python Notes
Created On: July 29, 2004
Last Updated: August 14, 2005
Table of Contents
- Introduction
- List Data Structures
- Dictionary Data Structures
- Sets Data Structures
- Working with Files
- Misc Stuff
- Full Examples
Here are my notes for programming in Python. Lately, I've found using Python as a more rewarding and friendly language then Perl, which is where I have a lot of experience. Python is nice especially for co-development and maintaining code down-the-line. Python code tends to be a little less cryptic since there is usually only one way of doing things; which is definitely not the Perl way. To be honest though, I can still do anything in Perl faster.
If you are really into these holy wars you can read about the great Python -vs- Perl debate here, here and even here. But as some one who just wants to do some work and stop wasting time. Who cares, pick a language and Just Code Baby™.
Names: Lists, Arrays, Tuples, (Strings). A tuple is a list that can't be changed; so append and delete do not work on tuples.
Create Empty List:
Create Shopping List:
Print List Item:
>> apples
Append to List:
Delete from List:
Size of List:
Names: Dictionaries, Dicts, Hash, Associative Array
Create Empty Dict:
Create Address Dict:
'firstname': 'Marcus',
'lastname': 'Kazmierczak',
'e-mail': 'marcus@mkaz.com'}
Print Dict Item:
>> Marcus
Delete from Dict:
Size of Dict:
List Dict Keys:
>> ['firstname', 'lastname', 'e-mail']
Python 2.3 introduced sets as another data structure, which have the same mathematical set properties: union, intersection, etc...
Create Sets:
set2 = sets.Set(['blue','red','orange','green'])
Union Sets:
>> Set(['blue', 'green', 'apple', 'orange', 'banana', 'red'])
# shortcut operator
set1 | set2
Intersection Sets:
>> Set(['orange'])
# shortcut operator
set1 & set2
Difference Sets:
>> Set(['apple', 'banana'])
set2.difference(set1)
>> Set(['blue', 'green', 'red'])
Sym Difference:
>> Set(['blue', 'green', 'apple', 'red', 'banana'])
Check if Element in Set:
Read Lines from File
f = open(filename, 'r')
lines = f.readlines();
for line in lines:
print line
f.close()
Check if File/Directory Exists, similar to Perl's -e checks
Use the os module, see the full documentation at
http://python.org/doc/current/lib/module-os.path.html
import os file = "/my/file/path/name" if os.path.exists(file): print "%s exists" % file # here are some other things you can check for if os.path.isabs(file): print "%s is absolute path" % file if os.path.isdir(file): print "%s is directory" % file if os.path.isfile(file): print "%s is file " % file if os.path.islink(file): print "%s is link" % file if os.path.ismount(file): print "%s is a mount point" % file
Grab a list of files in directory, similar to Perl's glob
dir = "/my/dir" files = os.listdir(dir) for file in files: print file # or if you only want *.py for file in os.listdir(dir): if (file.endswith(".py")): print file # or if you want all files the start with 'test' for file in os.listdir(dir): if (file.startswith("test")): print file
Read User Input from STDIN, Prompt User
It's nice to strip out any whitespace and the newline, this can be done in one line:
Read Data in from STDIN
- GetOpt Example - Parsing Command Line Arguments: getopt_ex.py.html
- Process E-mail Message, pipe STDIN, write file: sproc.py.html
![]() |
Learning Python by Mark Lutz; Paperback; Buy New: $23.77 Buy now from Amazon.com |
![]() |
Python Cookbook Alex Martelli; Paperback; Buy New: $27.17 Buy now from Amazon.com |

