3 Functions
3 Functions
html
Defining Functions
Defining functions use the def command.
Functions can have arguments and return values
Invoked by the name of the function
Function Call
In [ ]: happy()
Return Values
To let a function return a value, use the return statement:
In [ ]: def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
15
25
45
1 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
Function calling
In [ ]: x = 3;
w= square(x);
print("3x3 = ", w);
print (" 3 squared twice ", square(square(3)))
print (" In expression", 10+square(3))
3x3 = 9
3 squared twice 81
In expression 19
In [ ]: # Example 2
import numpy as np
def powers_any(x): # x is here argument
y= np.power(x,3)
return y
In [ ]: x = 3;
w= powers_any(x);
print("The power value is : = ", w);
print (" In expression", 10+powers_any(3))
Function calling
In [ ]: # Example 2
def hbd(name):
print ("Happy Birthday to ", name)
return
Function calling
In [ ]: name = "shyam"
hbd(name)
2 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
This way the function will receive a tuple of arguments, and can access the items accordingly:
Example
Keyword Arguments
You can also send arguments with the key = value syntax.
E.g. if you send a List as an argument, it will still be a List when it reaches the function:
In [ ]: def my_function(food):
for x in food:
print(x)
my_function(fruits)
apple
banana
cherry
In [ ]:
3 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
Function Parameters
In [6]: import numpy as np
def addinterest(balance,rate):
balance = balance + np.multiply(balance , rate)
# print(balance)
# print(newbalance)
return balance
[200, 400]
Amounts after adding interest
[210. 420.]
ID of Amount after adding Interest
2309572383408
Mean finding
In [ ]: import numpy as np
def mean(x):
b=np.sum(x)
y=b/len(x)
print(y)
return y
Calling function
In [ ]: a=np.arange(10)
z=mean(a)
4.5
https://docs.python.org/3/tutorial/classes.html (https://docs.python.org/3/tutorial/classes.html)
4 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
What is a class?
Classes are like a blueprint or a prototype that you can define to use to create objects.
A class is a code template for creating objects. Objects have member variables and have behaviour
associated with them. In python a class is created by the keyword class.
Example
Create a class named MyClass, with a property named x:
What is Object
An object is an instance of a class. This is the realized version of the class, where the class is manifested
in the program.
We can take the MyClass class defined above, and use it to create an object or instance of it.
Create Object
Now we can use the class named MyClass to create objects:
Example
Create an object named p1, and print the value of x:
In [2]: p1 = MyClass()
print(p1.x)
print(p1.y)
10
20
5 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
Example
In [5]: # Other Example
class Shark:
def swim(self):
print("The shark is swimming.")
def be_awesome(self):
print("The shark is being awesome.")
def main():
shyam = Shark()
shyam.swim()
shyam.be_awesome()
if __name__ == "__main__":
main()
6 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
Use theinit() function to assign values to object properties, or other operations that are necessary to do
when the object is being created:
The__init__() function is called automatically every time the class is being used to create a new
object.
The simplest class can be created using the class keyword.
Example 1
Create a class named Person, use theinit() function to assign values for name and age:
def be_awesome(self):
print("The shark is being awesome.")
def main():
shyam = Shark()
shyam .swim()
shyam.be_awesome()
if __name__ == "__main__":
main()
7 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
Example 2
Create a class named Person, use theinit() function to assign values for name and age:
In [ ]: class Person:
def __init__(self, name, age):
self.name = name
self.age = age
John
36
Secial Attributes
There are also special attributes in it that begins with double underscores. For exampledoc__gives us the
docstring of that class.
Example 3
Insert a function that prints a greeting, and execute it on the p1 object:
Object Methods
Objects can also contain methods. Methods in objects are functions that belong to the object.
8 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
In [ ]: class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
def myfunc(self):
print("Hello my name is " + self.name)
def main():
shyam = Person("John", 36)
shyam.myfunc()
if __name__ == "__main__":
main()
It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of
any function in the class:
Example
Use the words mysillyobject and abc instead of self:
9 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
In [ ]: class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
Example
Set the age of p1 to 50:
In [ ]: p1.age = 50
print(p1.age)
50
Example
Delete the age property from the p1 object:
In [ ]: del p1.age
# print(p1.age)
Delete Objects
You can delete objects by using the del keyword:
10 of 11 05-11-2020, 12:21
3_Functions file:///C:/Users/Shyam/AppData/Local/Temp/3_Functions.html
Example
Delete the p1 object:
In [ ]: del p1
11 of 11 05-11-2020, 12:21