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

Functions Question Practical 1111

The document provides Python functions for basic programming tasks, including calculating the square of a number, checking if a number is even or odd, finding the factorial of a number, and checking if a string is a palindrome. It also includes a program that accepts a list of numbers from the user and identifies the largest and smallest numbers in that list. Each function is accompanied by example outputs to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Functions Question Practical 1111

The document provides Python functions for basic programming tasks, including calculating the square of a number, checking if a number is even or odd, finding the factorial of a number, and checking if a string is a palindrome. It also includes a program that accepts a list of numbers from the user and identifies the largest and smallest numbers in that list. Each function is accompanied by example outputs to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Easy - Small Program


Question:

Write a Python function to find the square of a


number.
Answer:
def square(num):
return num * num
print(square(5)) # Output: 25
. Easy - Medium Program
Write a function to check if a number is even or odd.
. Answer:

def check_even_odd(num):

if num % 2 == 0:

return "Even"

else:

return "Odd"

print(check_even_odd(7)) # Output: Odd


Write a function that returns the factorial of a number.
Answer:

def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
print(factorial(5))

# Output: 120
The loop multiplies numbers from 1 to n.

factorial(5) means 1*2*3*4*5 = 120.


Write a function that checks if a string is a palindrome (reads the same forwards and backwards).

Answer:

def is_palindrome(s):

s = s.lower() # make lowercase for case insensitivity

return s == s[::-1]

print(is_palindrome("Madam"))

# Output: True

Explanation:

s[::-1] reverses the string.

Compare original and reversed string.


Write a program with functions to:
Accept a list of numbers from the user.

Find the largest number. Find the smallest number .


def accept_numbers():
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
input(...).split()
return numbers  input() takes a single line of input as a string.

def find_largest(numbers):  .split() breaks that string into separate words (based on spaces) and gives a list of strings.
👉 Example: If user types 5 10 15, input().split() will return ['5', '10', '15'] (list of strings).
return max(numbers) 2. map(int, [...])

def find_smallest(numbers):  map(int, ...) applies the int() function to each item in the list.
 It converts each string like '5', '10', '15' into integers 5, 10, 15.
return min(numbers) 👉 After map(int, ...), you get something like: map object that acts like [5, 10, 15] (numbers now).

# Main Program 3. list(map(...))


 map gives a special object, not a normal list.
nums = accept_numbers()  list(...) is used to convert that map object into a real list of integers.

print("Largest number:", find_largest(nums))


 👉 Finally, numbers becomes: [5, 10, 15]

• print("Smallest number:", find_smallest(nums))

You might also like