ITW File
ITW File
2. Assignment 2 6-9
(Sting, List, Tuple and Dictionary)
(1) Write a python program to check whether or not a
given string is a palindrome.
(2) Create a list and perform the following methods in
python.
insert() (2) remove() (3) append() (4) len() (5) pop()
(6)clear()
(3) Create a tuple and perform the following methods in
python.
Add items (2) len() (3) check for item in tuple
(4)Access items
(4) Create a dictionary and apply the following methods
in python.
Print the dictionary items (2) access items (3) change
values use len()
3. Assignment 3 9-13
(Function and Module)
4. Assignment 4 13-17
5. Assignment 5
6. Assignment 6
7. Assignment 7
Indian Institute of Information Technology Bhopal
Submitted by Submitted to
Yash Shukla Dr. Amit Gupta
23U03114 Assistant Professor
Assignment 1 (Control Statements and Loops)
Code:
def check_character(char):
vowels = 'aeiouAEIOU'
if char in vowels:
return f"{char} is a vowel."
else:
return f"{char} is a consonant."
Output:
2. Write a python program to check whether a given character is a digit or not. If the given
character is a digit, check whether or not it is less than 5.
Code:
def check_digit(char):
if char.isdigit(): # Check if the character is a digit
if int(char) < 5:
return f"{char} is a digit and it is less than 5."
else:
return f"{char} is a digit but it is not less than 5."
else:
return f"{char} is not a digit."
3. Write a python program to print all the natural numbers from 1 to n in the reverse order.
Code:
def print_natural_numbers_reverse(n):
for i in range(n, 0, -1):
print(i, end=" ")
Output:
4. Write a python program to check whether a given year is a leap year or not.
Code:
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
if is_leap_year(year):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
Output:
5. Write a python program to print the Fibonacci Series up to the number entered by the user.
Code:
def fibonacci_series(n):
a, b = 0, 1
while a <= n:
print(a, end=" ")
a, b = b, a + b
Output:
Assignment 2 (Strings, List, Tupple and Dictionary)
Code:
def is_palindrome(s):
s = s.lower().replace(' ', '')
return s == s[::-1]
Output:
Code:
my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 10)
print(my_list)
my_list.remove(3)
print(my_list)
my_list.append(6)
print(my_list)
length = len(my_list)
print(length)
last_element = my_list.pop()
print(my_list)
print(last_element)
my_list.clear()
print(my_list)
Output:
Code:
my_tuple = (1, 2, 3, 4, 5)
print("Original Tuple:", my_tuple)
temp_list = list(my_tuple)
temp_list.append(6)
my_tuple = tuple(temp_list)
print("Tuple after adding item:", my_tuple)
length = len(my_tuple)
print("Length of the tuple:", length)
item_to_check = 3
if item_to_check in my_tuple:
print(f"{item_to_check} is in the tuple.")
else:
print(f"{item_to_check} is not in the tuple.")
Output:
Code:
my_dict = {"name": "Alice", "age": 30, "city": "New York"}
print(my_dict)
name = my_dict["name"]
age = my_dict["age"]
city = my_dict["city"]
my_dict["age"] = 31
my_dict["city"] = "Los Angeles"
print(my_dict)
length = len(my_dict)
print(length)
Output:
Assignment 3 (Functions and Modules)
1. Write a program in python by taking a function called gcd that takes parameters a and b and
returns their greatest common divisor.
Code:
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
Output:
2. Write a python program to develop (i) Call by Value (ii) Call by Reference
Code:
# Call by Value
def add(x, y):
sum = x + y
return sum
a = 10
b = 20
c = add(a, b)
print("Sum:", c)
print("a:", a, "b:", b)
# Call by Reference
def swap(x, y):
temp = x
x = y
y = temp
a = 10
b = 20
swap(a, b)
print("a:", a, "b:", b)
Output:
Code:
def swap(x, y):
temp = x
x = y
y = temp
return x, y
print("After swapping:")
print("Number 1:", num1)
print("Number 2:", num2)
Output:
Code:
def binary_search(arr, x):
low = 0
high = len(arr) - 1
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
result = binary_search(arr, x)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
Output:
Code:
import math
def factorial(n):
if n < 0:
return None
elif n == 0:
return 1
else:
return math.factorial(n)
Output:
Assignment 4 (OOPs Concepts - Inheritance and Polymorphism)
Code:
class Parent:
self.name = name
def display_name(self):
self.age = age
def display_info(self):
self.display_name()
class Father:
self.father_name = father_name
def show_father(self):
class Mother:
self.mother_name = mother_name
def show_mother(self):
self.child_name = child_name
def display_family(self):
self.show_father()
self.show_mother()
print("Simple Inheritance:")
child_obj.display_info()
print("\nMultiple Inheritance:")
# Demonstration of Multiple Inheritance
multi_child.display_family()
Output:
Code:
class Animal:
self.name = name
def speak(self):
class Dog(Animal):
def speak(self):
print("Woof!")
class Cat(Animal):
def speak(self):
print("Meow!")
animal.speak()
class Vector:
self.x = x
self.y = y
def __str__(self):
v1 = Vector(2, 3)
v2 = Vector(4, 5)
v3 = v1 + v2
print(v3)
Output:
Assignment 5 (Multi-threading and Sending an email)
# Creating threads
thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_letters)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("Both threads have finished execution.")
Output:
2. Write a program to send an email using Python code.
Code:
import smtplib
from email.message import EmailMessage
Output:
Indian Institute of Information Technology Bhopal
Submitted by Submitted to
Yash Shukla Dr. Amit Gupta
23U03114 Assistant Professor
Assignment 6 (Classes, Constructor and Inheritance)
1. Write a Java Program to calculate Area of Rectangle using Classes.
Code
import java.util.Scanner;
class Rectangle {
double length;
double width;
double calculateArea() {
return length * width;
}
}
scanner.close();
}
}
Output
2. Write a Java Program to implement Constructors.
Code
class Book {
String title;
String author;
double price;
void displayInfo() {
System.out.println("Book Title: " + title);
System.out.println("Author: " + author);
System.out.println("Price: ₹" + price);
}
}
Output
3. Write Java program(s) on use of Inheritance.
Code
class Vehicle {
String brand;
int wheels;
void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Number of Wheels: " + wheels);
}
}
void displayCarInfo() {
displayInfo();
System.out.println("Number of Seats: " + seats);
}
}
Output
Assignment 7 ((Exception Handling, Interface and Applet)
1.Write Java program(s) which uses the Exception Handling features of the language,
creates exceptions and handles them properly, and uses the predefined exceptions.
Code
import java.util.Scanner;
try {
System.out.print("Enter a number: ");
int number = scanner.nextInt();
Output
2. Write a Java Program on implementing Interface.
Code
interface Animal {
void sound();
void habitat();
}
Output
3. Develop an Applet that receives an integer in one text field & computes its factorial
value & returns it in another text field when the button “Compute” is clicked.
Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public FactorialCalculator() {
inputField = new JTextField(10);
resultField = new JTextField(10);
resultField.setEditable(false);
computeButton = new JButton("Compute");
setLayout(new FlowLayout());
add(new JLabel("Enter a number:"));
add(inputField);
add(computeButton);
add(new JLabel("Factorial:"));
add(resultField);
computeButton.addActionListener(this);
setTitle("Factorial Calculator");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
int factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}
Output