Programming for Artificial
Intelligence-Functions
Muhammad Ahsan Raees
Introduction
• A Python function is a block of
organized, reusable code that is
used to perform a single, related
action.
• Functions provide better
modularity for application and a
high degree of code reusing.
• A top-to-down approach towards
building the processing logic
involves defining blocks of
independent reusable functions.
Types of Python Functions
• Built-in functions
• Python's standard library includes number of built-in functions. Some of
Python's built-in functions are print(), int(), len(), sum(), etc.
• User-defined functions
• Functions defined in built-in modules
Syntax to Define a Python Function
def function_name( parameters ):
"function_docstring"
function_suite
return [expression]
Example to Define a Python Function
def greetings():
print ("Hello World")
return
Calling a Python Function
def printme( str ):
print (str)
return;
# Now call the function
printme("I'm first call to user defined function!")
printme("Again second call to the same function")
Python Function Arguments
• Function arguments are the
values or variables passed into a
function when it is called. The
behavior of a function often
depends on the arguments
passed to it.
Example
def greetings(name):
print ("Hello ",name)
return
greetings(“Ahsan")
greetings(“Raees")
greetings(“Talha")
Types of Python Function Arguments
• Positional or Required Arguments
• Keyword Arguments
• Default Arguments
Positional or Required Arguments
• Required arguments are the
arguments passed to a function
in correct positional order. Here, def printme( str ):
the number of arguments in the print (str)
function call should match
exactly with the function return;
definition, otherwise the code
gives a syntax error.
printme()
Python Default Arguments
• Python allows to define a def showinfo( name, city =
function with default value “Rawalpindi" ):
assigned to one or more formal print ("Name:", name)
arguments.
print ("City:", city)
• Python uses the default value for
such an argument if no value is return
passed to it. showinfo(name = “Raees", city =
• If any value is passed, the default “Rahimyar Khan")
value is overridden with the showinfo(name = “Raees")
actual value passed.
Example
def percent(phy, maths, maxmarks=200): percentage: 65.0
val = (phy + maths) * 100/maxmarks
return val
percentage: 86.0
phy = 60
maths = 70
# function calling with default argument
result = percent(phy, maths)
print ("percentage:", result)
phy = 40
maths = 46
result = percent(phy, maths, 100)
print ("percentage:", result)
Python - Keyword Arguments
• Python allows to pass function arguments in the form of keywords
which are also called named arguments.
• Variables in the function definition are used as keywords.
• When the function is called, you can explicitly mention the name and
its value.
Example
def printinfo( name, age ):
"This prints a passed info into this function"
Name: Raees
print ("Name: ", name) Age 29
print ("Age ", age)
return
Name: Ahsan
Age 30
# Now you can call printinfo function
# by positional arguments
printinfo (“Raees", 29)
# by keyword arguments
printinfo(name=“Ahsan", age = 30)
Python Variable Scope
• The scope of a variable in Python is defined as the specific area or
region where the variable is accessible to the user.
• The scope of a variable depends on where and how it is defined.
• In Python, a variable can have either a global or a local scope.
Types of Scope for Variables in Python
• Local Variables
• Global Variables
Local Variables
• A local variable is defined within a def myfunction():
specific function or block of code.
• It can only be accessed by the function
a = 10
or block where it was defined, and it b = 20
has a limited scope.
print("variable a:", a)
• In other words, the scope of local
variables is limited to the function they print("variable b:", b)
are defined in and attempting to access return a+b
them outside of this function will result
in an error.
• Always remember, multiple local print (myfunction())
variables can exist with the same name.
Global Variables
• A global variable can be #global variables
accessed from any part of the name = 'TutorialsPoint'
program, and it is defined
outside any function or block of marks = 50
code. def myfunction():
• It is not specific to any block or # accessing inside the function
function. print("name:", name)
print("marks:", marks)
# function call
myfunction()
Python - Modules
• The concept of module in Python further enhances the modularity.
• You can define more than one related functions together and load
required functions.
• A module is a file containing definition of functions, classes, variables,
constants or any other Python object.
• Contents of this file can be made available to any other program.
• Python has the import keyword for this purpose.
Example of Python Module
import math
print ("Square root of 100:", math.sqrt(100))
Creating a Python Module
• Creating a module is nothing but def SayHello(name):
saving a Python code with the print ("Hi {}! How are
help of any editor. you?".format(name))
• Let us save the following code as return
mymodule.py
import mymodule
mymodule.SayHello(“Ahsan
Raees")
Python - Built-in Functions
• built-in functions are those functions that are pre-defined in the
Python interpreter and you don't need to import any module to use
them.
• These functions help to perform a wide variety of operations on
strings, iterators, and numbers.
• For instance, the built-in functions like sum(), min(), and max() are
used to simplify mathematical operations.
Example
• Write a Python function to find def maximumnumber(x,y,z):
the maximum of three numbers. if x>y and x>z:
print("Greater number is :",x)
elif y>x and y>z:
print("Greater number is :", y)
else:
print("Greater number is :", z)
maximumnumber(10,13,12)
Example
• Write a Python function to sum def sum(List):
all the numbers in a list. total=0
Sample List :[8, 2, 3, 0, 7] for i in List:
total +=i
return total
li=[8,2,3,0,7]
print(sum(li))
Quiz
• Write a Python program that prints all the numbers from 0 to 6 except
3 and 6.
Numbers=[0,1,2,3,4,5,6]