Python Functions

From rbachwiki
Jump to navigation Jump to search
def addnum(a,b):
   result = a +b
   return result
print addnum(4,5)

Problem: find out if the word "dog" is in a string

def dog(mystring):
  return 'dog' in mystring.lower():
dog('dog ran away')
true

Pig Latin

def piglatin(word):
 first_letter = word[0]
"check if vowel
if first_letter in 'aeiou':
   pig_word = word + 'ay'
else:
   pig_word = word[1:] + first_letter + 'ay' # the 1: means from index 1 all the way to the end
return pig_word

Back To Top - Category