Function
Stored (and reused) step
def thing():
print('Hello')
print('Fun')
thing()
print('Zip')
thing()
ເປັນການເກັບຄ່າໂປຣແກຣມ ຫຼື ຟັງຊັ້ນໃດໜຶ່ງໄວ້ໃນຕົວແປໃດໜຶ່ງ ເຊັ່ນໃນຕົວຢ່າງນີ້ ເຮົາເກັບ
print(‘Hello’) print(‘Fun’)
ໄວ້ໃນ thing() ແລະເມື່ອໂປຣແກຣມຣັນໄປ ແລ້ວເຮົາເອີ່ນໃຊ້ thing() ມັນກໍຈະເອົາຟັງຊັ້ນທີ່ເກັບໄວ້ໃນນັ້ນມາຣັນ
Example 2
big = max('hello world')
print(big)
Output
w
ທີ່ເປັນ w ແບບນີ້ອາດເພາະວ່າ ຄ່າຂອງ character ຕາມຕົວເລກ ອາດເປັນຄ່າທີ່ໃຫຍ່ສຸດ ລຽງຕາມ A-Z
tiny = min('hello world')
print (tiny)
ທີ່ມັນໄດ້ຄຳຕອບເປັນ ຍະວ່າງກໍເພາະໃນ argument ນັ້ນມີຍະວ່າງເປັນ ທີ່ມີຄ່າເປັນຕົວເລກນ້ອຍສຸດ
-
img

ລຳດັບ character
| *!”#$%&’()+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{ | }~** |
Example 3
print (float (99) /100)
i = 42
ii = type(i)
print(ii)
f = float(i)
print(f)
ff = type (f)
print(ff)
print(1 +2*float(3) / 4 - 5)
Output
0.99
<class 'int'>
42.0
<class 'float'>
-2.5
Example 4
aval = '123'
print (type(aval))
print(aval + 1)
Output
aval = '123'
print (type(aval))
#print(aval + 1)
ival = int(aval)
print(type(ival))
print(ival + 1)
ເປັນການປ່ຽນປະເພດຂອງຂໍ້ມູນກ່ອນ ຈຶ່ງສາມານຳມາຄິດໄລ່ໄດ້ ເຊັ່ນໃນຕົວຢ່າງນີ້ ເນື່ອງຈາກ aval ມີຂໍ້ມູນທີ່ເປັນປະເພດ str ເຊິ່ງບໍ່ສາມາດນຳມາບວກ ຫຼືຄິດໄລ່ທາງຄະນິດສາດໄດ້ ສະນັ້ນສາມາດປ່ຽນໄດ້ໂດຍການໄຊ້ຄຳສັ່ງ int(aval) ເພື່ອປ່ຽນມາເປັນ int ແລ້ວຈຶ່ງນຳມາຄິດໄລ່
Output
ແຕ່ສຳລັບການປ່ຽນ ບໍ່ສາມາດປ່ຽນປະເພດຂໍ້ມູນທຸກຢ່າງໄດ້ ຖ້າຫາກຂໍ້ມູນນັ້ນ ບໍ່ໄດ້ເປັນຕົວເລັກກໍບໍ່ສາມາດປ່ຽນມາເປັນຕົວເລກໄດ້ເຊັ່ນໃນຕົວຢ່າງ
nsv = 'hello word'
niv = int(nsv)
Output
-
questions of this lecture
What is the purpose of the “def” keyword in Python?
-
ans:
It indicates the start of a function, and the following indented section of code is to be stored for later.