set default value function python code example
Example 1: how to set a default parameter in python
def my_function(num1,num2=0):
return num1 + num2
print(my_function(1,2))
print(my_function(4))
Example 2: set default python
newDict = {1:"One",
2:"Two",
3:"Three"}
x=newDict.setdefault(4,"Four")
print(newDict[4])
newDict2 = {1:"One",
2:"Two",
3:"Three",
"Hello":"World"}
y=newDict.setdefault("Hello","Yellow")
print(newDict2["Hello"])
dict1 = {1:"One",2:"Two"}
x=dict1.setdefault(3,"Three")
x=dict1.setdefault(2,"Duo")
print(dict1)
Example 3: default values python
class Test:
def __init__(self, val, num = 0):
self.val = val
self.num = num
t = Test(1)
print(t.num)
t = Test(1, 2)
print(t.num)