Assignment & Augmented Operators

Assignment Operators ဆိုတာ ဘာလဲ?

Assignment Operators တွေက Variable တွေထဲကို တန်ဖိုးထည့်တဲ့ကောင်တွေ။ ဒီ Chapter မှာ Operator နှစ်မျိုး ရှိတယ်။

  • Simple Assignment (=) - တန်ဖိုးထည့်တာ။
  • Augmented Assignment (+=, -=, etc.) - Shortcut တွေ။

Augmented ဆိုတာ တိုးမြှင့်တာ၊ ပေါင်းထည့်တာ။ ဒီကောင်တွေက Shortcut တွေ။

Simple Assignment Operator (=)

= ဆိုတာ Variable ထဲကို တန်ဖိုးထည့်တာ။ ဒါက အခြေခံအကျဆုံး Operator။

simple_assignment.py
# Variable ထဲကို တန်ဖိုးထည့်တာ x = 10 name = "Aung Aung" price = 5000.50 is_active = True print(x) # 10 print(name) # Aung Aung print(price) # 5000.5 print(is_active) # True
သတိ: = က ထည့်တာ (Assignment)။ == က စစ်တာ (Comparison)။

x = 5 ဆိုတာ x ထဲကို 5 ထည့်လိုက်တာ။
x == 5 ဆိုတာ x ထဲကကောင်က 5 နဲ့ တူလား လို့ စစ်တာ။

ပြဿနာ - Variable ကို Update လုပ်ချင်တယ်

ဥပမာ - bro မှာ score = 0 ရှိတယ်။ bro point ရတိုင်း score ကို 10 တိုးချင်တယ်။

ရိုးရိုး နည်း:

long_way.py
score = 0 # Point ရတိုင်း score ကို 10 တိုးမယ် score = score + 10 print(score) # 10 # နောက်တစ်ခါ point ပြန်ရတယ် score = score + 10 print(score) # 20

ရှင်းလင်းချက်:

score = score + 10 ကို ခွဲကြည့်မယ်:

  • score ထဲက လက်ရှိ တန်ဖိုး (0) ကို ယူ
  • 10 ပေါင်း (10 ဖြစ်သွား)
  • ရလာတဲ့ အဖြေသစ် (10) ကို score ထဲ ပြန်ထည့်လိုက်တာ
ပြဿနာ: Variable နာမည် ရှည်ရှည်ဖြစ်ရင် နှစ်ခါရေးရတာ ညစ်တယ်၊ အချိန်ကုန်တယ်။
my_super_long_variable_name = my_super_long_variable_name + 1

နည်းအသစ် - Augmented Assignment Operators

အပေါ်က score = score + 10 ကို Shortcut အနေနဲ့ ဒီလိုရေးတယ်။

shortcut_way.py
score = 0 # Shortcut နည်း score += 10 # score = score + 10 နဲ့ တူတူပဲ print(score) # 10

= နဲ့ + ကို ပေါင်းလိုက်ပြီး += ဖြစ်သွားတယ်။ variable = variable + value ကို variable += value လို့ တိုချလိုက်တာ။

1. += (Addition Assignment)

addition_assignment.py
score = 100 score += 50 # score = score + 50 နဲ့ တူတူပဲ print(score) # အဖြေ: 150

2. -= (Subtraction Assignment)

Game မှာ HP လျော့တာမျိုးပေါ့။

subtraction_assignment.py
health = 100 health -= 20 # health = health - 20 print(health) # အဖြေ: 80

3. *= (Multiplication Assignment)

Bonus ရလို့ ပိုက်ဆံ နှစ်ဆ ဖြစ်သွားတာ။

multiplication_assignment.py
money = 5000 money *= 2 # money = money * 2 print(money) # အဖြေ: 10000

4. /= (Division Assignment)

တစ်ဝက် ဝေလိုက်တာ။

division_assignment.py
cake_pieces = 12 cake_pieces /= 2 # cake_pieces = cake_pieces / 2 print(cake_pieces) # အဖြေ: 6.0 (/ မို့လို့ float ဖြစ်သွားတာ မမေ့နဲ့)

5. //= (Floor Division Assignment)

floor_division_assignment.py
total = 15 total //= 2 # total = total // 2 print(total) # အဖြေ: 7 (ဒသမဖြုတ်ပစ်တယ်)

6. %= (Modulus Assignment)

modulus_assignment.py
remainder = 17 remainder %= 5 # remainder = remainder % 5 print(remainder) # အဖြေ: 2 (17 ကို 5 နဲ့ စားရင် အကြွင်း 2)

7. **= (Exponentiation Assignment)

exponentiation_assignment.py
base = 2 base **= 3 # base = base ** 3 print(base) # အဖြေ: 8 (2 သုံးထပ် = 2 * 2 * 2)

ဘာလို့ Shortcut တွေ သုံးရတာလဲ?

1. တိုတယ်

Code နည်းသွားတယ်။

shorter_code.py
# Long Way counter = counter + 1 # Short Way counter += 1

2. ရှင်းတယ်

Variable နာမည် ရှည်ရှည်ဖြစ်ရင် Shortcut က အများကြီး ပို သန့်တယ်။

cleaner_code.py
# Long Way - ညစ်တယ် my_super_long_variable_name = my_super_long_variable_name + 1 # Short Way - သန့်တယ် my_super_long_variable_name += 1

3. ဖတ်ရ လွယ်တယ် (Readable)

Developer တစ်ယောက်က += ကို တန်းမြင်တာနဲ့ ဪ... ဒီကောင် update လုပ်တာ မှန်း တန်းသိတယ်။

ဘယ်အခါ သုံးသင့်လဲ?
  • Variable ကို ကိုယ့်ဘာသာ update ပြန်လုပ်တဲ့နေရာ အားလုံးမှာ သုံးလို့ရတယ်။
  • Score တိုးတာ၊ HP လျော့တာ၊ Counter တိုးတာ စတဲ့ နေရာတွေမှာ အသုံးများတယ်။
  • Loop တွေထဲမှာ Counter variable တိုးတဲ့နေရာမှာ အရမ်း သုံးတယ်။

String တွေမှာရော သုံးလို့ရလား?

ရတာပေါ့။ += ကို String ဆက်တဲ့နေရာမှာ သုံးတယ်။

+= နဲ့ String ဆက်တာ:

string_concatenation.py
message = "Hello " message += "World" # message = message + "World" print(message) # အဖြေ: Hello World

*= နဲ့ String ထပ်တာ:

string_multiplication.py
line = "-" line *= 10 # line = line * 10 print(line) # အဖြေ: ----------

Practical String Examples:

building_output.py
# Output တစ်ခု တည်ဆောက်တာ output = "=== Report ===\n" output += "Name: Aung Aung\n" output += "Score: 95\n" output += "Status: Pass\n" output += "===============" print(output) # အဖြေ: # === Report === # Name: Aung Aung # Score: 95 # Status: Pass # ===============
creating_patterns.py
# Pattern ဆောက်တာ star = "*" star *= 5 print(star) # ***** # Box ဆောက်တာ border = "#" border *= 20 print(border) print("# Hello World! #") print(border) # အဖြေ: # #################### # # Hello World! # # ####################
menu_builder.py
# Menu တည်ဆောက်တာ menu = "" menu += "========== Menu ==========\n" menu += "1. Mohinga - 1500 ကျပ်\n" menu += "2. Shan Noodle - 1200 ကျပ်\n" menu += "3. Fried Rice - 2000 ကျပ်\n" menu += "==========================" print(menu)
အမိုက်စား String တွေမှာ += က အသုံးဝင်တယ်။ Report ဖန်တီးတာ၊ Output တည်ဆောက်တာ၊ Pattern ဆွဲတာတွေမှာ အသုံးများတယ်။

Practical Example: Shopping Cart System

ဈေးဝယ် ခြင်းတောင်း System တစ်ခု ဆောက်ကြည့်မယ်။

shopping_cart.py
print("=== Shopping Cart System ===") # အစပိုင်း တန်ဖိုးတွေ total_items = 0 total_price = 0.0 cart_list = "" # ပစ္စည်း (၁) - Shirt ဝယ်မယ် item1_name = "Shirt" item1_price = 15000 item1_quantity = 2 total_items += item1_quantity # 0 + 2 = 2 total_price += item1_price * item1_quantity # 0 + 30000 = 30000 cart_list += f"- {item1_name} x{item1_quantity} = {item1_price * item1_quantity} ကျပ်\n" print(f"ခြင်းတောင်းထဲ ထည့်ပြီး: {item1_name} x{item1_quantity}") # ပစ္စည်း (၂) - Pants ဝယ်မယ် item2_name = "Pants" item2_price = 25000 item2_quantity = 1 total_items += item2_quantity # 2 + 1 = 3 total_price += item2_price * item2_quantity # 30000 + 25000 = 55000 cart_list += f"- {item2_name} x{item2_quantity} = {item2_price * item2_quantity} ကျပ်\n" print(f"ခြင်းတောင်းထဲ ထည့်ပြီး: {item2_name} x{item2_quantity}") # ပစ္စည်း (၃) - Shoes ဝယ်မယ် item3_name = "Shoes" item3_price = 35000 item3_quantity = 1 total_items += item3_quantity # 3 + 1 = 4 total_price += item3_price * item3_quantity # 55000 + 35000 = 90000 cart_list += f"- {item3_name} x{item3_quantity} = {item3_price * item3_quantity} ကျပ်\n" print(f"ခြင်းတောင်းထဲ ထည့်ပြီး: {item3_name} x{item3_quantity}") # Discount တွက်မယ် (10%) discount_rate = 10 discount_amount = total_price * discount_rate / 100 total_price -= discount_amount # 90000 - 9000 = 81000 # အခွန် တွက်မယ် (5%) tax_rate = 5 tax_amount = total_price * tax_rate / 100 total_price += tax_amount # 81000 + 4050 = 85050 # ပြေစာ ပြမယ် receipt = "" receipt += "\n================================\n" receipt += " ပြေစာ\n" receipt += "================================\n" receipt += cart_list receipt += "--------------------------------\n" receipt += f"စုစုပေါင်း ပစ္စည်း: {total_items} ခု\n" receipt += f"Discount ({discount_rate}%): -{discount_amount} ကျပ်\n" receipt += f"အခွန် ({tax_rate}%): +{tax_amount} ကျပ်\n" receipt += "================================\n" receipt += f"ပေးရမည့် ငွေ: {total_price} ကျပ်\n" receipt += "================================" print(receipt)

Output Example:

Output
=== Shopping Cart System === ခြင်းတောင်းထဲ ထည့်ပြီး: Shirt x2 ခြင်းတောင်းထဲ ထည့်ပြီး: Pants x1 ခြင်းတောင်းထဲ ထည့်ပြီး: Shoes x1 ================================ ပြေစာ ================================ - Shirt x2 = 30000 ကျပ် - Pants x1 = 25000 ကျပ် - Shoes x1 = 35000 ကျပ် -------------------------------- စုစုပေါင်း ပစ္စည်း: 4 ခု Discount (10%): -9000.0 ကျပ် အခွန် (5%): +4050.0 ကျပ် ================================ ပေးရမည့် ငွေ: 85050.0 ကျပ် ================================
သတိထားမိလား? Shopping Cart System မှာ += ကို အများကြီး သုံးထားတယ်:
  • total_items += quantity (ပစ္စည်း အရေအတွက် တိုးတာ)
  • total_price += price (စုစုပေါင်း ငွေ တိုးတာ)
  • total_price -= discount (လျှော့စျေး နုတ်တာ)
  • total_price += tax (အခွန် ပေါင်းတာ)
  • cart_list += item (List ကို String ဆက်တာ)
  • receipt += line (ပြေစာ တည်ဆောက်တာ)

မှားတတ်တာ

Mistake 1: Variable ကို မဖန်တီးဘဲ += သုံးတာ

mistake_1.py
# နည်းအမှား - Variable မရှိသေးဘူး # score += 10 # Error! score ကို မဖန်တီးရသေးဘူး # နည်းအမှန် - Variable ကို အရင်ဖန်တီးရမယ် score = 0 score += 10 # အဆင်ပြေပြီ

Mistake 2: = နဲ့ += ကို ရောထွေးတာ

mistake_2.py
counter = 0 # မှားနေတဲ့ နည်း - ဒီလိုဆို counter ကို 10 ထည့်လိုက်တာပဲ counter = 10 # counter က 10 ဖြစ်သွားတယ် (လက်ရှိ တန်ဖိုးကို လျစ်လျူရှု) print(counter) # 10 # နည်းမှန် - လက်ရှိ တန်ဖိုးကို ပေါင်းချင်ရင် += သုံးရမယ် counter = 0 counter += 10 # counter က 0 + 10 = 10 ဖြစ်တယ် print(counter) # 10

Mistake 3: Type မတူတဲ့ ကောင်တွေ ပေါင်းတာ

mistake_3.py
# မှားတဲ့ နည်း - String နဲ့ Number ပေါင်းလို့ မရ # message = "Score: " # message += 10 # Error! # အမှန် - Number ကို String ပြောင်းရမယ် message = "Score: " message += str(10) # အဆင်ပြေပြီ print(message) # Score: 10 # မဟုတ်ရင် f-string သုံးတာ ပိုကောင်းတယ် score = 10 message = f"Score: {score}" print(message) # Score: 10
သတိထားရမယ့် အချက်:
  • Variable ကို += မသုံးခင် အရင် ဖန်တီးထားရမယ်။
  • = က အသစ်ထည့်တာ၊ += က လက်ရှိတန်ဖိုးကို ပေါင်းတာ။
  • Type မတူတဲ့ ကောင်တွေ ပေါင်းချင်ရင် Type Conversion လုပ်ရမယ်။
  • /= က အမြဲ float ပြန်ပေးတယ်။ //= သုံးရင် integer ရမယ်။

Long Way vs Short Way နှိုင်းယှဉ်ချက်

comparison_all.py
# အားလုံးကို နှိုင်းယှဉ်ကြည့်မယ် # Addition x = 10 x = x + 5 # Long Way x += 5 # Short Way # Subtraction x = 10 x = x - 3 # Long Way x -= 3 # Short Way # Multiplication x = 10 x = x * 2 # Long Way x *= 2 # Short Way # Division x = 10 x = x / 2 # Long Way x /= 2 # Short Way # Floor Division x = 10 x = x // 3 # Long Way x //= 3 # Short Way # Modulus x = 10 x = x % 3 # Long Way x %= 3 # Short Way # Exponentiation x = 2 x = x ** 3 # Long Way x **= 3 # Short Way

အနှစ်ချုပ်

Simple Assignment:

  • = - Variable ထဲကို တန်ဖိုးထည့်တာ။

Augmented Assignment Operators:

  • += - ပေါင်းပြီး ပြန်ထည့်တာ
  • -= - နုတ်ပြီး ပြန်ထည့်တာ
  • *= - မြှောက်ပြီး ပြန်ထည့်တာ
  • /= - စားပြီး ပြန်ထည့်တာ (float ရမယ်)
  • //= - အကြွင်းဖြုတ်စားပြီး ပြန်ထည့်တာ (integer ရမယ်)
  • %= - အကြွင်းယူပြီး ပြန်ထည့်တာ
  • **= - ထပ်ကိန်းတင်ပြီး ပြန်ထည့်တာ
The end:
  • = က ထည့်တယ်။
  • += , -= , *= , /= တွေက Shortcut တွေ။ ကိုယ့် variable ထဲက တန်ဖိုးကို ကိုယ့်ဘာသာ update ပြန်လုပ်တာ။
  • Code တိုသွားတယ်၊ ရှင်းသွားတယ်၊ ဖတ်ရလွယ်သွားတယ်။
  • String တွေမှာလည်း += နဲ့ *= သုံးလို့ရတယ်။
လွယ်လွယ်လေး။ bro Comparison တွေ၊ Logical တွေ ကျွမ်းပြီ။ Assignment တွေ သိပြီ။ နောက် Chapter မှာ If-Else Statement တွေ သိရမယ်။ အဲ့မှာ ဒီကောင်တွေ အကုန် ပေါင်းပြီး သုံးရမှာ။

Quick Reference

quick_reference.py
# Simple Assignment x = 10 # Augmented Assignment x += 5 # x = x + 5 (15) x -= 3 # x = x - 3 (12) x *= 2 # x = x * 2 (24) x /= 4 # x = x / 4 (6.0) x = 15 x //= 2 # x = x // 2 (7) x %= 3 # x = x % 3 (1) x = 2 x **= 3 # x = x ** 3 (8) # String Operations text = "Hello" text += " World" # "Hello World" line = "-" line *= 5 # "-----" # Practical Usage score = 0 score += 10 # Point တိုးတာ health = 100 health -= 20 # HP လျော့တာ money = 5000 money *= 2 # Bonus ရတာ