Python Programs
Python Programs
def is_leap_year(year):
return True
else:
return False
# Example usage:
year_to_check = 2024 # Replace this with the year you want to check
if is_leap_year(year_to_check):
else:
char_count = {}
for char in input_string:
if char in char_count:
else:
char_count[char] = 1
# Example usage:
input_str = "hello" # Replace this with the string you want to check
if has_duplicate_characters(input_str):
else:
print("Repetition:", sample_string * 2)
print("Uppercase:", sample_string.upper())
print("Lowercase:", sample_string.lower())
# String formatting
name = "Alice"
age = 30
substring = "How"
if substring in sample_string:
else:
# Stripping whitespace
sample_list = [1, 2, 3, 4, 5]
print("Repetition:", sample_list * 2)
print("Accessing Elements:")
# List methods
if 3 in sample_list:
else:
sample_list.clear()
sample_tuple = (1, 2, 3, 4, 5)
print("Repetition:", sample_tuple * 2)
print("Accessing Elements:")
# Tuple methods
if 3 in sample_tuple:
else:
# Unpacking a tuple
a, b, *rest = sample_tuple
print("Unpacked Values:")
print("a:", a)
print("b:", b)
print("Rest:", rest)
# Nested tuple
tuple_to_list = list(sample_tuple)
list_to_tuple = tuple(tuple_to_list)
print("Accessing Values:")
print("Name:", sample_dict['name'])
print("Age:", sample_dict['age'])
# Dictionary methods
keys = sample_dict.keys()
values = sample_dict.values()
items = sample_dict.items()
print("Keys:", keys)
print("Values:", values)
print("Items:", items)
if 'name' in sample_dict:
print("'name' is a key in the dictionary.")
else:
# Dictionary comprehension
sample_dict.clear()
def sum_of_digits(n):
if n < 10:
return n
else:
# Recursive case: sum the last digit and the sum of the remaining digits
# Example usage:
number = 12345 # Replace this with the number for which you want to find the sum of digits
result = sum_of_digits(number)
def inverted_star_pattern(rows):
print("*", end="")
print()
# Example usage:
num_rows = 5 # Replace this with the number of rows you want in the pattern
inverted_star_pattern(num_rows)
def fibonacci(n):
if n <= 0:
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib_series = fibonacci(n - 1)
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series
# Example usage:
num_terms = 10 # Replace this with the number of terms you want in the Fibonacci series
result = fibonacci(num_terms)
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# Union of sets
union_set = set1.union(set2)
# Intersection of sets
intersection_set = set1.intersection(set2)
difference_set1 = set1.difference(set2)
difference_set2 = set2.difference(set1)
symmetric_difference_set = set1.symmetric_difference(set2)
is_subset = set1.issubset(set2)
is_superset = set1.issuperset(set2)
set1.add(6)
set2.remove(6)
# Clearing a set
set1.clear()
# Example usage:
# Ensure that the divisor is not zero to avoid division by zero error
if divisor != 0:
else:
import math
class Circle:
self.radius = radius
def calculate_area(self):
return area
def calculate_perimeter(self):
return perimeter
# Example usage:
if radius >= 0:
circle_instance = Circle(radius)
area = circle_instance.calculate_area()
perimeter = circle_instance.calculate_perimeter()
else:
def remove_duplicates(input_list):
unique_list = list(set(input_list))
return unique_list
# Example usage:
input_list = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7]
result_list = remove_duplicates(input_list)
class Animal:
self.name = name
def make_sound(self):
super().__init__(name)
self.breed = breed
def make_sound(self):
class Cat(Animal):
super().__init__(name)
self.color = color
def make_sound(self):
# Example usage:
dog_instance.make_sound()
cat_instance.make_sound()
class Person:
self.name = name
self.age = age
def display_info(self):
class Employee:
self.employee_id = employee_id
self.salary = salary
def display_info(self):
print(f"Employee ID: {self.employee_id}, Salary: ${self.salary}")
self.department = department
def display_info(self):
Person.display_info(self)
Employee.display_info(self)
print(f"Department: {self.department}")
# Example usage:
manager_instance.display_info()