python programming list1[1]
python programming list1[1]
12
123
1234
print()
2)Write a program to create class EMPLOYEE with ID and NAME and display
its contents.
class EMPLOYEE:
self.emp_id = emp_id
self.name = name
def display(self):
emp1.display()
3) Write a program to create dictionary of students that includes their ROLL
NO. and NAME.
students = {}
students[1] = "Neha"
students[2] = "Aarya"
students[3] = "Rohan"
print(students)
students[2] = "Shreyas"
print(students)
del students[1]
print(students)
4) Explain decision making statements If- else, if- elif- else with example.
age = 18
else:
………………………
marks = 75
print("Grade: A")
print("Grade: B")
print("Grade: C")
else:
print("Grade: D")
iii) >>> t1 = [ 3, 5, 6, 7]
>>> print (t 1 [2])
>>> print (t 1 [–1])
>>> print (t 1 [2 :])
>>> print (t 1 [:])
6
7
[6, 7]
[3, 5, 6, 7]
import numpy as np
addition = A + B
subtraction = A - B
print("Addition:\n", addition)
print("Subtraction:\n", subtraction)
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
try:
print(100/num)
except:
9) Write a program to open a file in write mode and append some contents at
the end of file.
file.write("original content.\n")
file.close()
file.write("appended content.\n")
file.close()
print(file.read())
file.close()
1)>>>indices[:4]
['zero', 'one', 'two', 'three']
2)>>>indices[:-2]
['zero', 'one', 'two', 'three']
fact = 1
fact = fact * i
12)Write a python program to input any two tuples and interchange the tuple
variables.
t1, t2 = t2, t1
print("After swapping:")
file.close()
file.close()
lines = file.readlines()
print(line.strip())
file.close()
self.name = name
def display(self):
print("Name:", self.name)
person1.display()
person2.display()
class AgeException(Exception):
self.message = message
super().__init__(self.message)
def check_age(age):
print("Age is valid.")
try:
check_age(age)
except AgeException as e:
print("Error:", e)
def is_palindrome(s):
return s == s[::-1]
if is_palindrome(string):
else:
17) Write a Python program to calculate sum of digit of given number using
function.
def sum_list(items):
total = 0
for i in items:
total = total+i
return total
my_list=[2,3,4,5,6]
result=sum_list(my_list)
18) Write a Python Program to accept values from user in a list and find the
largest number and smallest number in a list
user_list = []
for i in range(n):
user_list.append(element)
largest = max(user_list)
smallest = min(user_list)
class Student:
self.name = name
self.roll_no = roll_no
self.department = department
self.mobile_no = mobile_no
def display(self):
print("Student Information:")
print("Name:", self.name)
print("Department:", self.department)
student1.display()
class Animal:
def speak(self):
class Dog(Animal):
def speak(self):
print(f"{self.name} barks")
dog = Dog("Buddy")
animal.speak()
dog.speak()
2
4 6 8
10 12 14 16 18
print()
print()
22)Write python program using module, show how to write and use module
by importing it.
def get_tuple():
return T
def list_from_tuple():
return list(T)
main_program.py:
# Importing the module
import my_module
T = my_module.get_tuple()
# Displaying outputs
print(T[2]) # 'SPAM!'
print(T[-2]) # 'SPAM!'
23) Write a Python program to create user defined exception that will check
whether the password is correct or not.
# User-defined exception
class PasswordError(Exception):
pass
def check_password(password):
correct_password = "mypassword123"
if password != correct_password:
else:
print("Password is correct!")
try:
check_password(password)
except PasswordError as e:
print(e)
class Animal:
def sound(self):
class Dog(Animal):
def sound(self):
print("Dog barks.")
class Calculator:
return a + b + c
animal = Animal()
dog = Dog()
calc = Calculator()
print(calc.add(5)) #5
print(calc.add(5, 10)) # 15
print(calc.add(5, 10, 15)) # 30
25)WAP to read contents of first.txt file and write same content in second.txt
file.
# Open the first file in read mode and second file in write mode
26)Write a program for seek ( ) and tell ( ) function for file pointer
manipulation in python .
# Move the file pointer to the 5th byte from the start
file.seek(5)
content = file.read()
27)Write a program to create class student with Roll no. and Name and
display its contents.
class Student:
self.roll_no = roll_no
self.name = name
def display(self):
student1.display()
# Creating a set
my_set = {1, 2, 3, 4, 5}
# 1. Add an element
my_set.add(6)
# 2. Remove an element
my_set.remove(4)
new_set = my_set.copy()
a = 10 # 1010 in binary
b = 4 # 0100 in binary
# Bitwise AND
print(a & b)
# Bitwise OR
print(a | b)
# Bitwise XOR
print(a ^ b)
# Bitwise NOT
print(~a)
# Bitwise Left Shift
print(a << 1)
print(a >> 1)
if num > 0:
print("Positive number")
print("Negative number")
else:
print("Zero")
my_list = [1, 2, 3, 4, 5]
# 1. Append an element
my_list.append(6)
# 3. Remove an element
my_list.remove(4)
my_list.pop()
print(my_list)
for i in range(4)]
filename = "file.txt"
text = file.read()
char_count = {}
if char.isalpha():
char_count[char] = char_count.get(char, 0) + 1
print(char_count)
34)Design a class student with data members; Name, roll number address
Create suitable method for reading and printing students details.
class Student:
self.name = name
self.roll_no = roll_no
self.address = address
def display(self):
student.display()
35)Create a parent class named Animals and a child class Herbivorous which
will extend the class Animal. In the child class Herbivorous over side the
method feed ( ). Create a object.
class Animal:
def feed(self):
print("Animal is eating.")
class Herbivorous(Animal):
def feed(self):
herbivore = Herbivorous()
herbivore.feed()
1010101
10101
101
1
for j in range(i):
print()
print(my_set)
my_set.add(6)
my_set.remove(3)
print(my_set)
Google 1
Facebook 2
Microsoft 2
GFG 1
Youtube 3
39) Write a python program that takes a number and checks whether it is a
palindrome.
if number == number[::-1]:
print(f"{number} is a palindrome.")
else:
40) Write a python program to create a user defined module that will ask
your program name and display the name of the program.
def get_program_name():
return "My Python Program"
import program_name
print("Program name:", program_name.get_program_name())
41)Write a python program takes in a number and find the sum of digits in a
number.
42)Write a program function that accepts a string and calculate the number of
uppercase letters and lower case letters.
def count_cases(s):
43)Write a program function that accepts a string and calculate the number of
uppercase letters and lower case letters
import numpy as np
print(random_numbers)
class Diploma:
def getdiploma(self):
class CO(Diploma):
def getdiploma(self):
def getdiploma(self):
diploma = Diploma()
co = CO()
if_ = IF()
diploma.getdiploma()
co.getdiploma()
if_.getdiploma()
46) Write a Python program to create user defined exception that will check
whether the password is correct or not.
# User-defined exception
class PasswordError(Exception):
pass
def check_password(password):
correct_password = "mypassword123"
if password != correct_password:
else:
print("Password is correct!")
try:
check_password(password)
except PasswordError as e:
print(e)
47) Write a Python program to check for zero division errors exception.
try:
result = 10 / num
except ZeroDivisionError:
48)Design a class student with data members; Name, Roll No., Address. Create
suitable method for reading and printing students detail
class Student:
def __init__(self, name, roll_no, address):
self.name = name
self.roll_no = roll_no
self.address = address
def display(self):
student.display()
try:
result = 10 / x
except ZeroDivisionError:
else:
print(f"Result: {result}")
finally:
print("Execution finished.")
50)Write a Python program to create a class ‘Diploma’ having a method ‘getdiploma’ that
prints “I got a diploma”. It has two subclasses namely ‘CO’ and ‘IF’ each having a method
with the same name that prints “I am with CO diploma” and ‘I am with IF diploma’
respectively. Call the method by creating an object of each of the three classes
class Diploma:
def getdiploma(self):
class CO(Diploma):
def getdiploma(self):
class IF(Diploma):
def getdiploma(self):
diploma = Diploma()
co = CO()
if_ = IF()
diploma.getdiploma()
co.getdiploma()
if_.getdiploma()