String Manipulation
String Manipulation
String Manipulation
that we have replaced it with ‘continue’. As you can see in the output except the letter ‘e’ all the
other letters get printed.
pass statement can be used in ‘if ’ clause as well as within loop construct, when you do
not want any statements or commands within that block to be executed.
Syntax:
pass
Output:
Enter any number :3
non zero value is accepted
When the above code is executed if the input value is 0 (zero)
then no action will be performed, for all the other input values the
output will be as follows:
Note
pass statement is generally used as a placeholder. When we have a loop or function
that is to be implemented in the future and not now, we cannot develop such functions
or loops with empty body segment because the interpreter would raise an error. So, to
avoid this we can use pass statement to construct a body that does nothing.
Output:
End of the loop, loop structure will be built in future.
Points to remember:
85 Control Structures
Evaluation
Part - I
Part -III
Part -IV
Table – 7.1 – Python Functions and it's • The code block always comes after a
Description colon (:) and is indented.
When you call the “hello()” function, the program displays the following string as
output:
Output
hello – Python
Alternatively we can call the “hello()” function within the print() function as in the
example given below.
Example: 7.3.2
def hello():
print (“hello - Python”)
return
print (hello())
If the return has no argument, “None” will be displayed as the last statement of the
output.
91 Python Functions
Output:
hello – Python
None
Syntax:
def function_name (parameter(s) separated by comma):
Let us see the use of parameters while defining functions. The parameters that you
place in the parenthesis will be used by the function itself. You can pass all sorts of data to the
functions. Here is an example program that defines a function that helps to pass parameters
into the function.
Example: 7.4
# assume w = 3 and h = 5
def area(w,h):
return w * h
print (area (3,5))
The above code assigns the width and height values to the parameters w and h. These
parameters are used in the creation of the function “area”. When you call the above function,
it returns the product of width and height as output.
The value of 3 and 5 are passed to w and h respectively, the function will return 15 as
output.
We often use the terms parameters and arguments interchangeably. However, there
is a slight difference between them. Parameters are the variables used in the function
definition whereas arguments are the values we pass to the function parameters
Arguments are used to call a function and there are primarily 4 types of functions that
one can use: Required arguments, Keyword arguments, Default arguments and Variable-length
arguments.
1 Required arguments
2 Keyword arguments
3 Default arguments
4 Variable-length arguments
Instead of printstring() in the above code if we use printstring (“Welcome”) then the
output is
Output:
Example - Required arguments
Welcome
93 Python Functions
Output:
Example-1 Keyword arguments
Name :Gshan
Output:
Example-3 Keyword arguments
Name : Gshan
Age : 25
Note
In the above program the parameters orders are changed
Example: 7.5.3
def printinfo( name, salary = 3500):
print (“Name: “, name)
print (“Salary: “, salary)
return
printinfo(“Mani”)
Output:
Name: Mani
Salary: 3500
Output:
Name: Ram
Salary: 2000
In the above code, the value 2000 is passed to the argument salary, the default value
already assigned for salary is simply ignored.
95 Python Functions
Example: 7.5.4
def sum(x,y,z):
print("sum of three nos :",x+y+z)
sum(5,10,15,20,25)
Example: 7.5.4. 1
def printnos (*nos): Output:
for n in nos: Printing two values
print(n) 1
return 2
# now invoking the printnos() function Printing three values
print ('Printing two values') 10
printnos (1,2) 20
print ('Printing three values') 30
printnos (10,20,30)
Evaluate Yourself ?
In the above program change the function name printnos as printnames in all places
wherever it is used and give the appropriate data Ex. printnos (10, 20, 30) as printnames ('mala',
'kala', 'bala') and see output.
Note
Keyword variable arguments are beyond the scope of this book.
Note
filter(), map() and reduce() functions are beyond the scope of this book.
Lambda function can take any number of arguments and must return one
value in the form of an expression. Lambda function can only access global variables
and variables in its parameter list.
97 Python Functions
The above lambda function that adds argument arg1 with argument arg2 and stores the
result in the variable sum. The result is displayed using the print().
• The return statement causes your function to exit and returns a value to its caller. The
point of functions in general is to take inputs and return something.
• The return statement is used when a function is ready to return a value to its caller. So,
only one return statement is executed at run time even though the function contains
multiple return statements.
• Any number of 'return' statements are allowed in a function definition but only one of
them is executed at run time.
7.7.1 Syntax of return
This statement can contain expression which gets evaluated and the value is returned.
If there is no expression in the statement or the return statement itself is not present inside a
function, then the function will return the None object.
99 Python Functions
When we run the above code, the output shows the following error:
The above error occurs because we are trying to access a local variable ‘y’ in a global
scope.
Note
Without using the global keyword we cannot modify the global variable inside
the function but we can only access the global variable.
In the above program, x is defined as a global variable. Inside the add() function, global
keyword is used for x and we increment the variable x by 5. Now We can see the change on the
global variable x outside the function i.e the value of x is 5.
In the above program, we declare x as global and y as local variable in the function
loc().
After calling the function loc(), the value of x becomes 16 because we used x=x * 2.
After that, we print the value of local variable y i.e. local.
Example : 7.8.3 (b) Global variable and Local variable with same name
x=5
def loc():
x = 10
print ("local x:", x)
loc()
print ("global x:", x)
Output:
local x: 10
global x: 5
In above code, we used same name ‘x’ for both global variable and local variable. We get
different result when we print same variable because the variable is declared in both scopes, i.e.
the local scope inside the function loc() and global scope outside the function loc().
The output :- local x: 10, is called local scope of variable.
The output:- global x: 5, is called global scope of variable.
Output:
25
125.0
343
Mathematical Functions
Note
Specify import math module before using all mathematical
functions in a program