LABSHEET-01
Topic Function
Write a program in python using function to
1. Check Square of a Number.
Ans def is_square(num):
root = num ** 0.5
return root.is_integer()
number = int(input("Enter a number: "))
if is_square(number):
print(f"{number} is a perfect square.")
else:
print(f"{number} is not a perfect square.")
2. Check Even or Odd.
Ans def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
number = int(input("Enter a number: "))
print(f"The number {number} is {check_even_odd(number)}.")
3. Check Factorial of a Number.
def factorial(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0 or n == 1:
return 1
else:
result = 1
for i in range(2, n + 1):
result *= i
return result;
number = int(input("Enter a number: "))
print(f"The factorial of {number} is {factorial(number)}.")
4. Check Sum of a List.
def sum_of_list(lst):
return sum(lst)
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
print(f"The sum of the list is {sum_of_list(numbers)}.")
5. Find Maximum in a List.
Ans def find_max(lst):
if not lst:
return "The list is empty."
return max(lst)
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
print(f"The maximum value in the list is {find_max(numbers)}.")
6. Reverse a String.
Ans def reverse_string(s):
return s[::-1]
input_string = input("Enter a string: ")
print(f"The reversed string is: {reverse_string(input_string)}")
7. Find Fibonacci Sequence.
Ans def fibonacci_sequence(N):
sequence = []
a, b = 0, 1
while len(sequence) < N:
sequence.append(a)
a, b = b, a + b
return sequence
N = int(input("Enter the number of Fibonacci numbers to generate: "))
print(f"The first {N} numbers in the Fibonacci sequence are:
{fibonacci_sequence(N)}")
8. Check if a Word is a Palindrome.
def is_palindrome(word):
return word == word[::-1]
word = input("Enter a word: ")
if is_palindrome(word):
print(f"{word} is a palindrome.")
else:
print(f"{word} is not a palindrome.")
9. Count Vowels in a String.
Ans def count_vowels(s):
vowels = 'aeiouAEIOU'
return sum(1 for char in s if char in vowel)
input_string = input("Enter a string: ")
print(f"The number of vowels in the string is: {count_vowels(input_string)}")
10. Find the GCD(Greatest Common Divisor) of Two Numbers.
Ans def gcd(a, b):
while b:
a, b = b, a % b
return a
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print(f"The GCD of {num1} and {num2} is {gcd(num1, num2)}")
LABSHEET-02
list and tuple
1. Write a program to find sum of All Elements in a List.
def sum_of_list(lst):
return sum(lst)
numbers = list(map(int, input("Enter numbers separated by space:
").split()))
print(f"The sum of the list is {sum_of_list(numbers)}.")
2. Write a program to remove Duplicates from a List.
def remove_duplicates(lst):
return list(set(lst))
numbers = list(map(int, input("Enter numbers separated by space:
").split()))
print(f"List after removing duplicates:
{remove_duplicates(numbers)}")
3. Write a program to find the Maximum and Minimum Elements in a
List.
def find_max_min(lst):
if not lst:
return "The list is empty."
max_element = max(lst)
min_element = min(lst)
return max_element, min_element
numbers = list(map(int, input("Enter numbers separated by space:
").split()))
max_num, min_num = find_max_min(numbers)
print(f"The maximum value in the list is {max_num}")
print(f"The minimum value in the list is {min_num}")
4. Write a program to check if Element Exists in a Tuple.
def element_exists(tpl, element):
return element in tpl
tpl = tuple(map(int, input("Enter tuple elements separated by
space: ").split()))
element = int(input("Enter the element to check: "))
if element_exists(tpl, element):
print(f"The element {element} exists in the tuple.")
else:
print(f"The element {element} does not exist in the tuple.")
5. Write a program to concatenate Two Tuples.
def concatenate_tuples(tuple1, tuple2):
return tuple1 + tuple2
tuple1 = tuple(map(int, input("Enter elements of the first tuple
separated by space: ").split()))
tuple2 = tuple(map(int, input("Enter elements of the second tuple
separated by space: ").split()))
result = concatenate_tuples(tuple1, tuple2)
print(f"The concatenated tuple is: {result}")
6. Write a program to Count Occurrences of an Element in a Tuple.
def count_occurrences(tpl, element):
return tpl.count(element)
tpl = tuple(map(int, input("Enter tuple elements separated by
space: ").split()))
element = int(input("Enter the element to count: "))
print(f"The element {element} occurs {count_occurrences(tpl,
element)} times in the tuple.")
LABSHEET-03
TOPIC -STRING
1. Write a program to find String Length.
def string_length(s):
return len(s)
input_string = input("Enter a string: ")
print(f"The length of the string is:
{string_length(input_string)}")
2. Write a program to make String Concatenation.
def concatenate_strings(str1, str2):
return str1 + str2
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
print(f"The concatenated string is: {concatenate_strings(string1,
string2)}")
3. Write a program to Count Vowels in a String.
def count_vowels(s):
vowels = 'aeiouAEIOU'
return sum(1 for char in s if char in vowels)
input_string = input("Enter a string: ")
print(f"The number of vowels in the string is:
{count_vowels(input_string)}")
4. Write a program to do String Reversal.
def reverse_string(s):
return s[::-1]
input_string = input("Enter a string: ")
print(f"The reversed string is: {reverse_string(input_string)}")
5. Write a program to Extract a Substring.
def extract_substring(s, start, end):
return s[start:end]
input_string = input("Enter the string: ")
start_index = int(input("Enter the start index: "))
end_index = int(input("Enter the end index: "))
print(f"The extracted substring is: {extract_substring(input_string,
start_index, end_index)}")
6. Write a program to Check if a String is a Palindrome.
def is_palindrome(s):
return s == s[::-1]
input_string = input("Enter a string: ")
if is_palindrome(input_string):
print(f"{input_string} is a palindrome.")
else:
print(f"{input_string} is not a palindrome.")
7.Write a program to Convert String to Uppercase.
def to_uppercase(s):
return s.upper()
input_string = input("Enter a string: ")
print(f"The uppercase version is: {to_uppercase(input_string)}")
8.Write a program to Check if String is a Digit.
def is_digit(s):
return s.isdigit()
# Example usage:
input_string = input("Enter a string: ")
if is_digit(input_string):
print(f"{input_string} is a digit.")
else:
print(f"{input_string} is not a digit.")