kamus
<code class="language-python">x = { 'name': 'python' }
fungsi dalam python
- Fungsi adalah unit/blok kode yang dapat digunakan kembali
- sintaksis
def <func-name>():
....
....
....
Dokumen
Aktivitas
- Mari membuat program kalkulator
- buat folder baru dan file baru bernama calc.py
- dalam modul python merujuk ke file dalam hal ini nama modulnya adalah
calc
"""Calculator with basic operations.
This module provides simple arithmetic functions such as addition,
subtraction, and multiplication. Each function accepts two integers
and returns an integer result.
"""
def add(a: int, b: int) -> int:
"""Add two integers.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The sum of `a` and `b`.
"""
return a + b
def sub(a: int, b: int) -> int:
"""Subtract one integer from another.
Args:
a (int): The number to subtract from.
b (int): The number to subtract.
Returns:
int: The result of `a - b`.
"""
return a - b
def mul(a: int, b: int) -> int:
"""Multiply two integers.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The product of `a` and `b`.
"""
return a * b
- memanggil fungsi dari modul yang sama
- tonton rekaman kelas untuk debugging
- memanggil fungsi dari modul lain
- Buat main.py
- Kita perlu menggunakan impor
- contoh impor
import calc
result = calc.add(5,6)
print(result)
result = calc.sub(6,4)
print(result)
from calc import add
result = add(5,6)
print(result)
from calc import add, sub
result = add(5,6)
print(result)
result = sub(6,4)
print(result)
from calc import add as c_add
def add():
pass
result = c_add(6,5)
print(result)
- Lihat Di Sini untuk perubahan
-
Lihat Di Sini untuk masalah yang terpecahkan
-
Latihan:
- buat fungsi berikut
- is_prime -> mengembalikan bool
is_prime(5) => True - faktor -> daftar pengembalian[int]
factors(20) => [1,2,4,5,10] - prime_factors -> mengembalikan daftar[int]
prime_factors => [2,5]
- is_prime -> mengembalikan bool
- buat fungsi berikut
Catatan Ruang Kelas DevOps 08/Okt/2025 – DevOps langsung dari Quality Thought


