Puzzle: Pig Latin Word
- If the word begins with vowels (a,e,i,o,u), add "way" to the endo of the word. So "air" => "airway", "eat" => "eatway"
- If the word is not vowel
python => ythonpay (move first to last and add "ay")
computer => omputercay
- Write a function in python that takes string (word) as input and returns the word in pig latin (pig_latin_word)
- Write a function in python that takes a sentence and treturns the sentence in pig latin (pig_latin_sentence)
Functions in Python
- We can do two things with a function
- Define it, with zero or more parameters
- Call it, get zero or more results
- python function syntax
def <function_name>():
pass
- Very Simple functions
def hello_world_function():
"""
This function is just to demonstrate function syntax
"""
# pass represents code block is not implemented
pass
def say_hello():
"""
This function demonstrates a function with zero parameters
"""
print("hello")
if __name__ == "__main__":
hello_world_function()
say_hello()
- Functions can return values
def agree():
"""
This function represents agreement
:return: True
"""
return True
def disagree():
"""
This function represents disagreement
:return:
"""
return False
if __name__ == "__main__":
if agree():
print("awsome")
- Lets add function with arguments
def echo(message):
"""
This function will print the message
:param message: messsage to be printed
"""
print(message)
def describe_traffic_signal(color):
"""
This functions returns textual meaning of traffic signals
:param color: color of the traffic signal
:return: message to be displayed and None for wrong color passed
usage is
message = describe_traffic_signal('red')
if message:
print(message)
"""
if color == 'red':
return "stop"
elif color == "green":
return "proceed"
elif color == "orange":
return "prepare to stop"
else:
return None
- None is useful in functions
def whatis(thing):
if thing or (thing == False):
print("Its not none")
print(f"Thing has some value: {thing}")
else:
print("None is passed")
if __name__ == "__main__":
whatis('hello')
whatis(1)
whatis(True)
whatis(false)
Positional Arguments and KeyWord Arguments in Python Functions
- See the sample below
def menu(starter, main_course, dessert):
"""
This function prints the menu
:param starter:
:param main_course:
:param dessert:
:return: menu to the printed on screen
"""
return {'starter': starter, 'main course': main_course, 'dessert': dessert}
if __name__ == "__main__":
# positional arguments
today_menu = menu('chilli manchurian', 'biryani', 'cake')
print(today_menu)
# Keyword arguments
tomorrow_menu = menu(dessert='icecream', starter='french fries', main_course='Veg Thali')
print(tomorrow_menu)
Specifying default values
- See the sample below
def menu(starter='chicken 65', main_course='biryani', dessert='cake'):
"""
This function prints the menu
:param starter:
:param main_course:
:param dessert:
:return: menu to the printed on screen
"""
return {'starter': starter, 'main course': main_course, 'dessert': dessert}
if __name__ == "__main__":
# positional arguments
today_menu = menu('chilli manchurian', 'biryani', 'cake')
print(today_menu)
# Keyword arguments
tomorrow_menu = menu(dessert='icecream', starter='french fries', main_course='Veg Thali')
print(tomorrow_menu)
# default
print(menu())
- *args in python:
def multiple_args(args=[]):
"""
Passing a list to the function
:param args:
:return:
"""
for arg in args:
print(arg)
def variable_arguments(*args):
"""
This function supports passing multiple arguments
:param args: arguments
:return:
"""
print(args)
print(f"the type of args is {type(args)}")
def add(*args):
"""
This method will add the arguments
:param args: arguments
:return: sum of arguments
"""
result = 0
for arg in args:
result += arg
return result
def join_string(symbol, *args):
"""
This method will join the string with the args passed
:param symbol: symbol to be used
:param args: arguments
:return:
"""
result = symbol.join(args)
return result
if __name__ == "__main__":
# multiple_args(['red', 'blue', 'green'])
print(join_string(" ", 'hello', 'how are you'))
print(add(1))
print(add(1, 2))
print(add(1, 2, 3, 4, 5, 6, 7, 8, 9))
- Keyword Arguments
def print_kwargs(**kwargs):
print('Keyword arguments: ', kwargs)
print(f'Type of Keyword arguments {type(kwargs)}')
if __name__ == "__main__":
print_kwargs()
print_kwargs(course='python', trainer='khaja', institute='Quality Thought')
-
Argument order in functions
- Required arguments
- Optional positional arguments (*args)
- Optional keyword argument (**kwargs)
-
Code is located at here