Codesphere Community
Відкрити в Telegram
Welcome to the Codesphere Community Coding Using any device, all source code is here.... YouTube Channel: https://youtube.com/@Codesphere-D01010 Email: BusinessCodeSphere@gmail.com Admin/owner: @Alghifari_01
Показати більше1 744
Підписники
Немає даних24 години
-67 днів
-1930 днів
Архів дописів
1 744
Search Github Profile ✓
Repositories ( Don't forget to give 🌟 )
https://github.com/DzarelDeveloper/SearchGithubProfile
Demo 👇
https://dzareldeveloper.github.io/SearchGithubProfile/
1 744
Hello everyone, sorry I haven't posted anything in recent days...
Why am I not posting?
Because I have very little time, I don't have time to post anything...
And
Starting today I will post code for you guys👌
1 744
Day 15 🔥 "Indonesia "
1. Handling Multiple Exceptions:
Fungsi
safe_divide menangani beberapa jenis exception, seperti ZeroDivisionError dan TypeError, untuk memberikan pesan error yang informatif ketika kesalahan terjadi.
def safe_divide(x, y):
"""Fungsi pembagian dengan penanganan beberapa jenis exception."""
try:
result = x / y
return result
except ZeroDivisionError:
print("Error: Pembagian oleh nol tidak diizinkan.")
except TypeError:
print("Error: Jenis data tidak sesuai untuk pembagian.")
result = safe_divide(8, 2)
print(f"Hasil pembagian: {result}")
result_error = safe_divide(5, '2')
# Akan mencetak pesan error karena tipe data tidak sesuai.
2. Mendokumentasikan Fungsi:
Fungsi power didokumentasikan menggunakan docstring. Docstring memberikan informasi tentang penggunaan, parameter, dan hasil yang diharapkan dari fungsi.
def power(base, exponent):
"""
Fungsi untuk menghitung pangkat.
Parameters:
- base (int): Basis pangkat.
- exponent (int): Eksponen pangkat.
Returns:
int: Hasil pangkat.
"""
return base ** exponent
result = power(2, 3)
print(f"2^3 = {result}")
3. Memahami dan Menggunakan Fungsi Generator:
Fungsi generator countdown menggunakan yield untuk membuat iterator yang menghasilkan nilai secara bertahap, dalam hal ini melakukan countdown dari suatu angka.
def countdown(n):
"""Fungsi generator untuk melakukan countdown."""
while n > 0:
yield n
n -= 1
for number in countdown(5):
print(number)
4. Fungsi Decorator:
Menggunakan fungsi decorator greeting_decorator untuk menambahkan sapaan pada fungsi get_name. Fungsi decorator menerima fungsi lain, menambahkan fungsionalitas, dan mengembalikan fungsi yang dimodifikasi.
def greeting_decorator(func):
"""Fungsi decorator untuk menambahkan sapaan pada fungsi."""
def wrapper(name):
result = func(name)
return f"Hello, {result}!"
return wrapper
@greeting_decorator
def get_name(name):
"""Fungsi sederhana untuk mendapatkan nama."""
return name
result = get_name("everyone")
print(result)1 744
Day 15 🔥 "English"
1. Handling Multiple Exceptions:
The
safe_divide function handles several types of exceptions, such as ZeroDivisionError and TypeError, to provide informative error messages when an error occurs.
def safe_divide(x, y):
"""Division function with handling of several types of exceptions."""
try:
result = x / y
return result
except ZeroDivisionError:
print("Error: Division by zero is not permitted.")
except TypeError:
print("Error: The data type is not suitable for sharing.")
result = safe_divide(8, 2)
print(f"Division results: {result}")
result_error = safe_divide(5, '2')
# Will print an error message because the data type does not match.
2. Documenting Functions:
The power function is documented using docstring. Docstring provides information about the function's usage, parameters, and expected results.
def power(base, exponent):
"""
Function to calculate powers.
Parameters:
- base (int): The base of the rank.
- exponent (int): Power exponent.
Returns:
int: Rank results.
"""
return base ** exponent
result = power(2, 3)
print(f"2^3 = {result}")
3. Understanding and Using Generator Functions:
The countdown generator function uses yield to create an iterator that produces values incrementally, in this case doing a countdown of a number.
def countdown(n):
"""The generator function is to perform countdown."""
while n > 0:
yield n
n -= 1
for number in countdown(5):
print(number)
4. Decorator Function:
Use the greeting_decorator decorator function to add a greeting to the get_name function. A decorator function accepts another function, adds functionality, and returns the demoted function.
def greeting_decorator(func):
"""Decorator function to add a greeting to the function."""
def wrapper(name):
result = func(name)
return f"Hello, {result}!"
return wrapper
@greeting_decorator
def get_name(name):
"""Simple function to get the name."""
return name
result = get_name("everyone")
print(result)1 744
Ilegal Script ✓
Made by : DzarelDeveloper ( Codesphere )
Code : Bash ( Shell )
Get it at Price ( $4 ) ( 62Rb )
@SupervisorCodesphere
When purchasing, you can get the source code and explanation of how to run it
Run In?
Termux And Linux
1 744
Jalankan Code Ini ( Run this code )
multiply = lambda x, y: x * y
result = multiply(5, 4)
print(f"Multiplication result: {result}")1 744
Day 14 🔥 "English"
1.. Further Modularity:
Separation of circle-related functions into a separate module,
geometry.py, to improve code structure and readability. Function calls from the module are made in main.py.
# File: geometry.py
def calculate_circle_properties(radius):
"""Function for calculating the properties of a circle."""
circumference = 2 * 3.14 * radius
area = 3.14 * radius ** 2
return circumference, area
# File: main.py
from geometry import calculate_circle_properties
radius = 5
circle_circumference, circle_area = calculate_circle_properties(radius)
print(f"Circumference: {circle_circumference}, Circle Area: {circle_area}")
2. Lambda Function:
Use lambda functions to create simple functions without formal definitions. In this example, the lambda function is used to perform multiplication.
multiply = lambda x, y: x * y
result = multiply(3, 4)
print(f"Multiplication result: {result}")
3.. Iterating through Functions:
Use the map function to apply the square_each function to each element in the numbers list. This produces a new list containing the elements that have been squared.
numbers = [1, 2, 3, 4, 5]
def square_each(number):
"""Function to square each element in a list."""
return number ** 2
squared_numbers = list(map(square_each, numbers))
print(f"Elements after being squared: {squared_numbers}")1 744
Day 14 🔥 "Indonesia"
1.. Modularitas Lebih Lanjut:
Pemisahan fungsi terkait lingkaran ke dalam modul terpisah,
geometry.py, untuk meningkatkan struktur dan keterbacaan kode. Pemanggilan fungsi dari modul tersebut dilakukan di main.py.
# File: geometry.py
def calculate_circle_properties(radius):
"""Fungsi untuk menghitung sifat-sifat lingkaran."""
circumference = 2 * 3.14 * radius
area = 3.14 * radius ** 2
return circumference, area
# File: main.py
from geometry import calculate_circle_properties
radius = 5
circle_circumference, circle_area = calculate_circle_properties(radius)
print(f"Keliling Lingkaran: {circle_circumference}, Luas Lingkaran: {circle_area}")
2. Fungsi Lambda:
Menggunakan fungsi lambda untuk membuat fungsi sederhana tanpa definisi formal. Dalam contoh ini, fungsi lambda digunakan untuk melakukan perkalian.
multiply = lambda x, y: x * y
result = multiply(3, 4)
print(f"Hasil perkalian: {result}")
3.. Iterasi melalui Fungsi:
Menggunakan fungsi map untuk menerapkan fungsi square_each pada setiap elemen dalam daftar numbers. Ini menghasilkan daftar baru yang berisi elemen-elemen yang telah dikuadratkan.
numbers = [1, 2, 3, 4, 5]
def square_each(number):
"""Fungsi untuk mengkuadratkan setiap elemen dalam daftar."""
return number ** 2
squared_numbers = list(map(square_each, numbers))
print(f"Elemen setelah dikuadratkan: {squared_numbers}")1 744
Jalankan Code Ini ( Run this code )
def factorial(n):
"""Fungsi rekursi untuk menghitung faktorial."""
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
result = factorial(5)
print(f"Faktorial dari 5: {result}")