0% found this document useful (0 votes)
46 views8 pages

Expected Problems

The document contains various Python code snippets demonstrating different programming concepts such as loops, classes, functions, and data structures. It includes examples of calculating areas and circumferences of circles, manipulating lists and sets, and performing operations like finding minimum and maximum values. Additionally, it showcases string manipulations, tuple assignments, and basic input/output operations.

Uploaded by

cc2800714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views8 pages

Expected Problems

The document contains various Python code snippets demonstrating different programming concepts such as loops, classes, functions, and data structures. It includes examples of calculating areas and circumferences of circles, manipulating lists and sets, and performing operations like finding minimum and maximum values. Additionally, it showcases string manipulations, tuple assignments, and basic input/output operations.

Uploaded by

cc2800714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Marks=[10,20,30,40,50] OUTPUT

i=0 Enter Radius: 5


sum=0 The Area = 78.5
while i<4: The Circumference = 31.400000000000002
sum+=Marks[i]
i+=1 import matplotlib.pyplot as plt
print(sum) a=[1,2,3]
OUTPUT b=[5,7,4]
100 x=[1,2,3]
y=[10,14,12]
class Sample: plt.plot(a,b,label='Label 1')
num=0 plt.plot(x,y,label='Label 2')
def __init__(self, var): plt.xlabel('X-Axis')
Sample.num+=1 plt.ylabel('Y-Axis')
self.var=var plt.legend()
print("The object value is = ", var) plt.show()
print("The count of object created = ", OUTPUT
Sample.num)
S1=Sample(15)
S2=Sample(35)
S3=Sample(45)

OUTPUT
The object value is = 15
The count of object created = 1 import matplotlib.pyplot as plt
The object value is = 35 plt.plot([1,2,3,4], [1,4,9,16])
The count of object created = 2 plt.show()
The object value is = 45 OUTPUT
The count of object created = 3

Mydict={chr(x):x for x in range(97,102)}


print(Mydict)
OUTPUT
{'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101}

A={x*3 for x in range(1,6)}


B={y**2 for y in range(1,10,2)} str1="THOLKAPPIYAM"
print("A=",A) print(str1[4:])
print("B=",B) print(str1[4::2])
print("A|B=",A|B) print(str1[::3])
print("A-B=",A-B) print(str1[::-3])
print("A&B=",A&B) OUTPUT
print("A^B=",A^B) KAPPIYAM KPIA
OUTPUT TLPY MIAO
A= {3, 6, 9, 12, 15}
B= {1, 9, 81, 49, 25} str1 = "welcome"
A|B= {1, 3, 6, 9, 12, 15, 81, 49, 25} str2 = "to school"
A-B= {3, 12, 6, 15} str3=str1[:2]+str2[len(str2)-2:]
A&B= {9} print(str3)
A^B= {1, 3, 6, 12, 15, 81, 25, 49} OUTPUT
weol
class Circle:
pi=3.14 Num = []
def __init__(self,radius): for x in range(1,11):
self.radius=radius Num.append(x)
def area(self): print("The list of numbers from 1 to 10 = ", Num)
return Circle.pi*(self.radius**2) for index, i in enumerate(Num):
def circumference(self): if(i%2==0):
return 2*Circle.pi*self.radius del Num[index]
r=int(input("Enter Radius: ")) print("The list after deleting even numbers = ", Num)
C=Circle(r) OUTPUT
print("The Area =",C.area()) The list of numbers from 1 to 10 = [1, 2, 3, 4, 5, 6, 7,
print("The Circumference =", C.circumference()) 8, 9, 10]
The list after deleting even numbers = [1, 3, 5, 7, 9]
# String within triple quotes to display a string mytuple=tuple([x**2 for x in range(2,11,2)])
with single quote print(mytuple)
>>> print ('''They said, "What's there?"''') print(mytuple[2:3])
They said, "What's there?" print(mytuple[3:])
print(mytuple[:])
# String within single quotes to display a string
OUTPUT
with single quote using escape sequence
(4, 16, 36, 64, 100)
>>> print ('They said, "What\'s there?"')
They said, "What's there?" (36,)
(64, 100)
# String within double quotes to display a string (4, 16, 36, 64, 100)
with single quote using escape sequence
>>> print ("They said, \"What's there?\"") Write a program to swap two values using tuple
He said, "What's there?" assignment
a = int(input("Enter value of A: "))
alpha=list(range(65,70)) b = int(input("Enter value of B: "))
for x in alpha: print("Value of A = ", a, "\n Value of B = ", b)
print(chr(x),end='\t') (a, b) = (b, a)
OUTPUT print("Value of A = ", a, "\n Value of B = ", b)
A B C D E OUTPUT
Enter value of A: 45
i=65 Enter value of B: 66
while(i<=70): Value of A = 45
for j in range(65,i): Value of B = 66
print(chr(j),end="\t") Value of A = 66
print(end='\n') Value of B = 45
i=i+1
or Tup1 = (12, 78, 91, "Tamil", "Telugu", 3.14, 69.48)
a=['A','B','C','D','E'] print(Tup1)
for i in range(0,6): print(Tup1[2:5])
for j in range(0,i): print(Tup1[:5])
print(a[j],end=" ") print(Tup1[4:])
else: print(Tup1[:])
print() OUTPUT
OUTPUT (12, 78, 91, 'Tamil', 'Telugu', 3.14, 69.48)
A (91, 'Tamil', 'Telugu')
A B (12, 78, 91, 'Tamil', 'Telugu')
A B C ('Telugu', 3.14, 69.48)
A B C D (12, 78, 91, 'Tamil', 'Telugu', 3.14, 69.48)
A B C D E
Write a program to check if a number is Positive,
a, b = 30, 20 Negative or zero.
min = a if a < b else b num = float(input("Enter a number: "))
print ("The Minimum of A and B is ",min) if num > 0:
OUTPUT print("Positive number")
The Minimum of A and B is 20 elif num == 0:
print("Zero")
a, b = 30, 20 else:
max = a if a > b else b print("Negative number")
print ("The Maximum of A and B is ",max)
OUTPUT i=1
The Maximum of A and B is 30 while True:
if i%3 ==0:
squares = [ ] break
for x in range(1,11): print(i,end='')
s = x ** 2 i +=1
squares.append(s) OUTPUT
print (squares) 12
OUTPUT
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] str1="TamilNadu"
print(str1[::-1])
squares = [ x ** 2 for x in range(1,11) ] OUTPUT
print (squares) udaNlimaT
OUTPUT
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] list = [1, 2, 4, 5]
list.insert (2, 3)
print (list)
OUTPUT str1="COMPUTER"
[1, 2, 3, 4, 5] index=7
for i in str1:
Write a program to print the following pattern print (str1[:index+1])
str1=' * ' index-=1
i=1 OUTPUT
while i<=5: COMPUTER
print(str1*i) COMPUTE
i+=1 COMPUT
OUTPUT COMPU
* COMP
* * COM
* * * CO
* * * * C
* * * * *
Write a program to display Fibonacci series 0 1 1 2 3
for i in range (5, 0, -1): 4 5…… (upto n terms) [pg.86]
for j in range (1, i + 1): a, b, c = 0, 1, 0
n = int(input("Enter number of terms"))
print (' * ', end = '')
print(a, end= ' ')
print (end = '\n') print(b, end= ' ')
i+=1 for i in range (3, n+1):
or c = a+b
str1=' * ' print (c, end = ' ')
i=5 a=b
while i>=1: b=c
print(str1*i)
Write a program to display sum of natural numbers,
i-=1 upto n. [pg.86]
OUTPUT s=0
* * * * * n = int (input("Enter number of terms"))
* * * * for i in range (1, n + 1):
* * * s=s+i
* * print ("sum = ", s)
* Write a program to check if the given number is a
palindrome or not. [pg.86]
Write a Python program to display the given n = int (input("Enter the number"))
pattern s, d, x = 0, 0, n
str1="COMPUTER" while (n!= 0):
index=0 d = n%10
s = (s* 10)+d
for i in str1:
n = n//10
print (str1[:index+1]) if s == x:
index+=1 print ("The given number is palindrome")
OUTPUT else:
C print ("The given number is not palindrome")
CO
COM
COMP str1="COMPUTER"
COMPU print(str1*3)
COMPUT OUTPUT
COMPUTE COMPUTERCOMPUTERCOMPUTER
COMPUTER
T=1
str="COMPUTER" while T:
index=9 print(True)
for i in str: break
print(str[:index-1]) OUTPUT
index-=1 True
OUTPUT
COMPUTER list=[2**X for X in range(5)]
COMPUTE print(list)
COMPUT OUTPUT
COMPU [1, 2, 4, 8, 16]
COMP
COM
CO
C
list=[2**X for X in range(1,5)] setA={3,6,9}
print(list) setB={1,3,9}
OUTPUT print(setA|setB)
[2, 4, 8, 16] print(setA&setB)
print(setA-setB)
List=[10,20,30,40,50] print(setA^setB)
List[2]=35 OUTPUT
print(List) {1, 3, 6, 9}
OUTPUT {9, 3}
[10, 20, 35, 40, 50] {6}
{1, 6}
List=[17,23,41,10]
List.append(32) set_A={'a',2,4,'d'}
print(List) set_B={'a','b','c','d'}
OUTPUT print(set_A&set_B)
[17, 23, 41, 10, 32] OUTPUT
{'a', 'd'}
list1=[2,4,6,8,10]
print(list1[-2]) num1=int (input("Number 1: "))
OUTPUT num2=int (input("Number 2: "))
8 print ("The sum of { } and { } is { }".format(num1,
num2,(num1+num2)))
list=[2**X for X in range(1,10,2)] OUTPUT
print(list) Number 1: 34
Number 2: 54
OUTPUT
The sum of 34 and 54 is 88
[2, 8, 32, 128, 512]
def fact(n):
for i in range (5,15,3):
if n==0:
print(i)
return 1
OUTPUT
else:
[5,8,11,14]
return n*fact(n-1)
print(fact(5))
MyTup4 = (10)
OUTPUT
print(type(MyTup4))
120
OUTPUT
<class 'int'>
class Findsum:
MyTup5 = (10,) def __init__(self,num1,num2):
print(type(MyTup5)) self.__n1=num1
OUTPUT self.__n2 = num2
<class 'tuple'> def sum(self):
return self.__n1+self.__n2
divBy4=[ ] n1=int(input("Enter the first number:"))
for i in range(21): n2=int(input("Enter the second number:"))
if (i%4==0): obj1=Findsum(n1,n2)
divBy4.append(i) print("The sum of ", n1,"and",n2,"is",obj1.sum())
print(divBy4) OUTPUT
OUTPUT Enter the first number:200
[0, 4, 8, 12, 16, 20] Enter the second number:300
The sum of 200 and 300 is 500
Dict = { x : 2 * x for x in range(1,10)}
print(Dict) class Sample:
def __init__(self, num):
OUTPUT
print("Constructor of class Sample...")
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18} self.num=num
print("The value is :", num)
S=[x**2 for x in range(5)] S=Sample(10)
print(S) OUTPUT
OUTPUT The value is :10
[0, 1, 4, 9, 16]
list1=[10,11,12,[9,13,15]]
list=[y**2 for y in range(5)] x=len(list1)
print(list) print(x)
OUTPUT OUTPUT
[0, 1, 4, 9, 16] 4
List1=[2,4,6,[1,3,5],1,2,5,6] Write a program to display sum of natural numbers,
x=len(List1) upto n.
print(x) s=0
OUTPUT n = int (input("Enter number of terms"))
8 for i in range (1, n + 1):
s=s+i
List1=[2,4,6,[1,3,5]] print ("sum = ", s)
x=len(List1) OUTPUT
print(x) Enter number of terms100
OUTPUT sum = 5050
4 or
num=int(input("Enter the number"))
a=int(input("Enter the first number:")) if num < 0:
b=int(input("Enter the second number:")) print("Enter a positive number")
if(a>b): else:
min1=a sum = 0
else: while(num > 0):
min1=b sum += num
while(1): num -= 1
if(min1%a==0 and min1%b==0): print("The sum is", sum)
print("LCM is:",min1)
break OUTPUT
min1=min1+1 Enter number of terms100
OUTPUT sum = 5050
LCM is: 4
Program to display the number of vowels and
A="Corporation" consonants in the given string
print(len(A)) str1=input ("Enter a string: ")
str2="aAeEiIoOuU"
OUTPUT
v,c=0,0
11 for i in str1:
if i in str2:
city="chennai" v+=1
print(city.capitalize()) else:
OUTPUT c+=1
Chennai print ("The given string contains {} vowels and {}
consonants".format(v,c))
Enter a string: hello im sunesh
Write a Python code to check whether a given The given string contains 5 vowels and 10
year is leap year or not. consonants
y=int(input("Enter a year : ")) ch = input("Please Enter Your Own Character : ")
n=y%4 if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch
if (n==0): == 'u' or ch == 'A'or ch == 'E' or ch == 'I' or ch =='O'
print("The year {} is a Leap Year".format(y)) or ch == 'U'):
else: print("The Given Character ", ch, "is a Vowel")
print("The year {} is not a Leap Year".format(y)) else:
print("The Given Character ", ch, "is a
OUTPUT Consonant")
Enter a year : 2024
The year 2024 is a Leap Year OUTPUT
Enter a year : 1999 Please Enter Your Own Character : r
The year 1999 is not a Leap Year The Given Character r is a Consonant
Please Enter Your Own Character : e
The Given Character e is a Vowel
str1 = "Welcome to learn Python"
print(str1[10:16])
print(str1[::-2]) Create an interactive program to accept the details
print(str1[0:7]) from user and store it in a csv file using Python for
print(str1[12:]) the following table.
print(str1[::3]) Database name;- DB1
Table name : Customer
print(str1*2)

OUTPUT
learn
nhy re teolW
Welcome
earn Python
Wceoenyo
Welcome to learn PythonWelcome to learn Python
import sqlite3 VALUES (1008,"monitor",15000);"""
# connecting to the database cursor.execute(sql_command)
connection = sqlite3.connect ("DB1.db") sql_command="""INSERT INTO
# cursor product(Icode,ItemName,Rate)
cursor = connection.cursor() VALUES (1010,"mouse",700);"""
cursor.execute ("""DROP TABLE customer;""")
sql_command = """
CREATE table customer( connection.commit()
Cust_Id INT PRIMARY KEY NOT NULL, connection.close()
Cust_Name TEXT NOT NULL, print("SUCCESSFULLY CREATED")
Address CHAR(50),
Phone_no INT (50), import sqlite3
City TEXT NOT NULL);""" connection = sqlite3.connect ("company.db")
cursor =connection.cursor()
cursor.execute(sql_command) crsr = connection.cursor()
sql_command="""INSERT INTO customer crsr.execute("SELECT * FROM product")
(Cust_Id,Cust_Name,Address,Phone_no,City) ans= crsr.fetchmany(5)
VALUES (111, 'Sandeep', '14/1 Pritam Pura', print(*ans,sep="\n")
41206819,'Delhi' );"""

cursor.execute(sql_command) Write a Python script to create a table called ITEM


sql_command="""INSERT INTO customer with following specification.
(Cust_Id,Cust_Name,Address,Phone_no,City) Add one record to the table.
VALUES (222, 'Anurag Basu', '15A, Park Road', Name of the database :- ABC
61281921,'Bangalore' );""" Name of the table :- Item
Column name and specification :-
cursor.execute(sql_command) Icode:-integer and act as primary key
sql_command="""INSERT INTO customer Item Name:-Character with length 25
(Cust_Id,Cust_Name,Address,Phone_no,City) Rate:-Integer
VALUES (3333, 'Hrithik', '17/2 Vasant Nagar',
Record to be added:-1008, Monitor,15000
26121949,'Mumbai' );"""

connection.commit() import sqlite3


connection.close() # connecting to the database
print("SUCCESSFULLY CREATED") connection = sqlite3.connect ("ABC.db")
# cursor
Write the Python script to display all the records cursor = connection.cursor()
of the following table using fetchmany() #cursor.execute ("""DROP TABLE Item;""")
Itemcode ItemName Rate sql_command = """
1003 Scanner 10500 CREATE table Item(
1004 speaker 3000 Icode INT PRIMARY KEY NOT NULL,
1005 Printer 8000 ItemName varchar(25) NOT NULL,
1008 monitor 15000 Rate int (50));"""
1010 Mouse 700
import sqlite3 connection.commit()
# connecting to the database connection.close()
connection = sqlite3.connect ("company.db") print("SUCCESSFULLY CREATED")
# cursor
cursor = connection.cursor() Create a plot. Set the title, the x and y labels for both
#cursor.execute ("""DROP TABLE product;""") axes.
sql_command = """ import matplotlib.pyplot as plt
CREATE table product( x = [1,2,3]
Icode INT PRIMARY KEY NOT NULL, y = [5,7,4]
ItemName TEXT NOT NULL, plt.plot(x, y)
Rate INT (50));""" plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
cursor.execute(sql_command) plt.title('LINE GRAPH')
sql_command="""INSERT INTO plt.show()
product(Icode,ItemName,Rate)
VALUES (1003,"scanner",10500);"""

cursor.execute(sql_command)
sql_command="""INSERT INTO
product(Icode,ItemName,Rate)
VALUES (1004,"speaker",3000);"""

cursor.execute(sql_command)
sql_command="""INSERT INTO
product(Icode,ItemName,Rate) Plot a pie chart for your marks in the recent
VALUES (1005,"printer",8000);""" examination.
import matplotlib.pyplot as plt
cursor.execute(sql_command) sizes = [89, 80, 90, 100, 75]
sql_command="""INSERT INTO labels = ["Tamil", "English", "Maths", "Science",
product(Icode,ItemName,Rate) "Social"]
plt.pie (sizes, labels = labels, autopct = "%.2f")
plt.axes().set_aspect ("equal")
plt.show()

Plot a bar chart for the number of computer science


periods in a week.
import matplotlib.pyplot as plt
labels=["SUN","MON","TUE","WED","THU","FRI"
,"SAT"]
use=[3,2,1,3,2,2,2]
y_positions=range(len(labels))
Plot a line chart on the academic performance of plt.bar(y_positions,use)
Class 12 students in Computer Science for the past 10 plt.xticks(y_positions,labels)
years. plt.xlabel("PERIODS")
import matplotlib.pyplot as plt plt.ylabel("years")
years=[2009,2010,2011,2012,2013,2014,2015,2016,2 plt.title("COMPUTER SCIENCE
017,2018] PERFORMANCE")
cs=[65,70,75,76,78,80,85,89,92,96] plt.show()
plt.plot(years,cs)
plt.title("COMPUTER SCIENCE
PERFORMANCE")
plt.xlabel("cs")
plt.ylabel("years")
plt.show()
S.J.SUNESH,
COMPUTER SCIENCE INSTRUCTOR,
EXCEL HIGHER SECONDARY SCHOOL,
THIRUVATTAR, KANYAKUMARI DIST.
:sunesh.stanly@gmail.com

:+91 8903759872,
+91 9080799989

: +91 8903759872
For downloading ppt materials https://www.nammakalvi.in/12th-computer-science-study-materials-and-
guides-download.html

You might also like