1. Basic

(1) function

  • raw_input()
  • Python v2.7.6 documentation: http://docs.python.org/2.7/

  • import this: you will get “The Zen of Python”
  • import math
    print math.sqrt(25)     or just:   from math import sqrt  or:  from math import *
  • import math # Imports the math module
    everything = dir(math) # Sets everything to a list of things from math
    print everything
  • built-in functions: .upper(), .lower(), str(), len(), min(), max(), abs(), type()
  • def function():   more than 1 parameter and you don’t know how many, use   def function(*args):

(2) list and dictionary

  • replace items in a list:  list[0]=’new_item’
  • add items to list:   list.append(item)
  • find the index of a string: list.index(“dog”)  insert an item into a list: list.insert(4, “cat”)
  • for loop and sort:for x in start_list:
  • dictionary key:  dictionary[‘key’]  assign/change value: dictionary[‘key’]=20/[‘dog’,’cat’]
  • delete key: del dictionary[‘key’]

2. Example

(1) PygLatin example

1)Move the first several consonants to the back + ay

word=raw_input(“What is your English word?”)

def isvowel(letter): return letter.lower() in “aeiou”

def pygLatin(word):
if isvowel(word[0]): # if word starts with a vowel
return word + “hay” # then keep it as it is and add hay to the end
first_vowel_position = get_first_vowel_position(word)
return word[first_vowel_position:] + word[:first_vowel_position] + “ay”

def get_first_vowel_position(word):
for position, letter in enumerate(word):
if isvowel(letter):
return position
return -1

print “The PygLatin translation is: ” + pygLatin(word)

2) Move just the first consonant to the back + ay
word=raw_input(“What is your English word?”)
VOWELS = (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
def pygLatin(word):
first_letter = word[0]
if first_letter in VOWELS:
return word + “hay”
return word[1:] + word[0] + “ay”
print “The PygLatin translation is: ” + pygLatin(word)


#check empty and character first

pyg = ‘ay’

original = raw_input(‘Enter a word:’)

if len(word) > 0 and word.isalpha():
if first in ‘aeiou’:
new_word = word + pyg
print new_word
new_word = word[1:] + first + pyg
print new_word
print ’empty’