Apa itu def di python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In python, a function is a logical unit of code containing a sequence of statements indented under a name given using the “def” keyword. In python def keyword is the most used keyword.

    Syntax:

    def function_name: 
        function definition statements...

    Use of def keyword:

    • In the case of classes, the def keyword is used for defining the methods of a class.
    • def keyword is also required to define the special member function of a class like __init__().

    The possible practical application is that it provides the feature of code reusability rather than writing the piece of code again and again we can define a function and write the code inside the function with the help of the def keyword. It will be more clear in the illustrated example given below. There can possibly be many applications of def depending upon the use cases. 

    Example 1: Use of def keyword.

    In this example, we are going to create a user define a function using def keyword.

    Python3

    def subNumbers(x, y):

        return (x-y)

    a = 90

    b = 50

    result = subNumbers(a, b)

    print("subtraction of ", a, " and ", b, " is = ", result)

    Output:

    subtraction of  90  and  50  is =  40

    Example 2: User defines a function with first 10 prime numbers.  

    Python3

    def prime(n):

        x = 1

        count = 0

        while count < n:

            for d in range(2, x, 1):

                if x % d == 0:

                    x += 1

            else:

                print(x)

                x += 1

                count += 1

    n = 10

    print("First 10 prime numbers are:  ")

    prime(n)

    Output: 

    First 10 prime numbers are:  
    1
    2
    3
    5
    7
    11
    13
    17
    19
    23

     Example 3: User defines a function with a factorial number.

    Python3

    def factorial(n):

        if n == 1:

            return n

        else:

            return n*factorial(n-1)

    num = 6

    if num < 0:

        print("Sorry, factorial does not exist for negative numbers")

    elif num == 0:

        print("The factorial of 0 is 1")

    else:

        print("The factorial of", num, "is", factorial(num))

    Output:

    The factorial of 6 is 720

    Apa guna def dalam Python?

    def pada bahasa python adalah suatu cara untuk mendifinisikan sebuah method atau fungsi.

    Struktur data Python apa saja?

    Sekarang kita akan mempelajari struktur data List, Tuple, Set, dan Dictionary..
    1. List. List adalah salah satu struktur data dalam bahasa pemrograman python yang mampu menyimpan kumpulan data (objek/nilai), yang disebut elemen list. Elemen pada list tersimpan menurut urutan (sequence) tertentu. ... .
    2. Tuple. ... .
    3. Set..

    Apa itu Len di Python?

    2. Len() Kita masuk dalam pembahasan yang pertama yaitu fungsi Len(). Fungsi len() digunakan untuk mengidentifikasi dan mengetahui seberapa panjang jumlah item atau anggota pada suatu objek. Penerapan fungsi len() ini bisa dipraktekkan pada berbagai jenis data seperti data sequence dan data collection.

    Apakah kegunaan tanda pagar (#) dalam Python?

    Anda dapat menggunakan tanda pagar '#' untuk menjadikan baris kode di Python menjadi komentar.