Cấp độ: elementary 30 phútmin Miễn phí Free Young Creator Young Creator

Python: Hàm và module Python: Functions and modules

Bộ hàm tính toán học A set of math calculation functions

Bạn sẽ học gì What you will learn

  • Định nghĩa và gọi hàm Define and call functions
  • Dùng tham số và return Use parameters and return
  • Import và dùng module Import and use modules

Bạn sẽ tạo gì What you will build

Bộ hàm tính toán học A set of math calculation functions

Code mẫu Sample code

python
import math
import random

def tinh_chu_vi(ban_kinh):
    """Tính chu vi hình tròn / Calculate circle circumference"""
    return 2 * math.pi * ban_kinh

def so_ngau_nhien(min_val, max_val):
    """Số ngẫu nhiên / Random number"""
    return random.randint(min_val, max_val)

# Gọi hàm / Call functions
r = 5
print(f"Chu vi / Circumference: {tinh_chu_vi(r):.2f}")
print(f"Số ngẫu nhiên / Random: {so_ngau_nhien(1, 100)}")