1. Home
  2. Computing & Technology
  3. Python

"Merry Christmas!": A Quick Tour of Python

From Al Lukaszewski, for About.com

5 of 7

Define Your holiday()

def holiday(type, lang):
hols = {"Christian": "Christmas, Noël, Weichnachtszeit",
"Jewish": "Channukah, Hannukah, Chanukkah",
"Muslim": "Ramadan, Ramadan, Ramadan"}

sals = {"Christian": "Happy, Joyeux, Fröhliche",
"Jewish": "Happy, Joyeux, Happy",
"Muslim": "Happy, Joyeux, Schön"}

hol_choice = string.split(hols[type], ",")
sal_choice = string.split(sals[type], ",")

holiday = string.strip(hol_choice[lang])
salutation = string.strip(sal_choice[lang])
return(holiday, salutation)
The second function takes two arguments: the type of holiday and the language in which the output is to be phrased. In the course of the function, two associative arrays are defined (associative arrays are called dictionaries in Python). Depending on the key supplied by type, a trio of equivalents is split via string.split and assigned in turn to hol_choice and sal_choice. Which of the trio is returned is determined by the value of lang. This value is stripped of any beginning and end spaces by string.strip and assigned as needed to holiday or salutation. These two string variables then form the tuple that is returned by the function.

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.