And performing the function the power
def function_name():
BODY OF THE FUNCTION
Here, we are defining a function one () which performs some operations.
What if we want to call this function?
We can create a function which can take values as parameters and will execute the operations performed with that parameter
Let us take and example and understand it
A pre-defined function is a function in which we just need to use it, we do not need to write anything inside it, rather it is a part of some package that you are importing in order to use that function. If you will get into that package, there you will find the definition of that function. Python is full of these pre- defined functions there are a lot of functions that we can use and get the desired output quite easily.
We can also give a value to a variable that is, giving it a default value. Incase the user is not giving the value to that variable or the user wants it to hold a value, then he can pass assign a value to that variable. let us look at an example to get a better understanding.
We can make a variable as a global variable. A global variable is declared before creating a function. It will hold that value which is assigned to it till the scope of the program. Let us look at an example and see its functioning.
total = 5
def func(a,b):
total = a+b
print('Sum is:', total)
func(6,7)
print('Value of total is:',total)
Output: Subtraction of g from j gives: 8
Division of g by j gives: 5.0
Let us look ant an example to understand it.
def type(par,*partuple):
print('value is:',par)
for par in partuple:
print('value is:',par)
type(30)
type(70,80,'Rahul',100.54)
value is: 100.54
In the above program, we are printing the values of the parameters that we are sending to our function type(). The “*partuple” is used for the variable inputs that are dynamic. The use of tuple gives us the liberty to enter different types of input variables like character type, float type etc. that we used in the above function. It is a very useful function for dynamic programming. This function provides us flexibility to do whatever we want to do.
Let us look at an example to get a better understanding.
o = lambda s1,s2: s1**s2;
print('The value of s1 to the power of s2 is ', o(5,4))
print('The value of s1 to the power of s2 is ', o(2,6))
See you in the next tutorial!
---------------------------------------------------------------------------------------------------------------------
If we want to take a particular functionality from a module, then we need to use the “from” that is, from Module_name import functionality.
If you want to use all the functionalities of a module, then you need to write an asterisk (*) like from module_name import *.
It is quite simple. Suppose I have made a program which calculates sum of numbers, then in the other file I can import that program as a module. All you need to do is:
Import operation from name_of_file/project.
we have imported the time module and used the localtime() function and the time function and then printed the local time.
Output:
Output:
Mon Jul 1 17:04:18 2019
t = calendar.month(2019,5)
print(‘calendar for the fifth month of 2019 is:’)
1 2 3 4 5
6 7 8 9 10 11 12
2 |
|
3 | |
4 | |
5 |
|
6 | |
7 | |
8 |
|
9 | |
10 | |
11 |
|
12 |