Saturday, January 25, 2020

Chapter -13 (Def function)


For learn how to download and install python link here: How to download and install python in window


For learn how to run and execute the code in python link here:How to write and run the code


In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.



<Simply If you want to learn def function then ignore first line which contain def command, like in example-1, Ignore first line def print_hello(), then you see same command as you learn in previous chapter, print ('Hello!') >

Example-1: Use of "def" function


Code:

def print_hello():
    print('Hello!')
print_hello()
print('1234567')
print_hello()

Result:

Hello!
1234567
Hello!


Explanation:- The first two lines define the function. In the last three lines we call the function twice.
One use for functions is if you are are using the same code over and over again in various parts of
your program, you can make your program shorter and easier to understand by putting the code
in a function.

Example-2: Draw Square by def function


Code:
def draw_square():
    print('*' * 15)
    print('*', ' '*11, '*')
    print('*', ' '*11, '*')
    print('*' * 15)
draw_square()

Result:
***************
*                          *
*                          *
***************


Explanation:- You see these example problem in chapter 1 also but then you use print command to print square. But now you can use def command and you can print square any time in the code by type draw_square() anywhere in the code.


Example-3: Write a program and print string/number different times.


Code:
def print_hello(n):
    print('Hello ' * n)
    print()
print_hello(3)
print_hello(5)
times = 2
print_hello(times)

Result:
Hello Hello Hello
Hello Hello Hello Hello Hello
Hello Hello


Explanation:- Here use n in bracket and in 2nd line write Hello*n which means you only write print hello and in bracket how many times you want to multiply Hello.

Example-4: Write a program and print string/number different times.


Code:
def multiple_print(string, n):
    print(string * n)
    print()
multiple_print('Hello', 5)
multiple_print('A', 10)

Result:
HelloHelloHelloHelloHello
AAAAAAAAAA


Explanation:- It is slightly different from previous example, In previous example we write "hello" in 2nd line but here below.

Example-5: Write a program and convert Celsius to Fahrenheit.


Code:
def convert(t):
    return t*9/5+32
print(convert(20))

Result:
68.0


Explanation:- Similar to previous example but here use return in 2nd line which is used for calculation purpose.

Example-6: Calculate the value of sin 45.


Code:
from math import sin
def deg_sin(x):
    return sin(x)
print (deg_sin(45))

Result:
0.8509035245341184


Explanation:- Here you see we calculate the value of sin but as you know the value of sin(45) is 0.70 not 0.85, But here different because python calculate trigonometric value in radian not in degree if you want to convert into degree then multiply by 3.14/180

Example-7: As another example, the Python math module contains trigonometric functions, but they only
work in radians. Let us write 45 sine function that works in degrees.


Code:
from math import pi, sin
def deg_sin(x):
    return sin(pi*x/180)
print (deg_sin(45))

Result:
0.7071067811865476


Explanation:- Python calculate all trigonometric value in radian so if we want to convert into degree, we multiply by pi/180 so we get answer in degree.
Here in 3rd line we multiply by pi/180 so we get answer in degree.


Example-8: write a function that solves the system of equations x = (d*e-b*f )/(a*d-b*c) and y = (a*f-c*e)/(a*d-b*c). We need our function to return both the x and y solutions. Take a=2, b=3 , c=4 ,d=1, e=2, f=5


Code:
def solve(a,b,c,d,e,f):
    x = (d*e-b*f)/(a*d-b*c)
    y = (a*f-c*e)/(a*d-b*c)
    return [x,y]
xsol, ysol = solve(2,3,4,1,2,5)
print('The solution is x = ', xsol, 'and y = ', ysol)

Result:
The solution is x =  1.3 and y =  -0.2


Explanation:- Compare with next example.

Example-9: write a function that solves the system of equations x = (d*e-b*f )/(a*d-b*c) and y = (a*f-c*e)/(a*d-b*c). We need our function to return both the x and y solutions. Take a=2, b=3 , c=4 ,d=1, e=2, f=5


Code:
a=2
b=3
c=4
d=1
e=2
f=5
xsol = (d*e-b*f)/(a*d-b*c)
ysol = (a*f-c*e)/(a*d-b*c)
print('The solution is x = ', xsol, 'and y = ', ysol)

Result:
The solution is x =  1.3 and y=  -0.2


Explanation:- In previous example we see def command but here as we solve problem in previous chapter.
In def command problem , we first write the equation and then give value of a,b,c,d,e,f but in this example first write value of a,b,c,d,e,f otherwise it show error.

Example-10: A return statement by itself can be used to end a function early.


Code:
def multiple_print(string, n, bad_words):
    if string in bad_words:
        return
    print(string * n)
multiple_print('Hello', 3, 'Hello')

Result:
Nothing


Explanation:- Here nothing print in result because bad_words=Hello so it stop the code, If you use anything else then Hello then it print Hello three times.

Example-11: Previous example problem


Code:
def multiple_print(string, n, bad_words):
    if string in bad_words:
        return
    print(string * n)
multiple_print('Hello', 3, 'Hell')

Result:
HelloHelloHello


Explanation:- Here if we change 'Hello' to 'Hell' then it means in bad_words not equal to Hell so it run the code.

Example-12: Default arguments


Code:
def multiple_print(string, n=1):
    print(string * n)
    print()
multiple_print('Hello', 5)
multiple_print('Hello')

Result:
HelloHelloHelloHelloHello

Hello


Explanation:- Same as previous example-4.

Example-13: Local variables


Code:
def func1():
    for i in range(10):
        print(i)

def func2():
    i=100
    func1()
    print(i)
func2()

Result:
0
1
2
3
4
5
6
7
8
9
100


Explanation:- You see this example in chapter-2 (for loop), but here now we solve the problem by def function.
First we use for loop repeation of 10 and then only write i=100 and then use func2() to complete call the function.


Practice Questions:



No comments:

Post a Comment

If you have any doubt, let me know.

How and Why you should learn python? About Python Programming Language Python is the programming language. By the help of Python you can ...