Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views5 pages

Ans Key

The document provides explanations and examples of various programming concepts in Python, including conditional statements, data types, loops, error types, and list operations. It also includes sample code snippets for tasks such as counting positive and negative numbers, replacing values in a list, and searching for elements. Additionally, it outlines the differences between certain functions and operators in Python.

Uploaded by

akshat1949
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)
10 views5 pages

Ans Key

The document provides explanations and examples of various programming concepts in Python, including conditional statements, data types, loops, error types, and list operations. It also includes sample code snippets for tasks such as counting positive and negative numbers, replacing values in a list, and searching for elements. Additionally, it outlines the differences between certain functions and operators in Python.

Uploaded by

akshat1949
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/ 5

21. What is the difference between if..else and if..elif..else conditional statements?

Explain with (2)


an example.

Ans. The IF- Else statement is used when we need to choose between two alternatives. If
only one statement have to evaluate we can use the IF- Else statement.

Whereas The IF-Elif-Else statement is used when we have more than two alternatives to
choose from. The use of IF-Elif statements is appropriate when we need to assess multiple
statements.

Example- if condition:
# do something if the condition is true
else:
# do something if the condition is false

if condition1:
# do something if condition1 is true
elif condition2:
# do something if condition 2 is true
else:
# do something if both condition1 and condition2 are false

22 Write any two differences between list and string? (2)

Ans. List are mutable whereas String are immutable.


List can store different datatypes of data like int , float, list , string
whereas string can store string datatype data only.

23. What do you mean by infinite loops? Explain with an example. (2)

Ans. An infinite loop occurs when a condition always evaluates to true and because of this
the loop control doesn't go outside of that loop.
Example:
i = -1
while(i != 0):
print(1)

24. Rao has written a code to input a number and check whether it is prime or not. His code is (2)
having errors. Rewrite the correct code and underline the corrections made.

n=int(input("Enter number to check :: ")


for i in range (2, n//2):
if n%i=0:
print(Number is not prime \n)
break
else:
print("Number is prime \n’)

Ans.
n=int(input("Enter number to check :: ")) #bracket missing
for i in range (2, n//2):
if n%i==0: # = missing
print("Number is not prime \n") #quotes missing
break
else:
print("Number is prime \n”) # quote mismatch

25. Explain membership operator in python with an example. (2)

Ans. In Python, the membership operators for strings are "in" and "not in". These operators
allow to check the presence or absence of a substring within a given string. They return a
boolean value - True if the substring is present, and False otherwise.

Example-

x = ["apple", "banana"]

print("banana" in x)

# returns True

26. What is identity operator? Explain with an example. (2)


Ans. Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is not z)
# returns False because z is the same object as x
print(x is not y)
# returns True because x is not the same object as y, even if they have the same content
print(x != y)
# to demonstrate the difference betweeen "is not" and "!=": this comparison returns False
because x is equal to y
27. How you can identify the valid identifier in python? (2)

Ans. the rules are


a. Keywords are not allowed for variable names.
b. Spaces are not allowed for variable names.
c. Variable names cannot start from number.
d. Special symbols other than _ underscore is not allowed.

28. What is the difference between keyword and variable? (2)

Ans. Keywords are reserved words for python language.


Whereas variables are programmer defined words following the valid variable rules to name
the memory storage.

SECTION-C

Attempt any 5 questions from 29 to 34 3*5=15

29. What is the difference between break and continue statements? Explain with an example. (3)

Ans. Break: Terminates loop prematurely.

for i in range(1, 6):


if i == 4:
break
print(i) #123

Continue: Skips current iteration of loop.


for i in range(1, 6):
if i == 4:
continue
print(i) #1235
30. Write a program to count positive and negative numbers from the list. (3)
Ans.
L=[23,45,67,-1,-56]
cp=0
cn=0
for ele in L:
if ele>0:
cp=cp+1
elif ele<0:
cn=cn+1
print(“the positive numbers are –“ , cp)
print(“ythe negative numbers are -” , cn)

31. What are datatypes? Explain different types of data types in python. (3)
Ans.
Data types specify the type of data that can be stored inside a variable.
Python Data Types
Data Types Classes Description
Numeric int, float, complex holds numeric values
String str holds sequence of characters
Sequence list, tuple, range holds collection of items
Mapping dict holds data in key-value pair form

32. Write a program to replace all even numbers with 0 and odd numbers with 1 in a list. (3)
Ans.

L=[3,45,67,31,5]
for i in range(len(L)):
if L[i]%2==0:
L[i]=0
elif L[i] %2 != 0:
L[i] =1
print(“the updated list is –“ , L)

33. Define syntax error, run time error, and logical error. Explain with an example. (3)
Ans. Error: Any problem in the program due to which the program does not executes or
the program executes but generates unexpected output or behaves abnormally.
Syntax error: Mistake in code structure, e.g., missing colon in a loop.
Logical error: Flaw in code's logic, e.g., incorrect condition.
Run-time error: Error that occurs while program is running, e.g., division by zero.
34. What do you understand by logical operators? Explain. (3)
Ans. Logical operators are used to combine conditional statements:

Operator Description Example

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is true x < 5 or x < 4

not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

SECTION-D

Attempt any 2 questions from 35 to 37. 4*2 = 8

35. (a) Write a program to find out the factorial of a given number. (3+1)

Ans.(a)

num = 7
factorial = 1
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

36. (a) Write a program in python to calculate the sum of the given list elements and display it. (3+1)
(b) Write down any one point of difference between compiler and interpreter.
Ans(a)
L=[23,45,67,-1,-56]
s=0
for ele in L:
s+=ele
print(“the sum is –“ , s)

(b)Compiler converts the high-level language code into binary language in one go whereas
the interpreter converts the code line by line.

37. (a) Write a program in python to print the table of the given no till 10. (3+1)

Ans. (a)
num = 12
# To take input from the user
# num = int(input("Display multiplication table of? "))
# Iterate 10 times from i = 1 to 10
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
SECTION-E
Attempt any 3 questions from 38 to 41. 5*3 = 15

38. (a) Write a program in python to search the given number in the list. (3+2)
(b) What is the difference between pop() and remove() function?
Ans. (a)
L=[23,45,67,-1,-56]
Token=1
s=int(input(“enter no to be searched”))
for ele in L:
if ele == s:
print(“no found”)
Token=0
break
if Token ==1:
print(“element not found”)

(b) pop() Returns the element whose index is passed as parameter to this function
and also removes it from the list. If no parameter is given, then it returns and
removes the last element of the list
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop(3)
40

remove() Removes the given element from the list. If the element is present multiple
times, only the first occurrence is removed. If the element is not present, then ValueError is
generated
>>> list1 = [10,20,30,40,50,30]
>>> list1.remove(30)
>>> list1
[10, 20, 40, 50, 30]

39. (a) Write a program in python to find the maximum number from the list. (3+2)
(b) What is the difference between append() and insert() function?

Ans. (a)

L=[23,45,67,-1,56]
max1=0
for ele in L:
if ele > max1:
max1=ele
print(“maximum no is -”, max1)
(b) append() Appends a single element passed as an argument at the end of the list
The single element can also be a list
>>> list1 = [10,20,30,40]
>>> list1.append(50)
>>> list1
[10, 20, 30, 40, 50]
insert() Inserts an element at a particular index in the list
>>> list1 = [10,20,30,40,50]
>>> list1.insert(2,25)
>>> list1
[10, 20, 25, 30, 40, 50]

You might also like