Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Solution of Python Program - Ipynb - Colab

Notes

Uploaded by

dharanidn1312
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Solution of Python Program - Ipynb - Colab

Notes

Uploaded by

dharanidn1312
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Write the output for the following snippet code: mysub=” Computer Science”

mysub= "Computer Science"


print(mysub[0:len(mysub)])
print(mysub[-7:-1])
print(mysub[::-2])
print(mysub[:3]+mysub[3:])
print(mysub.isalpha())
print(mysub[2:7], "and", mysub[-4:-1])

Computer Science
Scienc
eniSrtpo
Computer Science
False
mpute and enc

2. Which string method is used to implement the following:

t= "DataAnalytics with Python"


for ch in t[2:7]:
print("welcome")

welcome
welcome
welcome
welcome
welcome

3. What will be the Output for following statements

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])

[10, 26, 32]


[12, 32, 62, 26, 80, 10]
[10, 20, 30, 40, [50, 60]]
[10, 20, 30, 40, [50, 60], 80, 90]
[20, 40, 80]
1
3
5
7
10

4. Predict the output:

t=tuple()
t=t + ("python",)
print(t)
t1=(10,20,30)
print(len(t1))

('python',)
3

5. What will be the output for the following code:


keys={'A','B','C','D'}
value=[10]
dict1=dict.fromkeys(keys,value)
print(dict1)
value.append(3)
print(dict1)
print()
A={10:100,20:200,30:300,40:400,50:500}
print(A.items())
print (A.keys())
print (A.values())

{'B': [10], 'C': [10], 'D': [10], 'A': [10]}


{'B': [10, 3], 'C': [10, 3], 'D': [10, 3], 'A': [10, 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])

6. What will be the output for the following code:

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"))

Mon Aug 5 08:38:31 2024


Mon
Aug
August
20
38
31

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")

Enter the year: 2028


2028 is 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")

enter the total number of students2


student 0
enter the rollno11
enter the namebh
enter the percentage85
student 1
enter the rollno12
enter the namesn
enter the percentage86
enter the rollno to search12
(12, 'sn', 86)

11.Python script to create a class to implement pow(x, n) by taking user input.

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)

ENTER X(BASE) VALUE:12


ENTER N(POWER) VALUE:2
power of given literals:
x: 12.0
n: 2.0 is: 144.0

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)

name = input("Enter the name of Student:")


s1 = students(name,[])
s1.enterMarks()
s1.display()
print(s1.average())

name = input("Enter the name of Student:")


s2 = students(name,[])
s2.enterMarks()
s2.display()
s2.average()

Enter the name of Student:bh


Enter the marks of subject2
Enter the marks of subject10
Enter the marks of subject20
bh got [2, 10, 20]
10.666666666666666
Enter the name of Student:sn
Enter the marks of subject50
Enter the marks of subject20
Enter the marks of subject10
sn got [50, 20, 10]
26.666666666666668

You might also like