# Examples of Function - 1

Example 1:

```python
def hello():
    name = input("Enter your name : ")
    print("Welcome {} to the python functions.".format(name))
print("function calling starts")
x = hello()
print("function calling finished")
print(x)
```

Output

function calling starts

Enter your name : John

Welcome John to the python functions.

function calling finished

None

Example 2:

```python
# function to reverse string
def reverse(string):
    return string[::-1]
s = reverse(input("Enter a string : "))
print(s)
s = reverse(input("Enter a list space seperated : ").split())
print(s)
```

Enter a string : gyansetu

utesnayg

Enter a list space seprated : \[1 2]

\['2]', '\[1']&#x20;

Example 3:

```python
# function to calculate power
def pow(x,y):
    return x**y
x = pow(3,4) #positional Arguments
print(x)
```

Output

81

Example 4:

```python
# function to calculate power
def pow(x,y):
    return x**y
r = pow(y=2,x=3)
print(r)
```

Output

9

Example 5:

```python
# function to calculate power
def pow(x,y):
    return x**y
k = pow(2,4)
p = pow(2,3)
print(k)
print(p)
print(pow(y=2,x=1))
print(pow(3,y=5))
```

Output

16

8

1

243

Example 6:

```python
def swap(x,y):
    return y,x
x,y = int(input("Enter x : ")),int(input("Enter y : "))
x,y = swap(x,y)
print(x,y)
```

Output

Enter x : 3

Enter y : 6&#x20;

6 3

Example 7:

```python
def add(*args):
    return sum(args)
print(add())
print(add(1))
print(add(1,2))
print(add(1,2,3,4,5,6,7,8,9,10))
```

0

1

3

55

Example 8:

```python
def info(*args):
    print(type(args))
    c = 1
    for var in args :
        print("value {} : {}".format(c,var))
        c = c + 1
info('one','two')
print("\n\n")
info('hello','hi','how','are','you')
```

\<class 'tuple'>

value 1 : one

value 2 : two

\<class 'tuple'>

value 1 : hello

value 2 : hi

value 3 : how

value 4 : are

value 5 : you

(\*\*)  returns dictionary

Example 9:

```python
def info(**kwargs):
    print(type(kwargs))
    for key,value in kwargs.items():
        print("{} = {}".format(key,value))

info(name='sachin',)
print()
info(name='python',framework=['django','flask'])
print()
def info(**info):
    return info
st1 = info(name=input("Enter your name "),addr=input("Enter your address"))
print(st1)
```

\<class 'dict'>

name = sachin&#x20;

\<class 'dict'>

name = python

framework = \['django', 'flask']

Enter your name gyansetu

Enter your address gurgaon

{'name': 'gyansetu', 'addr': 'gurgaon'}

Example 10:

```python
def calc(x,y,ch):
    ch = ch.strip()
    if ch == '+' :
        return x + y
    elif ch == '-' :
        return x - y
    elif ch == '*' :
        return x * y
    elif ch == '/' or ch == '//' or ch == '%' :
        if y == 0  :
            return "Error!!Can not Divide by Zero"
        else :
            if ch == '/' :
                return x / y
            elif ch == '//' :
                return x // y
            else :
                return x % y
    elif ch == "**" :
        return x ** y
    else :
        return "Error!!!Invalid Operation"

r = calc(3,3,'**')
print(r)
```

27

Example 11:

```python
def fun(x,y=0,*mytuple):
    print("Positional Argument : ",x)
    print("Default Argument : ",y)
    print("Here is Your Var length arguments : ")
    c = 1
    for var in mytuple :
        print("{} Arg = {}".format(c,var))
        c = c + 1
    
fun(1)
fun(1,2)
fun(1,2,3,4,5,6,7,8,9)
```

Positional Argument : 1&#x20;

Default Argument : 0&#x20;

Here is Your Var length arguments :&#x20;

Positional Argument : 1&#x20;

Default Argument : 2&#x20;

Here is Your Var length arguments :

Positional Argument : 1&#x20;

Default Argument : 2&#x20;

Here is Your Var length arguments :&#x20;

1 Arg = 3

2 Arg = 4&#x20;

3 Arg = 5&#x20;

4 Arg = 6&#x20;

5 Arg = 7&#x20;

6 Arg = 8&#x20;

7 Arg = 9

Example 12:

```python
def fun1(*args):
    print(args)
fun1(1,2,3)
fun1(4,5,6,7,8,'hello','hi','bye')
```

(1, 2, 3)

(4, 5, 6, 7, 8, 'hello', 'hi', 'bye')

Example 13:

```python
def count_Digits(num):
    """count_Digits(num) -> This function takes a num as formal argument and return no of digits in the num."""
    count = 0
    while num:
        num = num // 10
        count = count + 1
    return count

n_digit = count_Digits(int(input("Enter number : ")))
print("Number of Digits = ",n_digit)
```

Enter number : 34

Number of Digits = 2

Example 14:

```python
#recursion
#Factorial
import time
num = int(input("Enter a no to calculate factorial : "))
c = time.time()
s = 1
for var in range(1,num+1):
    s = s * var
n = time.time()
#print(s)
print("Time Taken = ",n-c)
```

Enter a no to calculate factorial : 23

Time Taken = 0.0

Example 15:

```python
# Armstrong Number
def count_Digits(num):
    """count_Digits(num) -> This function takes a num as formal argument and return no of digits in the num."""
    count = 0
    while num:
        num = num // 10
        count = count + 1
    return count

def check_Armstrong(num):
    p = count_Digits(num)
    copy_num = num
    s = 0
    while num :
        r = num % 10 
        s = s + r**p
        num = num // 10 
    if s == copy_num :
        return True
    else :
        return False
        
def armstrong():
    if check_Armstrong(int(input("Enter a number : "))) :
        print("Given number is Armstrong ")
    else :
        print("Given number is not A Armstrong Number ")
    if input("\nType something to repeat Again") :
        armstrong()

armstrong()
```

Enter a number : 59

Given number is not A Armstrong Number

Type something to repeat Again

(Note:  An **Armstrong number** of three digits is an integer such that the sum of the cubes of its digits is equal to the **number** itself. For example, 371 is an **Armstrong number** since 3\*\*3 + 7\*\*3 + 1\*\*3 = 371. Write a program to find all **Armstrong number** in the range of 0 and 999)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-python.gitbook.io/python-programming/functions-in-python/examples-of-function-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
