"""
Default paramater value::
"""
# Function - ий параметрт эхлэл утга өгч болно.
def add_numbers_with_default_params(num1, num2 = 100):
return num1 + num2
result = add_numbers_with_default_params(100)
print(result)
# 200
result = add_numbers_with_default_params(100, 200)
print(result)
# 300
def student(name = 'Unknown', age = 0):
print(f'name is {name}')
print(f'age is {age}')
student()
# name is Unknown
# age is 0
student(name='Dorj')
# name is Dorj
# age is 0
student(age=23)
# name is Unknown
# age is 23
student(age=25, name="Бат")
# name is Бат
# age is 25
student("Дорж")
# name is Дорж
# age is 0
student(31)
# name is 31
# age is 0
student("Болд", 29)
# name is Болд
# age is 29
"""
* args, ** kwargs::
"""
# Функцэд дамжуулж өгсөн параметрүүдийг tuple, dictionary байдлаар авах боломжийг олгоно.
# tuple::
def tupleFunction(*name):
print("Намайг " + name[2] + " гэдэг.")
tupleFunction("Болд", "Солонго", "Бат")
# dictionary::
def dictFunction(**person):
newDict = person
print(newDict['name'])
print("Намайг ", newDict['name'],
" гэдэг,", newDict['age'], " настай.")
#print(f'Намайг {newDict['name']} гэдэг, {newDict['age']} настай.')
dictFunction(name = "Дорж", age = 28)
def student_with(name, age, *friends, **marks):
print(f'name: {name}')
print(f'age: {age}')
print(f'friends: {friends}')
print(f'marks: {marks}')
student_with('DORJOO', 15, 'Bataa', 'Sukhee', 'Tsetsgee',
math = 100, physics = 80, biology = 90)
"""
Recursion :: Функц өөрөө өөрийгөө дуудах,
"""
def minusRecursion(param):
if(param > 0):
result = param + minusRecursion(param - 1)
print(result)
else:
result = 0
return result
print("\n\nRecursion - 1::")
minusRecursion(6)
# 6 + 15 = 21
# 5 + 10 = 15
# 4 + 6 = 10
# 3 + 3 = 6
# 2 + 1 = 3
# 1 + 0 = 1
def plusRecursion(param):
if(param < 5):
result = param + plusRecursion(param + 1)
print(result)
else:
result = 4
return result
print("\n\nRecursion + 1::")
plusRecursion(0)
# 0 + 14 = 14
# 1 + 13 = 14
# 2 + 11 = 13
# 3 + 8 = 11
# 4 + 4 = 8
No comments:
Post a Comment