Experiments

Experiment 1: Swap two numbers and check positive or negative

        
    def swap(a,b):
        temp = b
        b = a
        a = temp
        return a,b
    
    
    def numchecker(num):
        if num>0:
            return 1
        elif num<0:
            return -1
        else:
            return 0
    
    if __name__=='__main__':
        num1 = int(input("Enter number 1: "))
        num2 = int(input("Enter number 2: "))
        num1_check = numchecker(num1)
        num2_check = numchecker(num2)
        print()
        print(f"Value of Number 1 before swapping is {num1}")
        print(f"Value of Number 2 before swapping is {num2}")
        print()
        num1,num2 = swap(num1,num2)
        print(f"Value of Number 1 after swapping is {num1}")
        print(f"Value of Number 2 after swapping is {num2}")
        print()
        if num1_check == 1:
            print(f"Number 1 is Positive")
        elif num1_check == -1:
            print("Number 1 is Negative")
        else:
            print("Number 1 is zero")
    
    

Experiment 2: To check palindrome number and string and find factorial of a number

    
    def number_palindrome(num):
    if 0 >= num <= 9:
        print(f"Its a palindrome number")
    temp = num
    rev = 0
    while num > 0:
        dig = num % 10
        rev = rev * 10 + dig
        num = num // 10
    if temp == rev:
        print("The number is a palindrome!")
    else:
        print("The number isn't a palindrome!")


def string_palindrome():
    word = input("Enter any word: ")
    temp = word
    temp = word[::-1]
    if word == temp:
        print("The string is palindrome")
    else:
        print("The string isn't a palindrome")


def factorial(num):
    f = 1
    if num < 0:
        print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
        print("The factorial of 0 is 1")
    else:
        for i in range(1, num + 1):
            f = f * i
        print("The factorial of", num, "is", f)


while True:
    print()
    print("1.Palindrome number or not")
    print("2.Palindrome String or not")
    print("3.Factorial of the number")
    print("4.Stop")
    choice = int(input("Enter your choice:"))
    num = None

    if choice == 1:
        num = int(input("Enter any number: "))
        number_palindrome(num)
    if choice == 2:
        string_palindrome()
    if choice == 3:
        num = int(input("Enter any number: "))
        factorial(num)
    if choice == 4:
        exit()

  

Experiment 3: Use of list in python

        
        

def a():
    even_list = []
    odd_list = []
    main_list = []
    no_of_item = int(input("Enter number of items in the main list: "))

    for i in range(no_of_item):
        no = int(input())
        main_list.append(no)

    for i in main_list:
        if i % 2 == 0:
            even_list.append(i)
        else:
            odd_list.append(i)

    print("Even no list: ",even_list)
    print("Odd no list: ",odd_list)


def b():
    list1 = [1,20,0,-21,4]
    list2 = [2.5,11,55,21]

    print("List 1: ",list1)
    print("List 2: ",list2)
    list3 = list1 + list2
    print("After merging list 1 and list 2: ",list3)
    list3.sort(reverse=False)
    print("After sorting: ",list3)

def c():
    main_list = [10,20,21,25,89,100,110]
    print("Main list: ",main_list)
    main_list.insert(0,101)
    print("After updating the first element with 101 value:",main_list)
    middle_element = int(len(main_list)/2)
    main_list.pop(middle_element)
    print("After deleting the middle element from the list:",main_list)


def d():
    list1 = [1,11,2,22,3,33,4,44,100]
    print("List 1:",list1)
    print("Max element from the list 1:",max(list1))
    print("Min element from the list 1:",min(list1))


def e():
    list1 = [10,20,30,50,60]
    print("List 1:",list1)
    print("Enter any three words in the list:")
    for i in range(3):
        word = input().lower()
        list1.append(word)
    print("After adding three words in the list:", list1)
    if "python" in list1:
        index = list1.index("python")
        print(f"Word 'python' found at {index}")
    else:
        print("Word 'python' not found")

a()
print()
b()
print()
c()
print()
d()
print()
e()

    

Experiment 4: demonstrate Use of tuples in python

def add_student(records,no_of_student):
    for i in range(no_of_student):
        roll_number = input("Enter student roll number:")
        name = input("Enter student name:")
        marks = []
        for i in range(3):
            marks.append(int(input(f"Enter marks for subject {i+1}:")))
        records.append((roll_number,name,tuple(marks)))
        print("Student record added successfully")

def show_students(records):
    print("\nStudent records:")
    for roll_number,name,marks in records:
        print(f"Roll number: {roll_number}")
        print(f"Name: {name}")
        print(f"Marks: {marks}")
        print("-"*20)


def display_student_by_name(records):
    search_name = input("Enter student name to be searched: ")
    print(f"\nDisplaying student roll number and marks for students with name: {search_name}")

    name_found = False  # Add a flag to check if the name is found

    for roll_number, name, marks in records:
        if name == search_name:
            print(f"Roll number: {roll_number}")
            print(f"Marks: {marks}")
            print("-" * 20)
            name_found = True  # Set the flag to True if the name is found

    if not name_found:
        print("No name found")


def sort_records_by_name(records):
    sorted_records = sorted(records, key=lambda x:x[1])
    print("\nSorted student records by name:")
    for roll_number, name , marks in sorted_records:
        print(f"Roll number: {roll_number}")
        print(f"Name: {name}")
        print(f"Marks: {marks}")
        print("-"*20)


student_records = []
while True:
    print("\n1.Add student record.")
    print("2.Show all students record.")
    print("3.Display students marks and roll no by name.")
    print("4.Sort records by name.")
    print("5.Exit")
    choice = int(input("Enter your choice:"))
    if choice == 1:
        no_of_students = int(input("Enter no of students:"))
        add_student(student_records,no_of_students)
    elif choice == 2:
        show_students(student_records)
    elif choice == 3:
        display_student_by_name(student_records)
    elif choice == 4:
        sort_records_by_name(student_records)
    elif choice == 5:
        print("Exiting program")
        exit()
    else:
        print("Invalid choice, please select correct option")
        
    

Experiment 5: demonstrate single and multiple inheritance in python

Person and student is used to demonstrate single inheritance and bird, mammal, bat are used for multiple inheritance

        
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        return f"Name: {self.name}, Age: {self.age}"

class Student(Person):
    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self.grade = grade

    def display_info(self):
        return super().display_info() + f", Grade: {self.grade}"

name = input("Enter the person's name: ")
age = input("Enter the person's age: ")
grade = input("Enter the student's grade: ")

student = Student(name, age, grade)
print("Person Information:", student.display_info())

class Bird:
    def __init__(self, name):
        self.name = name

    def fly(self):
        return "I can fly"

class Mammal:
    def __init__(self, name):
        self.name = name

    def run(self):
        return "I can run"

class Bat(Bird, Mammal):
    def __init__(self, name):
        super().__init__(name)

name = input("Enter the name of the bat: ")
bat = Bat(name)
print(bat.fly())
print(bat.run())

    

Comments

Popular posts from this blog

Machine Learning

Blockchain Experiments

java exp