def keyword in Python-Def is keyword to make a user defined function

FUNCTION CONCEPT


Def 


Def is syntax to make or write a function

Syntax of Function
Keyword def that marks the start of the function header. A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python. Parameters (arguments) through which we pass values to a function. They are optional.



1.here def is word that we can use to make function by giving any name ,
then under your function you can write any operation .

Let understand using example:

#here abcd() is become your function you can write any instrucction under this


def abcd():


2.Now you have wrote your program and you have to execute or run the program so you have write that function of the operation then your program will run

To undersatnd above line let take a example
def abcd():
    sum=0
    num=int(input("Enter a first number:"))
    num2=int(input("Enter a second number:"))
    sum=num+num2
    print("sum of the two numbers is:",sum)
        


3.Now we have wrote our operation whatever we wanted 
#Now the time to execute this program you just have to write your function() name that you have made 

Q.What's your function in the above code ?
ans.Your function is abcd() ,

NOTE:And if you want you write any other name like sum ,...........etc any name you can write that is not predefined .


Now we are going to execute this program

def abcd():
    sum=0
    num=int(input("Enter a first number:"))
    num2=int(input("Enter a second number:"))
    sum=num+num2
    print("sum of the two numbers is:",sum)
abcd()

#Just you have to write your function name after writing the operation code .

#the best profit of using "def" is to make a program very very small only of two lines
To understand above line 
let take a example


Testcases1:
if your code in the first file and you have saved and you have given the name to that fille
then in next file you open 
write "import" and file name 
then filename.function name
Now you will run your output will shown
understand by below given image




See first file saved as sum


you can see we have opened another file and only you just and write only two line and your operation will run whatever you wrote in the saved file 

This is the profit of using def syntax.





Now we will do sum program using def sytnatx :
Q.1)
Program to find add two elementS

def sum():
#To make sumof two numbers assume sum equal to 0
    sum=0
#Now use int() function to type caste the input in the integer form 
    num=int(input("Enter a first number:"))
    num2=int(input("Enter a second number:"))
#Take sum variable
    sum=num+num2
    print("sum of the two numbers is:",sum)
#To print your operation you have to write your fuction name that you have made
sum()



Q.Write a program to remove duplicate element from the list


Testcases1:

INPUT

List=[1,3,5,5,3]


OUTPUT:
L=[1,3,5]


Testcases2:
INPUT
List=[100,45,2,3,4,2,3,2,100,1,12,3,2]
OUTPUT:
L=[100,45,2,3,4,1,12]





#Let take def keyword to make function named as duplicateelement()

        def duplicateelement():
            List=eval(input("Enter an element in the list:"))
            #Assume empty list
            L=[]
            #Take a loop to iterate
            for ele in range(len(List)):
            
        #Now test the condition using if else structure if the condition is true then do this operation i.e you given
                if List[ele] not in L:
                    L.append(List[ele])
        #If the condition is false then ues pass keyword 
                else:
                   pass
        #Then use print() to print the list
            print("Your list after removing duplicate element is :",L)
        #In the last without writing  the function that you have made your program will not execute in console screen
        duplicateelement()
        



Q.How to print diamond in python

#here we have used def() function that is only for 12 class students. Only 12 class students can do easily this program. 

Q. Write a program to print diamond pattern. 
""" WAP to print diamond pattern 
"""

def diamond(no):

    for i in range(no):

        print(' '*(no-i-1) + "* " * (i+1))

    for j in range(no-1,0,-1):

        print(' '*(no-j) + "* " * (j))

print(dimond(3))

Output:




























No comments:

Powered by Blogger.