KEMBAR78
Python 3 | PDF
0% found this document useful (0 votes)
8 views3 pages

Python 3

The document contains a series of Python functions that demonstrate various programming concepts. These include calculating Fibonacci numbers, digit sums, reversing strings, swapping values, finding the maximum of three numbers, calculating the area of a circle, and merging two lists. Each function is accompanied by user input prompts and outputs for demonstration.

Uploaded by

magarratna471
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Python 3

The document contains a series of Python functions that demonstrate various programming concepts. These include calculating Fibonacci numbers, digit sums, reversing strings, swapping values, finding the maximum of three numbers, calculating the area of a circle, and merging two lists. Each function is accompanied by user input prompts and outputs for demonstration.

Uploaded by

magarratna471
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1)

def fibonacci(n):
if n == 0 or n == 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)

n = int(input("Enter a number: "))


print("Fibonacci number:", fibonacci(n))
output:-

2)
def digit_sum(n):
if n < 10:
return n
return digit_sum(sum(int(i) for i in str(n)))

n = int(input("Enter a number: "))


print("Digit sum:", digit_sum(n))
Output:-

3)
def reverse(s):
if s == "":
return s
return reverse(s[1:]) + s[0]

s = input("Enter a string: ")


print("Reversed string:", reverse(s))
Output:-

4)
def swap(a, b):
a, b = b, a
return a, b
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
x, y = swap(x, y)
print("After swapping:")
print("First number:", x)
print("Second number:", y)
Output:-

5)
def maximum(a, b, c):
return max(a, b, c)

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))
z = int(input("Enter third number: "))

print("Maximum number is:", maximum(x, y, z))


Output:-

6)
def area(radius):
return 3.1416 * radius * radius

r = float(input("Enter radius: "))


print("Area of circle:", area(r))
Output:-

7)
def merge(l1, l2):
return sorted(l1 + l2)
l1 = [1, 3, 5]
l2 = [2, 4, 6]
result = merge(l1, l2)
print("Merged list:", result)
Output:-

You might also like