Example :function program in Python is used to show the Default Argument/Parameter.
def msg(name="World"):
    return f"Hello, {name}!"

print(msg())        # Output: Hello, World!  Default Parameter.
print(msg("India")) # Output: Hello, India!

NB: Here, the default value is 'World'. When the parameter value is not supplied to the function then default value works.

------------  OR  ------------

class Calculator:
    def add(self, a, b=0, c=0):
        return a + b + c

# Create an object of the Calculator class
calc = Calculator()

# Call the method with different numbers of arguments
print(calc.add(10))          
print(calc.add(10, 20))       
print(calc.add(10, 20, 30))   

Output;
10  #(by adding 10 + 0 + 0)
30  #(by adding 10 + 20 + 0)
60  #(by adding 10 + 20 + 30)
Example : A user-defined function(UDF) program in Python is used to show the addition result of two user-accepted values.
# No Argument No Return UDF
 
x=int(input("Enter first value ="))
y=int(input("Enter second value ="))
def Addition():
    z=x+y
    print("The addition result is = ",z)    
Addition()

Output:
Enter first value =10
Enter second value =20
The addition result is =  30

------------  OR  ------------

def Addition():
    x=int(input("Enter first value = "))
    y=int(input("Enter second value = "))
    
    z=x+y
    print("The addition result is = ",z)
    
Addition()

Output:
Enter first value = 10
Enter second value = 20
The addition result is = 30

------------  OR  ------------

def Addition():
    x=50
    y=75
    
    z=x+y
    print("The addition result is = ",z)
    
Addition()

Output:
The addition result is =  125

# With Argument No Return UDF

def Addition(x,y):
    a=x 
    b=y
    c=a+b
    print("The addition result is = ",c)    
Addition(10,20)

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    print("The addition result is = ",z)    
Addition(10,20)

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    print("The addition result is = ",z)
    
m=int(input("Enter first value = "))
n=int(input("Enter second value = "))
Addition(m,n)

Output:
Enter first value = 100
Enter second value = 200
The addition result is =  300

# No Argument With Return UDF

def Addition():
    x=20
    y=50
    
    z=x+y
    return z
# print("The addition result is = ", Addition())

k=Addition()
print("The addition result is = ", k)

Output
The addition result is =  70

------------  OR  ------------

def Addition():
    x=int(input("Enter first value = "))
    y=int(input("Enter second value = "))
    
    z=x+y
    return z
# print("The addition result is = ", Addition())

k=Addition()
print("The addition result is = ", k)

Output:
Enter first value = 20
Enter second value = 30
The addition result is =  50

# With Argument With Return UDF

def Addition(x,y):
    z=x+y
    return z
k=Addition(10,20)
print("The addition result is = ",k)

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    return z
Addition(10,20)
print("The addition result is = ", Addition(10,20))

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    return z
    
m=int(input("Enter first value = "))
n=int(input("Enter second value = "))
print("The addition result is = ",Addition(m,n))

Enter first value = 20
Enter second value = 30
The addition result is =  50
Example : A UDF program in Python to show the accepted single value is Odd or Even.
# No Argument No Return

def OddEven():
    x=int(input("Enter your value = "))
    if x%2==0:
        print("Value is Even")
    else:
        print("Value is Odd")
    
OddEven()

Output:
Enter your value = 20
Value is Even

# No Argument With Return

def OddEven():
    x=int(input("Enter first value = "))
    if x%2==0:
        return "Value is Even"
    else:
        return "Value is Odd"
    
result = OddEven()
print(result)

Output:
Enter first value = 41
Value is Odd

# With Argument No Return

def OddEven(x):
    if x%2==0:
        print ("Value is Even")
    else:
        print ("Value is Odd")
        
y=int(input("Enter your value = "))    
OddEven(y)

# With Argument With Return

def OddEven(x):
    if x%2==0:
        return "Value is Even"
    else:
        return "Value is Odd"
        
y=int(input("Enter your value = "))    
result = OddEven(y)
print(result)
Example : A UDF program in Python is used to show the table’s values of the accepted single value.
# No Argument No Return

def Addition():
    x=int(input("Enter table value = "))
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    
Addition()

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

-------------  OR  -------------

def Addition():
    x=int(input("Enter table value = "))
    k=x
    while x<=k*10:
        print(x,end=" ")
        x=x+k
    
Addition()

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

# No Argument With Return

def Addition():
    x=int(input("Enter table value = "))
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    return i
result=Addition()
print (result)

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

# With Argument No Return

def Addition(x):
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    
y=int(input("Enter table value = "))
Addition(y)

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

-------------  OR  -------------

def Addition(x):
    k=x
    for i in range(1,11):
        print (k,"*",i,"=",k*i)
    
y=int(input("Enter table value = "))
Addition(y)

Output:
Enter table value = 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

# With Argument With Return

def Addition(x):
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    return i
    
y=int(input("Enter table value = "))
result=Addition(y)
print (result)

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

-------------  OR  -------------

# Function to generate and return a single line of the table

def table(number, multiplier):
    return f"{number} x {multiplier} = {number * multiplier}"

def main():
    
    num = int(input("Enter a number for table: "))
    
    print("\nThe Table Values are : ")
    for i in range(1, 11):
        values = table(num, i)
        print(values)

# Call the main function
if __name__ == "__main__":
    main()

Output:
Enter a number for table: 8

The Table Values are : 
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.