Solution of Python Program - Ipynb - Colab
Solution of Python Program - Ipynb - Colab
Write the output for the following snippet code: mysub=” Computer Science”
Computer Science
Scienc
eniSrtpo
Computer Science
False
mpute and enc
welcome
welcome
welcome
welcome
welcome
List1=[12,32,62,26,80,10]
print(List1[::-2])
print(List1[:3]+List1[3:])
myList=[10,20,30,40]
myList.append([50,60])
print(myList)
myList.extend([80,90])
print(myList)
del myList[::2]
print(myList)
myList=[1,2,3,4,5,6,7,8,10]
for i in range(0,len(myList)):
if i %2==0:
print(myList[i])
t=tuple()
t=t + ("python",)
print(t)
t1=(10,20,30)
print(len(t1))
('python',)
3
dict_items([(10, 100), (20, 200), (30, 300), (40, 400), (50, 500)])
dict_keys([10, 20, 30, 40, 50])
dict_values([100, 200, 300, 400, 500])
class test:
def __init__(self,a):
self.a=a
def display(self):
print(self.a)
obj=test("bh")
obj.display()
bh
7.Write a python script to print the current date in following format “Sun May 29 02:26:23 IST 2017”
import datetime
x = datetime.datetime.now()
print(x.strftime("%c"))
print(x.strftime("%a"))
print(x.strftime("%b"))
print(x.strftime("%B"))
print(x.strftime("%C"))
8. Write a python script to check if a given year is leap year using function
def leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
n=int(input("Enter the year: "))
if leap_year(n):
print(f"{n} is a leap year")
else:
print(f"{n} is not a leap year")
9.Python script to count the number of times a character appears in a given string using dictionary.
st=input("enter a string")
dict={}
for ch in st:
if ch in dict:
dict[ch]+=1
else:
dict[ch]=1
print(dict)
for key in dict:
print({key:dict[key]})
enter a stringbhbha
{'b': 2, 'h': 2, 'a': 1}
{'b': 2}
{'h': 2}
{'a': 1}
10.write a python program to store roll no, name and percentage of 5 students in a tuple and display the name of the student whose roll is
entered by the user.
list=[]
n=int(input("enter the total number of students"))
for i in range(n):
print("student",i)
rollno=int(input("enter the rollno"))
name=input("enter the name")
percent=int(input("enter the percentage"))
tup=(rollno,name,percent)
list.append(tup)
rn=int(input("enter the rollno to search"))
for i in list:
if i[0]==rn:
print(i)
break
else:
print("not found")
class py_power:
def power(x,n):
print("power of given literals:\nx:",x,"\nn:",n,"is:",x**n)
x=float(input("ENTER X(BASE) VALUE:"))
n=float(input("ENTER N(POWER) VALUE:"))
py_power.power(x,n)
12. Write a Python program to create a complex class. Include attributes like real and img and add two complex numbers.
class complex:
def __init__(self,real,img):
self.real=real
self.img=img
def add(self,number):
self.real=self.real+number.real
self.img=self.img+number.img
result=complex(self.real,self.img)
return result
n1=complex(5,6)
n2=complex(-6,7)
result=n1.add(n2)
print(result.real)
print(result.img)
-1
13
13. Write a Python program to create a student class. Include attributes like student_name, roll_no and marks. Implement a method to
determine the average of n student.
class students:
def __init__(self, name,marks):
self.name = name
self.marks =[]
def enterMarks(self):
for i in range(3):
m = int(input("Enter the marks of subject"))
self.marks.append(m)
def display(self):
print (self.name, "got ", self.marks)
def average(self):
return sum(self.marks)/len(self.marks)