0% found this document useful (0 votes)
2 views3 pages

python_assignment3

The document contains Python code demonstrating the use of loops, conditionals, and functions. It includes examples of a while loop for user input, functions for calculating factorial, power, and finding the maximum value, as well as advanced functions for sorting and decorators. Additionally, it showcases recursion with a function to sum the digits of a number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

python_assignment3

The document contains Python code demonstrating the use of loops, conditionals, and functions. It includes examples of a while loop for user input, functions for calculating factorial, power, and finding the maximum value, as well as advanced functions for sorting and decorators. Additionally, it showcases recursion with a function to sum the digits of a number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#Part 1 while loop and if-elif-else loop

while True:
number = int(input("Enter an integer: "))
if number > 100:
print("Program ends")
break
elif number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")

for number in range(1, 21):


if number % 2 == 0:
print(f"{number}: Even")
else:
print(f"{number}: Odd")
#Part 2 Basic function that are Factorial and power and find_maximum
def factorial(n):
result = 1
while n > 0:
result *= n
n -= 1
return result

print(factorial(5))

def power(x, y=2):


return x ** y

print(power(3, 3))
print(power(4))

def find_maximum(*args):
if not args:
return None
return max(args)

print(find_maximum(1, 5, 9, 2, 8))
print(find_maximum()) #
#Part 3: Advanced Functions
students = [("Alice", 85), ("Bob", 90), ("Charlie", 78), ("David", 92)]
students_sorted = sorted(students, key=lambda student: student[1])
print(students_sorted)

def print_execution(func):
def wrapper(*args, **kwargs):
print("Before function execution")
result = func(*args, **kwargs)
print("After function execution")
return result
return wrapper

def greet(name):
print(f"Hello, {name}!")

greet("Alice")

def sum_of_digits(n):
if n == 0:
return 0
return n % 10 + sum_of_digits(n // 10)

print(sum_of_digits(1234))

You might also like