Assignment statement គឺជាសញ្ញា = ដែលជាបញ្ជាតំរូវអោយភ្ជាប់ឈ្មោះណាមួយទៅនឹងវត្ថុណាមួយ។ យើងគួររំលឹកឡើងវិញថា ដើម្បីភ្ជាប់ឈ្មោះណាមួយទៅនឹងវត្ថុណាមួយ យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
1 2 3 | profit = 1000 print (profit) |
ក្រៅពីការភ្ជាប់ឈ្មោះណាមួយ ទៅនឹងវត្ថុណាមួយ យើងអាចភ្ជាប់ឈ្មោះជាច្រើនទៅនឹងវត្ថុមួយចំនូន ដោយសរសេរកម្មវិធីដូចខាងក្រោមនេះ៖
1 2 3 4 5 | sale, buy, profit = 1000 , 900 , 100 print (sale) print (buy) print (profit) |
900
100
យើងបានដឹងរួមមកហើយថា ការសរសេរថា sale, buy, profit និងការសរសេរថា 1000, 900, 100 គឺជាការបង្កើតកំរង tuple (sale, buy, profit) និងកំរង tuple (1000, 900, 100) ។ ដូចនេះ ការសរសេរថា sale, buy, profit = 1000, 900, 100 ស្មើនឹងការសរសេរថា (sale, buy, profit) = (1000, 900, 100) ។ សរុបមក ការសរសេរថា sale, buy, profit = 1000, 900, 100 គឺជាការប្រើប្រាស់ assignment statement ដើម្បីភ្ជា់បឈ្មោះនៅក្នុងកំរង tuple មួយទៅនឹងវត្ថុនៅក្នុងកំរង tuple មួយទៀត។
ដូចគ្នាដែរ ក្រៅពីកំរង tuple យើងក៏អាចភ្ជាប់ឈ្មោះនៅក្នុងកំរង list ទៅនឹងវត្ថុនៅក្នុងកំរងប្រភេទណាក៏បានដែរ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 5 6 7 | [sale, buy, profit] = [ 1000 , 900 , 100 ] (width, height, depth) = [ 30 , 15 , 10 ] [name, last_name, age] = ( 'Kosal' , 'Keo' , 35 ) print (sale) print (width) print (last_name) |
30
Keo
លើសពីនេះទៀត យើងក៏អាចប្រើប្រាស់ assignment statement ដើម្បីធ្វើការផ្តោះប្តូរឈ្មោះរបស់វត្ថុពីរ ទៅវិញទៅមកបានដែរ។
1 2 3 4 5 6 | width = 173.33 larger = 200 width, larger = larger, width print (width) print (larger) |
173.33
ក្រៅពីការប្រើប្រាស់ assignment statement ដើម្បីភ្ជាប់ឈ្មោះមួយទៅនឹងវត្ថុមួយ យើងក៏អាចភ្ជាប់ឈ្មោះជាច្រើនទៅនឹងវត្ថុតែមួយ ដោយធ្វើដូចខាងក្រោមនេះ៖
1 2 3 4 5 | sale = buy = fee = 1000 print (sale) print (buy) print (fee) |
1000
1000
នៅក្នុងភាសា Python យើងអាចធ្វើប្រមាណវិធីម៉្យាង ដូចខាងក្រោមនេះ៖
1 2 3 4 | sale = 1000 sale = sale + 1 print (sale) |
sale = sale + 1 គឺជាការប្រើប្រាស់ assignment statement ដើម្បីបង្កើនតំលៃវត្ថុឈ្មោះ sale អោយធំជាងមុន 1 ។ ការធ្វើប្រមាណវិធីតាមរបៀបនេះ ត្រូវគេហៅថាជា augmented assignment ដែលអាចត្រូវសរសេរតាមរបៀបម៉្យាងទៀត ដូចខាងក្រោមនេះ៖
1 2 3 4 | sale = 1000 sale + = 1 print (sale) |
ក្រៅពីការប្រើប្រាស់ augmented assignment ជាមួយនឹង operator + យើងក៏អាចប្រើប្រាស់ augmented assignment នេះជាមួយនឹង operator ផ្សេងៗទៀតបានដែរ។
1 2 3 4 5 6 7 8 | sale = 1000 sale + = 2 sale - = 2 sale * = 2 sale / = 2 sale / / = 2 sale % = 2 sale * * = 2 |
ដូចគ្នាដែរ យើងអាចប្រើប្រាស់ augmented assignment នេះជាមួយនឹងឃ្លាប្រយោគផ្សេងៗបានផងដែរ។
1 2 3 4 5 | saying = 'Drop by drop water breaks a stone.' saying + = '\n' saying * = 3 print (saying) |
Drop by drop water breaks a stone.
Drop by drop water breaks a stone.
ដូចគ្នាដែរ យើងអាចប្រើប្រាស់ augmented assignment ជាមួយនឹងកំរង tuple ផ្សេងៗដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 | mytuple = ( 100 , 'profit' ) mytuple + = ( 50 , True ) print (mytuple) mytuple * = 2 print (mytuple) |
(100, 'profit', 50, True, 100, 'profit', 50, True)
ដូចគ្នាដែរ យើងអាចប្រើប្រាស់ augmented assignment ជាមួយនឹងកំរង list ដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 | mylist = [ 100 , 'profit' ] mylist + = [ 50 , True ] print (mylist) mylist * = 2 print (mylist) |
[100, 'profit', 50, True, 100, 'profit', 50, True]
ដូចគ្នាដែរ យើងអាចប្រើប្រាស់ augmented assignment ជាមួយនឹងសំណុំ set ដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 | myset = { 100 , 1.5 , 'profit' } myset - = { 1.5 } print (myset) myset | = { 50 , 4 , True } print (myset) myset & = { 50 , 4 , True } print (myset) myset ^ = { 100 , 1.5 } print (myset) |
{True, 50, 100, 4, 'profit'}
{True, 50, 4}
{True, 1.5, 100, 4, 50}