Function, String
Function, String
➢ Function is a sub program which consists of set of instructions used to perform a specific task.
A large program is divided into basic building blocks called function.
Types of function:
Functions can be classified into two categories:
i) user defined function
ii) Built in function
i) Built in functions
• Built in functions are the functions that are already created and stored inpython.
• These built in functions are always available for usage and accessed by a programmer. It cannot be
modified.
Built in function Description
Example:
def my_add(a,b):
c=a+b
return c
Flow of Execution:
• The order in which statements are executed is called the flow of execution
• Execution always begins at the first statement of the program.
• Statements are executed one at a time, in order, from top to bottom.
• Function definitions do not alter the flow of execution of the program, but remember that statements
inside the function are not executed until the function is called.
• Function calls are like a bypass in the flow of execution. Instead of going to the next statement, the
flow jumps to the first line of the called function, executes all the statements there, and then comes
back to pick up where it left off.
Note: When you read a program, don‟t read from top to bottom. Instead, follow the flow of execution. This
means that you will read the def statements as you are scanning from top to bottom, but you should skip the
statements of the function definition until you reach a point where that function is called.
Function Prototypes:
OUTPUT: OUTPUT:
enter a5 enter a5
enter b 10 enter b 10
15 15
OUTPUT: OUTPUT:
enter a5 enter a5
enter b 10 enter b 10
15 15
Fruitful Function
• Fruitful function
• Void function
• Return values
• Parameters
• Local and global scope
• Function composition
• Recursion
Fruitful function:
Example:
Root=sqrt(25)
Example:
def add():
a=10
b=20
c=a+b
return c
c=add()
print(c)
Void Function
Example:
print(“Hello”)
Example:
def add():
a=10
b=20
c=a+b
print(c)
add()
Return values:
return keywords are used to return the values from the function.
example:
return a – return 1 variable
return a,b– return 2 variables
return a,b,c– return 3 variables
return a+b– return expression
return 8– return value
PARAMETERS / ARGUMENTS:
❖ Parameters are the variables which used in the function definition. Parameters are
inputs to functions. Parameter receives the input from the function call.
❖ It is possible to define more than one parameter in the function definition.
Types of parameters/Arguments:
1. Required/Positional parameters
2. Keyword parameters
3. Default parameters
4. Variable length parameters
Output:
George 98
Keyword parameter:
When we call a function with some values, these values get assigned to the parameter according to
their position. When we call functions in keyword parameter, the order of the arguments can be
changed.
Example
def student(name,roll,mark):
print(name,roll,mark)
student(90,102,"bala")
Output:
90 102 bala
Default parameter:
Python allows function parameter to have default values; if the function is called without the
argument, the argument gets its default value in function definition.
Example
def student( name, age=17):
print (name, age)
student( “kumar”):
student( “ajay”):
Output:
Kumar 17
Ajay 17
❖ Sometimes, we do not know in advance the number of arguments that will be passed
into a function.
❖ Python allows us to handle this kind of situation through function calls with number of
arguments.
❖ In the function definition we use an asterisk (*) before the parameter name to denote
this is variable length of parameter.
Example
def student( name,*mark):
print(name,mark)
student (“bala”,102,90)
Output:
bala ( 102 ,90)
Local and Global Scope
Global Scope
❖ The scope of a variable refers to the places that you can see or access a variable.
❖ A variable with global scope can be used anywhere in the program.
❖ It can be created by defining a variable outside the function.
Local Scope A variable with local scope can be used only within the function .
Function Composition:
❖ Function Composition is the ability to call one function from within another function
❖ It is a way of combining functions such that the result of each function is passed as the
argument of the next function.
❖ In other words the output of one function is given as the input of another function is
known as function composition.
Example:
math.sqrt(math.log(10))
def add(a,b):
c=a+b
return c
def mul(c,d):
e=c*d
return e
c=add(10,20)
e=mul(c,30)
print(e)
Output:
900
output
enter a:4
enter b:8
the avg is 6.0
Recursion
A function calling itself till it reaches the base value - stop point of function call. Example:
factorial of a given number using recursion
Factorial of n
def fact(n):
if(n==1):
return 1
else:
return n*fact(n-1)
n=eval(input("enter no. to find
fact:"))
fact=fact(n)
print("Fact is",fact)
Output
enter no. to find fact:5
Fact is 120
Explanation
Strings:
❖
Strings
❖
String slices
❖
Immutability
❖
String functions and methods
❖
String module
Strings:
❖
String is defined as sequence of characters represented in quotation marks(either single
quotes ( ‘ ) or double quotes ( “ ).
❖
An individual character in a string is accessed using a index.
❖
The index should always be an integer (positive or negative).
❖
A index starts from 0 to n-1.
❖
Strings are immutable i.e. the contents of the string cannot be changed after it is created.
❖
Python will get the input at run time by default as a string.
❖
Python does not support character data type. A string of size 1 can be treated as characters.
Operations on string:
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
5. Member ship
String slices:
A part of a string is called string slices.
The process of extracting a sub string from a string is called slicing.
Immutability:
Python strings are “immutable” as they cannot be changed after they are created.
Therefore [ ] operator cannot be used on the left side of an assignment.
string built in functions and methods:
A method is a function that “belongs to” an object.
Syntax:
import module_name
Example
import string
print(string.punctuation)
print(string.digits)
print(string.printable)
print(string.capwords("happy birthday"))
print(string.hexdigits)
print(string.octdigits)
output
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
0123456789
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJ
KLMNOPQRSTUVWXYZ!"#$%&'()*+,-
./:;<=>?@[\]^_`{|}~
Happy Birthday
0123456789abcdefABCDEF
01234567