Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Tuesday, April 16, 2024

April 16, 2024

Write a program to create a file in write mode and add some contents into it and display the output.

Code:

def main():

    # Open a file in write mode

    file_path = "output.txt"

    with open(file_path, "w") as file:

        # Write some content to the file

        file.write("Hello, this is some content written to the file.\n")

        file.write("Adding more lines for demonstration.\n")

        file.write("Python is a powerful language for file handling.\n")


    # Read and display the contents of the file

    with open(file_path, "r") as file:

        contents = file.read()

        print("Contents of the file:")

        print(contents)


# Call the main function directly

main()


Output:




April 16, 2024

Program to illustrate the concept of method overriding in python.

Code: 

# Python program to demonstrate  

# method overriding 

  

  

# Defining parent class 

class Parent(): 

      

    # Constructor 

    def __init__(self): 

        self.value = "Inside Parent"

          

    # Parent's show method 

    def show(self): 

        print(self.value) 

          

# Defining child class 

class Child(Parent): 

      

    # Constructor 

    def __init__(self): 

        self.value = "Inside Child"

          

    # Child's show method 

    def show(self): 

        print(self.value) 

          

          

# Driver's code 

obj1 = Parent() 



obj2 = Child() 

  

obj1.show() 

obj2.show()


Output:





April 16, 2024

Program to illustrate the concept of inheritance in python.

 Code:


class Person(object):

  

  # Constructor

  def __init__(self, name, id):

    self.name = name

    self.id = id


  # To check if this person is an employee

  def Display(self):

    print(self.name, self.id)


# Driver code

emp = Person("Krishna", 999) # An Object of Person

emp.Display()


Output:



April 16, 2024

Program to illustrate the concept of class and objects in python.

 Code:

class Dog:

    # Class variable

    species = "BULL DOG"


    # Constructor or initializer method

    def __init__(self, name, age):

        # Instance variables

        self.name = name

        self.age = age


    # Instance method

    def bark(self):

        return "Woof!"


# Creating objects (instances) of the Dog class

dog1 = Dog(name="Buddy", age=2)

dog2 = Dog(name="Max", age=3)


# Accessing attributes and calling methods of the objects

print(f"{dog1.name} is {dog1.age} years old.")

print(f"{dog2.name} is {dog2.age} years old.")


print(f"{dog1.name} says: {dog1.bark()}")

print(f"{dog2.name} says: {dog2.bark()}")


# Accessing class variable

print(f"They both belong to the species {Dog.species}.")


Output:






April 16, 2024

Program in python to generate first n Fibonacci terms using recursive function.

 Code:

def fibonacci_recursive(n):

    # Base case: return 0 for the first term

    if n == 0:

        return 0

    # Base case: return 1 for the second term

    elif n == 1:

        return 1

    else:

        # Recursive case: F(n) = F(n-1) + F(n-2)

        return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

# Example usage

try:

    n = int(input("Enter the number of Fibonacci terms to generate: "))

    if n < 0:

        raise ValueError("Please enter a non-negative integer.")

    fibonacci_sequence = [fibonacci_recursive(i) for i in range(n)]

    print(f"The first {n} Fibonacci terms are: {fibonacci_sequence}")

except ValueError as e:

    print(f"Error: {e}")


Output:





April 16, 2024

Program to demonstrate different argument (positional, default, keyword) pass to function.

 Code :

def example_function(positional_arg, default_arg="default_value", *args, **kwargs):

    

    #Function to demonstrate different types of arguments.

    print("Positional Argument:", positional_arg)

    print("Default Argument:", default_arg)

    print("Additional Positional Arguments (*args):", args)

    print("Additional Keyword Arguments (**kwargs):", kwargs)

    print()


# Example usage

example_function("Positional Argument")

example_function("Positional Argument", "Custom Default Value")

example_function("Positional Argument", "Custom Default Value", 1, 2, 3)

example_function("Positional Argument", custom_kwarg1="Value1", custom_kwarg2="Value2")


Output:



April 16, 2024

Code:

def find_max_min(numbers):

    # Check if the list is not empty

    if len(numbers) == 0:

        return None, None

    # Find the maximum and minimum numbers

    maximum = max(numbers)

    minimum = min(numbers)

    return maximum, minimum

# Example usage

user_input = input("Enter a list of numbers separated by spaces: ")

# Convert the input string to a list of floats

numbers_list = [float(x) for x in user_input.split()]

# Find the maximum and minimum numbers and display the result

max_num, min_num = find_max_min(numbers_list)

if max_num is not None and min_num is not None:

    print(f"The maximum number in the list is: {max_num}")

    print(f"The minimum number in the list is: {min_num}")

else:

    print("The list is empty.")


Output: