Sei sulla pagina 1di 8

SRI KRISHNA COLLEGE OF TECHNOLOGY

An Autonomous College, Accredited by NAAC with “A”


grade and by NBA-AICTE
Coimbatore, Tamil Nadu
Academic Year 2017-2018 (EVEN SEM)
B.E. COMPUTER SCIENCE ENGINEEIRNG
CONTINUOUS INTERNAL ASSESSMENT – I
ANSWER KEY

Class Course Code Course Title Date


III Year CSE 16CS408 PYTHON PROGRAMMING 30.1.19
Duration: 90 min Max: 50 Marks

Course Outcomes:
Recognize and construct common programming idioms: variables, loop, branch, subroutine,
CO1
and input/output.
Describe the principles of object-oriented programming and the interplay of algorithm in
CO2
well-written modular code.
CO3 Be able to design, code, and test Python programs.
Solve problems requiring the writing of well-documented programs in the Python language,
CO4
including use of the logical constructs of that language.
Demonstrate significant experience with the Python program development environment.
CO5

Part – A (09 x 02 = 18 Marks)


Answer All Questions BT CO Marks
Compare List and Tuple
LIST TUPLE
List is mutable Tuples is immutable
A list contains items separated Tuples are enclosed within
by commas and enclosed within parentheses.
square brackets ([]).
1 U CO2 2
Elements and size can be Tuples can be thought of as read-only
changed lists.
Eg: list = [ 'abcd', 786 , 2.23, tuple = ( 'abcd', 786 , 2.23, 'john',
'john', 70.2 ] 70.2 )
List is slower Indexing speed is faster than lists
List is that use more memory Tuples is that they use less memory
Identify the output for the following function
If not a or b :
2 Print 1 AP CO1 2
elif not a or not b and c :
print 2
elif not a or b or not b and a :
print 3
else :
print 4
ANS: 3

List the supported datatypes in python


• Numbers

• String

• Boolean

3 • List R CO1 2

• Tuple

• Set

• Dictionary

Write a program to print the list after removing delete even numbers in
[5,6,63,47,22,14,24]

list = [] # create empty list


n=input(“enter length of list)
for i in range(0, n): # set up loop to run 5 times
number = int(input(' enter a number: '))
list.append(number)
# print original list
4 print ("Original list:") AP CO2 2
print (list)
for i in list:
if(i%2 == 0):
list.remove(i)
# print list after removing EVEN elements
print ("list after removing EVEN numbers:")
print (list)
(Any logic with proper output and syntax is acceptable )

Classify the types of operator in python along with suitable example.


U CO2
• Arithmetic Operators 2
• Comparison (Relational) Operators
• Assignment Operators
5 • Logical Operators
• Bitwise Operators
• Membership Operators 2
• Identity Operators

(Mark can be allocated for any example with correct symbols and keywords )
Identify the output for the following function
i)len([1,2,3])
ii)[1,2,3]+[4,5,6]
6 iii)L[1:] if L =[1,2,3] AP CO1 2
iv)[SKCT]*4
ANS
i)3 ii) [1, 2, 3, 4, 5, 6] iii) [2, 3] iv)Print for 4 times
Illustrate the use of * and + operators in string
• Python allows for either pairs of single or double quotes. Subsets of
strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes
starting at 0 in the beginning of the string and working their way from -
1 at the end.
• The plus ( + ) sign is the string concatenation operator, and the asterisk
7 AP CO1 2
( * ) is the repetition operator.
(Mark can be allocated for any example with correct syntax)
Eg:
str = 'Hello World!'
print str[2:5]
print str * 2
Program to check whether the number is power of 2 or not
def PowerOfTwo(n):
if (n == 0):
return False
while (n != 1):
if (n % 2 != 0):
return False
n = n // 2
8 AP CO1 2
return True

# Driver code
n=int(input("enter the number"))
x=PowerOfTwo(n)
print(x)
(Any logic with proper output and syntax is acceptable )
Write a program that generate and print 30 random integer between 5 and 100
ANS
from random import randint
for i in range(1,30):
x = randint(5,100)
9 AP CO1 2
print('A random number between 5 and 100: ', x)

Part – B (2 x 16 = 32 Marks)
BT CO Marks
Answer any five Questions
10 i) A store charge $12 per item if you buy less than 10 items. If you buy
between 10 and 99 items , the cost is $10 per item . If you buy 100 or more
than 100 items, the cost is $7 per item.Write a program that asks the user
how many items they are buying and print the total cost.
ANS:
item=int(input("enter the item"))
if item<10: AP CO1 8
item = item * 12
elif item>=10 and item<=99:
item = item *10
elif item>100:
item=item * 7
print(item)
ii) Print the pattern using python
0
22
444
8888
ANS:
for num in range(0,10): AP CO1 8

if num%2==0:
for i in range(num):
print(num, end=" ")
# new line after each row to display pattern correctly
print("\n")
(Marks can be allocated for any logic with proper method and syntax)
11 (i) i)To count number of vowels in string
string=input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I'
or i=='O' or i=='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)

ii)To count occurrences of character in string.


string = input("Enter any string to count character: ");
char = input("Enter a character to count to count from above string: ");
val = string.count(char);
print("Total = ",val);
AP CO1 8

iii)To perform string reverse


def reverse(s):
str = ""
for i in s:
str = i + str
return str
s=input("enter string")
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using loops) is : ",end="")
print (reverse(s))
iv)To count digits and special characters
string = input("Enter String : ")
alphabets = digits = special = 0

for i in range(len(string)):
if(string[i].isalpha()):
alphabets = alphabets + 1
elif(string[i].isdigit()):
digits = digits + 1
else:
special = special + 1

print("\nTotal Number of Alphabets in this String : ", alphabets)


print("Total Number of Digits in this String : ", digits)
print("Total Number of Special Characters in this String : ", special)
v)To remove vowels from the string
def rem_vowel(string):
vowels = ('a', 'e', 'i', 'o', 'u')
for x in string.lower():
if x in vowels:
string = string.replace(x, "")
# Print string without vowels
print(string)
string = input("string")
rem_vowel(string)

vi)To replace all ‘a’ with ‘x’ in string


str = input("enter the string")
print (str.replace("a", "x"))
(Marks can be allocated for any logic with proper method and syntax)
ii) Function to calculate the discount from the product from an online
store
Quantity Discount
1-49 15%
50-99 35%
100>more 45%

Class Store
def Discount(self,quantity):
self.quantity=quantity
if quantity >=1 and quantity <= 49:
statement
elif quantity >=50 and quantity <= 99:
statement AP CO2 6
elif quantity>100
statment
total = quantity * package – discount
return total
D=Store()
Q=int(input(“enter quantity”))
Tot=D.Discount(Q)
print('Your total for this purchase is ${}. Thank you for shopping with
us'.format(Tot))
(Can be solved using other methods with function concept)

Potrebbero piacerti anche