+ 1
Can any one confess me about def function in python..?
9 Antworten
+ 5
Let say if we want to test whether the number is even or odd. We create a function using def Keyword.
def isEven(x):
if x%2==0:
print("Even")
else:
print("Odd")
We can use the function many times as we want without needing to create many if else statements
x= 25
y= 10
isEven(x)
isEven(10)
without using def, maybe we need to:
if x%2==0:
print("Even")
else:
print("Odd")
if y%2==0:
print("Even")
else:
print("Odd")
+ 3
Säñ Ùmãř
function is used to write reusable code. For example as Muhd Khairul Amirin explained if you want to check even and odd of multiple numbers then you have to write different if else condition. In this situation your code maybe lengthy and also it will not be generic logic means for every other number you have to write another if else condition.
So to avoid this problem we make a function which will check for every number.
For example: Suppose we have a list and we want to check even and odd of each number in the list then we can do like this:
li = [1, 2, 4, 5, 6]
#defined function
def even_odd(li):
for x in li:
if (x % 2 == 0):
print (str(x) + " is even")
else:
print (str(x) + " is odd")
#calling function
even_odd(li)
+ 2
I ᴀᴍ "Tɪᴍᴇ" okay thank you🤗
+ 2
Säñ Ùmãř You are welcome
+ 2
Def function can we used when we want python to do something in a particular way that we set it. It avoids lots of lines of code.
As its name shows, def (define), is a way to define a new function to use it and it's really useful.
You can define a new function like this :
def TheFunction():
#the thing they should do
For example you want to see if a number is more than 10 or not.
You can use def this way :
def moreThanTen (x)
if x > 10:
print ("More than 10")
else :
print ("Equal or less than 10")
And you can call it this way with every number you like :
moreThanTen(4)
moreThanTen(11)
Or for another example, imagine you want to write a code in order to say hello and greet new users.
You have to write it this way without def :
name = input ()
print ("Hello " , name , " Welcome!")
But you can use def so easily it will became:
def greet(name):
print ("Hello " , name , " Welcome!")
And everytime you just need to call it:
greet (name)
I hope it was useful :)
+ 1
Säñ Ùmãř The 'def' keyword is used to create a function object in the heap, with its reference stored in the variable-name given just after the keyword.
+ 1
A function in Python is an object that takes arguments & returns a value.
Usually a function is defined using a def statement.
For example:
def add(x, y):
return x + y
"return" says to return a value. In our case, the function returns the sum of x & y.
Now we can call the function:
>>> add(1, 100)
101
>>> add('abc', 'def')
'abcdef'
A function can be of any complexity & return any objects (lists, tuples, & even functions).
The function can be used without "return". In this case, the function will return the value "None".
0
Muhd Khairul Amirin oh thanks🤗
0
I ᴀᴍ "Tɪᴍᴇ" and can you tell me briefly about all string functions too..