Difference between revisions of "Python Functions"

From rbachwiki
Jump to navigation Jump to search
(Created page with " 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.low...")
 
Line 8: Line 8:
  dog('dog ran away')
  dog('dog ran away')
  true
  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

Revision as of 22:02, 2 February 2019

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