You are on page 1of 7

PYTHON PROGRAMMING

Lecture# 8

Lecture Content
1. Introduction to Functions
2. What is Function?
3. Types of Functions in Python
4. How to Create UDFs?
5. Calling a Function
6. Parameters VS Arguments
7. Global VS Local Variables
8. Types of Arguments
9. Word Guessing Game
Introduction to Functions:
What is Function?
A function is a block of code that performs some specific task and only runs when
it is called.
It can be called and reused multiple times. You can pass information/data and it
can send information back as a result.
Types of Functions in Python:
There are three types of functions in Python:
 Built-in functions: These are the functions that are predefined in Python
and we can call them such as help() to ask for help, min() to get the
minimum value, print() to print an object to the terminal.
 User-Defined Functions (UDFs): These are the functions created by the
user according to their programming needs.

How To Define/Create a User-Defined Function (UDF) in Python?


The four steps in defining a function in Python are the following:
 Use the keyword def to declare the function and follow this up with the
function name.
 Add parameters to the function. These should be written within the
parentheses of the function. End your line with a colon.
 Add statements that the function should execute.
 End your function with a return statement if the function should output
something.

Calling a Function:
To call a function, use the function name followed by parenthesis:
Example:
def my_function():
print("Hello from a function")

my_function()

Parameters vs Arguments
 The argument is the value that is supplied/sent to a function when it is
called.
 The parameter is the variable inside the parenthesis in the definition of the
function.

Global vs Local Variables:


 In general, variables that are defined inside a function have a local scope and
are known as local variables. These can only be accessed inside that
function.
 The variables that are defined outside the functions have a global scope and
are known as global variables. These can be accessed by all the functions
that might be there in your script.
Types of arguments:
There are four types of arguments that Python UDFs can take:
 Default arguments
 Required arguments
 Keyword arguments
 Variable number of arguments
Default Arguments: Those that take a default value if no argument value is passed
during the function call. Example:
# Define `plus()` function
def plus(a,b = 2):
return a + b
# Call `plus()` with only `a` parameter
plus(a=1)
Required arguments: As the name suggests, the required arguments of a UDF are
those that have to be passed during the function call and in the right order.
Example:
# Define `plus()` with required arguments
def plus(a,b):
return a + b
Keyword arguments: If you want to make sure that you call all the arguments in
the right order, you can choose the keyword arguments in your function call.
Example:
# Define `plus()` function
def plus(a,b):
return a + b
# Call `plus()` function with keyword arguments
plus(a=1, b=2)
A variable number of arguments: If you do not know how many arguments will
be passed into your function, add a * before the parameter name in the function
definition. Example:
# Define `plus()` function to accept a variable number of arguments
def plus(*args):
return sum(args)
# Calculate the sum
plus(1,4,5)
plus(5,6,7,8,4)

Word Guessing Game:


Code:
import random

words = ["orange","purple","python","paper","window","apple","mother","father",
"ock","scissors","room","remote","desktop","draw","design","home","horse",
"cat","cake","character","vowel"]

def shuffled_word(word):
list_of_word = list(word)
random.shuffle(list_of_word)
shuffled_word = "".join(list_of_word)
return shuffled_word

def main():
score=0
while True:
user_input = input("Do you want to play? press y/n for yes/no : ")
user_input= user_input.lower() #to make user input in lower case
if user_input == "y":
word = random.choice(words)
user_guess = input("Guess this shuffled word --- {} :
".format(shuffled_word(word)))
if (user_guess == word):
print("Your Guess is Correct")
score+=1
else:
print("Your Guess is wrong")
else:
break
print("****************************************")
print("You get", score, "scores")
print("GoodBye!!!")
main()

Output:

Exercise:
Write a Python function celsius_to_fahrenheit that takes a temperature in Celsius
as input and returns the corresponding temperature in Fahrenheit. The conversion
formula is:
Fahrenheit = (Celsius * 9/5) + 32

Sample Input:
celsius = 25

Sample Output:
77.0

You might also like