Professional Documents
Culture Documents
Lukman Seminar
Lukman Seminar
Lukman Seminar
BY
Syed Lukman S N
201371601075
BSC.COMPUTER SCIENCE
SYLLABUS
CONTENTS
Functions Function calling
Function parameters
Types of function in python
• Easy to Write
• Easy to Understand
• Object-Oriented
• Robust Standard Libraries
• Supports Various Programming Paradigms
• Support for Interactive Mode
• Dynamically Typed and Type Checking
FEATURES OF FUNCTION
• Using functions, we can avoid rewriting the same logic/code again and again in a
program
• We can call Python functions multiple times in a program and anywhere in a program
• We can track a large Python program easily when it is divided into multiple functions
• Reusability is the main achievement of Python functions
• However, Function calling is always overhead in a Python program
SYNTAX FOR CREATING FUNCTION
• In Python, after the function is created, we can call it from another function.
A function must be defined before the function call; otherwise, the Python
interpreter gives an error. To call the function, use the function name followed
by the parentheses
• Consider the following example of a simple example that prints the message
"Hello World"
EXAMPLE
• Output
hello world
THE RETURN STATEMENT
• The return statement is used at the end of the function and returns the result of the
function.
• It terminates the function execution and transfers the result where the function is called.
• The return statement cannot be used outside of the function
Syntax :
return [expression_list]
It can contain the expression which gets evaluated and value is returned to the caller
function.
If the return statement has no expression or does not exist itself in the function then it
returns the None object.
EXAMPLE
• def sum():
a = 10
b = 20
c=a+b
return c
print("The sum is :",sum ())
• Output:
The sum is: 30
In the above code, we have defined the function named sum, and it has a statement c = a+b,
which computes the given values, and the result is returned by the return statement to the caller
function.
PROGRAM TO SOLVE
• def sum():
a = 10
b = 20
c = a+b
print(sum())
• Output:
None
In the above code, we have defined the same function without the return
statement as we can see that the sum() function returned the None object to the
caller function.
FUNCTION TYPES
• User-define functions –
The user-defined functions are those define by the user to
perform the specific task
• Built-in functions –
The built-in functions are those functions that are pre-defined in
Python.
USER DEFINED FUNCTION
EXAMPLE 1
• def hello():
name = str(input("Enter your name: "))
if name:
print ("Hello " + str(name))
else:
print("Hello World")
return
hello()
• Output 1:
Enter your name : Syed Lukman
Hello Syed Lukman
EXAMPLE 1
• Output 2:
Enter your name :
Hello World
EXPLANATION:
In the above function, you ask the user to give a name.
If no name is given, the function will print out “Hello World”.
Otherwise, the user will get a personalized “Hello” response.
PROGRAM TO SOLVE
• def add _ numbers(x , y):
sum = x + y
return Sum
num1 = 15
num2 = 5
print("The sum is", add _ numbers (num1, num2))
• Output:
The sum is 20
PHYTHON UNICODE CODES
PYTHON BUILT-IN FUNCTIONS
• The print() function prints the specified message to the screen or another standard output device.
• Output:
Syed Lukman
TYPE() FUNCTION
• The type() function returns the type of the specified object.
• Output:
<class 'tuple'>
PROGRAM TO SOLVE
• Example 2: Return the type of the following objects:
variable_1 = “Syed Lukman”
variable_2 = 105
print(type(variable_1))
print(type(variable_2))
• Output:
<class 'str’>
<class 'int'>
INPUT() FUNCTION
• The input() function allows taking the input from the user.
Example: to write the complete message after giving the input
a = input('Enter your name :’)
print('Hello, ' + a + ' Welcome to Crescent University’)
• Output:
Enter your name : Syed Lukman
Hello, Syed Lukman Welcome to Crescent University
ABS() FUNCTION
• The abs() function returns the absolute value of the specified number.
• Output:
676
PROGRAM TO SOLVE
• Positive_number = 4/3
print(abs(positive_number))
• Output:
1.3
POW() FUNCTION
• The pow() function returns the calculated value of x to the power of y i.e, xy.
• If a third parameter is present in this function, then it returns x to the power of y, modulus z.
• Example 1: Return the value of 3 to the power of 4:
x = pow(3, 4)
print(x)
• Output:
81
• Output:
1
SORTED() FUNCTION
• The sorted() function returns a sorted list of the specified iterable object.
• You can specify the order to be either ascending or Descending. In this function, Strings are
sorted alphabetically, and numbers are sorted numerically.
• Output:
['4', '6', 'a', 'c', 'd', 'f', 'g', 'h']
MAX() FUNCTION
• The max() function returns the item with the maximum value.
• If the values this function takes are strings, then it is done using an alphabetical comparison.
• Output:
2785
• Example 2: Return the name with the highest value, ordered alphabetically
names_tuple = ('Dhoni’, ‘Gayle', 'Devilliers', 'Starc','Archer’)
print(max(names_tuple))
• Output:
Starc
MIN() FUNCTION
• Returns the smallest item of a list or of the typed-in arguments. It can even be a string.
• Example 1:
a=(5,7,9)
Print(min(a))
• Output:
5
• Example 2:
a=('s’, 'v’, 'z’)
Print(min(a))
• Output:
s
ROUND() FUNCTION
• The round() function returns a floating-point number that is a rounded version of the specified number, with
the specified number of decimals.
• Output:
88
• Output:
-88
PROGRAM TO SOLVE
• Example 2:
a = (1, 2, 3, 4, 5)
print(sum(a, 7))
• Output:
22
DIVMOD() FUNCTION
• The divmod() function returns a tuple containing the quotient and the
remainder when the first argument i.e, the dividend is divided by the second
argument i.e, the divisor.
• Example 1:
x = chr(115)
print(x)
• Output:
s
• Example 2:
x = chr(88)
print(x)
• Output:
X
PROGRAM TO SOLVE
• Y=chr(64)
print(Y)
• OUTPUT:
@
ISINSTANCE() FUNCTION
• The isinstance() function returns True if the specified object is of the specified type, others
False.
• EXAMPLE:
number= 23
print (isinstance(number, int))
print (isinstance(number, str))
print (isinstance(number, float))
• OUTPUT:
True
False
False
PROGRAM TO SOLVE
• X = “Syed Lukman”
Y=5
A=(‘apple’, ‘mango’)
B=[‘banana’, ‘cherry’]
Z=10.0
C={“name”: “Lukman”, “age”:19}
print (isinstance(Y, int))
print (isinstance(A, tuple))
print (isinstance(X, str))
print (isinstance(C, dict))
print (isinstance(Z, float))
print (isinstance(B, list))
OUTPUT
True
True
True
True
True
True
ANONYMOUS FUNCTION
• Output:
Using lambda function:144
Using regular function:144
PROGRAM TO SOLVE
• OUTPUT:
Enter value of x: 50
Enter value of y :3
x power y is: 125000
BUILT-IN VS USED DEFINED FUNCTION
• Built-in functions are those that are already defined in Python libraries and
we can call them directly. User defined functions are those that we define
ourselves in our program and then call them wherever we want.
SCOPE OF VARIABLE
Scope of a variable is the portion of a program where the variable is recognized.
• Global Scope -variables defined outside the function and part of main
program
• Output:
value inside the function: 10
value outside the function: 20
EXAM PL E
• Output:
Inside the function local total : 30
Outside the function global total : 50
PROGRAM TO SOLVE
• add=10
def sum(a,b):
tot=a+b
print("The local value of total is:",tot)
sum(12,3)
print("The global value of total is:", add)
OUTPUT:
The local value of total is:15
The global value of total is:10
HOW TO CALL A FUNCTION
•There are two ways to call a function either by PASSING A VALUE or PASSING REFERENCE .
•PASSING BY VALUE
•Example:
# passing by value
a=20
def func(b):
print("The value of b is:",b)
b=50
print("The new value of b is:",b)
# calling a function passing by value
func (a)
•Output:
The value of b is: 20
The new value of b is: 50
PASSING BY REFERENCE
• Example:
# passing by reference
a=[10,20,30]
def sum (b):
print("The value of b is:",b)
b[0]=40
b[1]=50
print("The new value of b is:",b)
# calling a function passing by refernce
sum (a)
• Output:
The value of b is: [10, 20, 30]
The new value of b is: [40, 50, 30]
FUNCTION PARAMETERS
• Output:
Enter first number: 10
Enter second number: 20
Sum= 30.0
• In the above example, x and y are formal parameters. a and b are actual parameters.
EXAMPLE
• def evenodd(x):
if x%2==0:
print("It is an even number")
else:
print("It is a odd number")
n=int (input("Enter any number:"))
evenodd(n)
• OUTPUT:
Enter any number:5
It is a odd number
• OUTPUT:
Enter any number:8
It is an even number
PROGRAM TO SOLVE
• OUTPUT:
The sum is: 25
ARGUMENTS IN FUNCTION
• Output:
Hi Lukman
PROGRAM TO SOLVE
• OUTPUT:
Lukman from Crescent University
TYPES OF ARGUMENTS
• There may be several types of arguments which can be passed at the time of
function call.
1. Required arguments
2. Keyword arguments
3. Default arguments
4. Variable-length arguments
REQUIRED ARGUMENTS
• Keyword arguments are related to the function calls. When you use keyword
arguments in a function call, the caller identifies the arguments by the
parameter name. This allows you to skip arguments or place them out of
order because the Python interpreter is able to use the keywords provided to
match the values with parameters. You can also make keyword calls to the
printme() function
EXAMPLE
• Output:
My name is Surya and age is 10
PROGRAM TO SOLVE
• Output:
Sum is 18
Sum is 26
Sum is 35
IMPORT FUNCTION
i) PYTHON IMPORT STATEMENT ENABLES THE USER TO IMPORT PARTICULAR MODULES IN THE CORRESPONDING PROGRAM.
➤ IMPORT TIME
➤ THE TIME PACKAGE CONTAINS A NUMBER OF FUNCTIONS THAT RELATE TO TIME. WE WILL
CONSIDER SOME FUNCTIONS LIKE: SLEEP AND TIME
PYTHON TIME.SLEEP()
➤ THE SLEEP FUNCTION SUSPENDS (DELAYS) EXECUTION OF THE CURRENT
THREAD FOR THE GIVEN NUMBER OF SECONDS.
import time
print(“computer")
time.sleep(2.4)
print(“science")
OUTPUT:
computer
science
• import time
print(“Logged in.")
time.sleep(3)
print(“Logged out.")
• OUTPUT:
Logged in.
Logged out.
( after 3 seconds)
RANDOM FUNCTION
• RANDOM.RANDINT()
• RANDOM.CHOICE()
• RANDOM.SHUFFLE()
RANDOM.RANDINT()
➤ THIS FUNCTION USED TO GET A RANDOM NUMBER BETWEEN
THE TWO GIVEN NUMBERS
➤ THIS FUNCTION TAKES TWO VALUES WHICH ONE IS A
LOWERLIMIT AND ANOTHER ONE IS THE UPPER LIMIT
• EXAMPLE:
IMPORT RANDOM
PRINT(‘A RANDOM INTEGER IS:’)
PRINT(RANDOM.RANDINT(1,10))
• OUTPUT:
RUN 1: A RANDOM INTEGER IS: 4
RUN 2: A RANDOM INTEGER IS: 6
RUN 3: A RANDOM INTEGER IS: 8
RANDOM.CHOICE()
EXAMPLE:
IMPORT RANDOM
A=[1,2,5, ‘ LUKMAN’, ‘KUMAR’, 3.5, ‘INCLUDE’, ‘HELP’]
PRINT(A RANDOM VALUE FROM THE GIVEN LIST:)
PRINT(RANDOM.CHOICE(A))
OUTPUT
• EXAMPLE:
import random
A=[“apple”, “banana”, “cherry”]
random.shuffle(A)
print(‘NEW LIST AFTER SHUFFLING:’,A)
• OUTPUT:
NEW LIST AFTER SHUFFLING : [‘cherry’, ‘apple’, ‘banana’]
TYPES OF FUNCTIONS IN PYTHON
def Adding():
a = 20
b = 30
C=a+b
print(“THE SUM IS :", Sum)
Adding()
OUTPUT:
THE SUM IS : 50
2.Function with no argument and with a Return value
def Multiply():
a = 10
b = 25
C=a*b
Return C
print(“THE MULTIPLICATION IS : ", Multiply())
OUTPUT:
THE MULTIPLICATION IS : 250
3.Python Function with argument and No
Return value
OUTPUT:
THE SUBTRACTION IS : 20
4.Function with argument and return value
OUTPUT:
THE DIVISION IS : 9.0
THANK YOU