Sunday, April 20, 2025

Python Function Arguments

Arguments are data that we pass to functions so that other operations can be done further using what we provided as input.

We will type arguments between parentheses.

This function will just add same number we provided to itself:


def addition(num_1):
    print("Addition:", num_1 + num_1)

addition(5)

Result: 


Addition: 10
>>> 

Sure, you can use multiple arguments, delimited by comma:


def print_them(x, y, z):
    print(x)
    print(y)
    print(z)

print_them(43, 24, "Something")

Result:  


43
24
Something
>>> 

And here, we see very simple calculator:


def calculator(x, y):
    print("Add: ", x + y)
    print("Sub: ", x - y)
    print("Mul: ", x * y)
    print("Div: ", x / y)

calculator(10, 5) 

Result:  


Add:  15
Sub:  5
Mul:  50
Div:  2.0
>>> 

Easy. In next tutorial we will learn how to take arguments from keyboard.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...