Thuta Learning
ရှာဖွေရန်
IntermediateData & Databasesintermediate

Basic Operations

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

NumPy arithmetic operations တွေက element-wise အလုပ်လုပ်ပါတယ်။ ဆိုလိုတာက array နှစ်ခုထဲက တူညီတဲ့ position မှာရှိတဲ့ element တွေကို တစ်စုံချင်းတွက်ပေးတာပါ။ Python loop မရေးဘဲ discount, tax, score adjustment, unit conversion စတာတွေကို အလွယ်တကူတွက်နိုင်ပါတယ်။

python
import numpy as np

price = np.array([1000, 1500, 2000])
tax = np.array([50, 75, 100])

print("Price + tax:", price + tax)
print("Price after 10% discount:", price * 0.9)
print("Price difference from 1500:", price - 1500)

price + tax က position တူတဲ့ element တွေကိုပေါင်းပါတယ်။ price * 0.9 က price element တိုင်းကို 0.9 နဲ့မြှောက်ပြီး 10% discount ပြီးသားတန်ဖိုးကိုတွက်ပါတယ်။ Scalar number တစ်ခုနဲ့ array ကိုတွက်တဲ့အခါ scalar ကို element တိုင်းပေါ်သက်ရောက်စေပါတယ်။

You should see
Price + tax: [1050 1575 2100] Price after 10% discount: [ 900. 1350. 1800.] Price difference from 1500: [-500 0 500]

Info

Array နှစ်ခုကို element-wise တွက်ချင်ရင် shape တူရတာများပါတယ်။ Shape မတူရင် broadcasting rule နဲ့ကိုက်မှသာတွက်လို့ရပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Python list မှာ [1, 2, 3] * 2 ဆိုရင် list ကိုနှစ်ခါထပ်ပေးတာပါ။ NumPy array မှာတော့ element တိုင်းကို 2 နဲ့မြှောက်ပေးပါတယ်။ ဒီကွာခြားချက်ကိုမမှားပါနဲ့။

လေ့ကျင့်ခန်း

Currency conversion, measurement conversion, price update တွေမှာ scalar operation က အလွန်အသုံးဝင်ပါတယ်။

python
import numpy as np

usd_prices = np.array([10, 25, 40])
exchange_rate = 2100
mmk_prices = usd_prices * exchange_rate

print("MMK prices:", mmk_prices)

You'll know it worked when: MMK prices: [21000 52500 84000]

Basic Operations | Thuta Learning