Variables၊ Data Types နှင့် Strings

3.15.2

ကိန်းဂဏန်းနဲ့ စာသားသုံးပါ။ Input ကို type ပြောင်းပြီး တန်ဖိုးတွေကို ဖတ်ရလွယ်အောင် ပုံစံချပါ။

ဒီစာမျက်နှာမှာ

နာမည်နှင့် Type များ

Assignment က နာမည်တစ်ခုကို object တစ်ခုနဲ့ ချိတ်ပေးပါတယ်။ int က ကိန်းပြည့်၊ float က floating-point ကိန်း၊ str က စာသား၊ bool က True သို့မဟုတ် False ပါ။ None ရဲ့ type က NoneType ဖြစ်ပါတယ်။ ပုံမှန်စားဖို့ /၊ အောက်ဘက်ကိန်းပြည့်ရအောင် စားဖို့ // သုံးပါ။

quantity = 3
unit_price = 2.5
print(quantity * unit_price)
print(7 / 2, 7 // 2)
နမူနာ ရလဒ်
7.5
3.5 3

စာသား သန့်စင်၍ ပုံစံချခြင်း

String ကို နေရာမှာတင် ပြောင်းလို့မရပါ။ strip() နဲ့ title() က string အသစ် ပြန်ပေးပါတယ်။ f-string မှာ ကွင်းတွန့်ထဲက expression ကို တွက်ပြီး .2f က ဒဿမနှစ်နေရာနဲ့ ပြပါတယ်။

item = "  iced coffee  ".strip().title()
price = 7.5
print(f"{item} — {price:.2f}")
နမူနာ ရလဒ်
Iced Coffee — 7.50

Input ကို Type ပြောင်းခြင်း

input() က string ပြန်ပေးပါတယ်။ မတွက်ခင် int() သို့မဟုတ် float() နဲ့ ပြောင်းပါ။ int("3.5") က ValueError ဖြစ်ပါတယ်။ ငွေပမာဏကို ကိန်းပြည့်များနဲ့ သိမ်းရင် cents လို အငယ်ဆုံး unit ကို သတ်မှတ်ပါ။

text = "4"
print(int(text) * 3)
နမူနာ ရလဒ်
12

တရားဝင် ကိုးကားချက်များ