Basics of Python-XI
Basics of Python-XI
Ans. (i) //
Ans.
(i) Key word (ii) Identifier
Q3. Rewrite the following code in python after removing all syntax error(s).
Underline each correction done in the code. [2]
30=To
for K in range(0,To)
IF k%4==0:
print (K*4)
Else:
print (K+3)
Ans.
To=30 # Correction 1
for K in range(0,To): # Correction 2
if k%4==0: # Correction 3
print (K*4)
else: # Correction 4
print (K+3)
Page 1
Q7. Find and write the output of the following python code: [1]
x = "abcdef"
i = "a"
while i in x:
print(i, end = " ")
Q8. Write the names of any four data types available in Python. 2019 AI [2]
Ans.
Numbers
Integer
Boolean
Floating Point
Complex
None
Sequences
Strings
Tuple
List
Sets
Mappings
Dictionary
Q9. Rewrite the following code in python after removing all syntax error(s). Underline
each correction done in the code. 2019 AI [2]
250 = Number
WHILE Number<=1000:
if Number=>750:
print(Number)
Number=Number+100
else
print(Number*2)
Number=Number+50
Ans.
Number = 250 # Correction 1
while Number<=1000: # Correction 2
if Number >= 750: # Correction 3
print(Number)
Number = Number+100
else: # Correction 4
print(Number*2)
Number = Number+50
Page 2
Q10. Find and write the output of the following python code: 2019 AI [2]
Msg1="WeLcOME"
Msg2="GUeSTs"
Msg3=""
for I in range(0,len(Msg2)+1):
if Msg1[I]>="A" and Msg1[I]<="M":
Msg3=Msg3+Msg1[I]
elif Msg1[I]>="N" and Msg1[I]<="Z":
Msg3=Msg3+Msg2[I]
else:
Msg3=Msg3+"*"
print(Msg3)
Ans.
G*L*TME
Q11. Which of the following are valid operators in Python ? 2019 AI Comptt. [2]
(i) +=
(ii) ^
(iii) in
(iv) &&
(v) between
(vi) */
(viii) like
Q12. Rewrite the following code in Python after removing all syntax error(s). Underline each
correction done in the code. 2019 AI Comptt. [2]
"HELLO"=String
for I in range(0,len(String)–1)
if String[I]=>"M":
print(String[I],"*")
Else:
print(String[I-1])
Ans.
String=ʺHELLOʺ # Correction 1
for I in range(0,len(String)–1): # Correction 2
if String[I]>="M": # Correction 3
print(String[I],"*")
else: # Correction 4
print(String[I-1])
Page 3
Q13. Find and write the output of the following Python code : 2019 AI Comptt [2]
Str1="EXAM2018"
Str2=""
I=0
while I<len(Str1):
if Str1[I]>="A" and Str1[I]<="M":
Str2=Str2+Str1[I+1]
elif Str1[I]>="0" and Str1[I]<="9":
Str2=Str2+ (Str1[I-1])
else:
Str2=Str2+"*"
I=I+1
print(Str2)
Ans. X*M2M201
Q14. Differentiate between Syntax Error and Run-Time Error? Also, write a suitable example in
Python to illustrate both. 2018 AI [2]
Ans. Syntax error : An error of language resulting from code that does not conform to the
syntax of the programming language.
Example
a=0
while a < 10 # : is missing as per syntax
a=a+1
print(a)
Runtime error : A runtime error is an error that causes abnormal termination of program during
running time.
Example
A=10
B= int(input("Value:"))
print(A/B)
Page 4
Q15. Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code. 2018 AI [2]
Val = int(input("Value:"))
Adder = 0
for C in range(1,Val,3)
Adder+=C
if C%2=0:
Print(C*10)
Else:
print(C*)
print(Adder)
Ans.
Q16. Find and write the output of the following python code: 2018 AI [3]
Data = ["P",20,"R",10,"S",30]
Times = 0
Alpha = ""
Add = 0
for C in range(1,6,2):
Times= Times + C
Alpha= Alpha + Data[C-1]+"$"
Add = Add + Data[C]
print(Times,Add,Alpha)
Ans.
1 20 P$
4 30 P$R$
9 60 P$R$S$
Page 5
Q17. Find and write the output of the following python code : 2018 AI Comptt. [3]
Val = [20,"A",40,"K",10,"H"]
Freq = 0
Sum = 0
Cat = ""
for I in range(1,6,2):
Freq = Freq + I
Sum = Sum + Val[I-1]
Cat = Cat + Val[I] + "*"
print(Freq,Sum,Cat)
Ans.
1 20 A*
4 60 A*K*
9 70 A*K*H*
Q18. Which of the following can be used as valid variable identifier(s) in Python? 2017 AI [2]
(i) 4thSum
(ii) Total
(iii) Number#
(iv) Data
Q19. Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code. 2017 AI [2]
STRING=""WELCOME
NOTE""
for S in range[0,7]:
print(STRING(S))
print(S+STRING)
Ans.
Page 6
Q20. Find and write the output of the following python code: 2017 AI [2]
TXT = ["20","50","30","40"]
CNT = 3
TOTAL = 0
for C in [7,5,4,6]:
T = TXT[CNT]
TOTAL = float (T) + C
print(TOTAL)
CNT- =1
Ans.
47.0
35.0
54.0
26.0
Q21. Which of the following can be used as valid variable identifier(s) in Python ?
2017 AI Comptt. [2]
(i) elif
(ii) BREAK
(iii) in
(iv) _Total
Q22. Rewrite the following code in Python after removing all syntax error(s). Underline
each correction done in the code. 2017 AI Comptt. [2]
NUM1=1234
1=DAY1
for C in range[1,4]:
NUM+C=NUM1
DAY1=DAY1+2
print(C)
print(NUM1:DAY1)
Ans.
NUM1=1234
DAY1=1 # Correction 1
for C in range(1,4): # Correction 2
NUM1=NUM1+C # Correction 3
DAY1=DAY1+2
print(C)
print(NUM1:DAY1)
Page 7
Q23. Find and write the output of the following Python code : 2017 AI Comptt.[2]
L1 = [100,900,300,400,500]
START = 1
SUM = 0
for C in range (START,4):
SUM = SUM + L1[C]
print(C,":",SUM)
SUM = SUM + L1[0]*10
print(SUM)
Ans.
1 : 900
1900
2 : 2200
3200
3 : 3600
4600
Q24. Out of the following, find those identifiers, which can not be used for naming Variable or
Functions in a Python program:
Total*Tax, While, switch, 3rdRow, Column31, _Total 2016 AI [2]
Q25. Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code. 2016 AI [2]
for Name in [Ramesh,Suraj,Priya]
IF Name[0]='S':
print(Name)
Ans.
for Name in [ ʺRameshʺ , ʺSurajʺ , ʺ Priyaʺ] : # Correction 1 & 2 // ʹ ʹ can be used
if Name[0] = = ‘S’: # Correction 3 & 4
print(Name)
Q26. Find and write the output of the following python code: 2016 AI [2]
Values=[10,20,30,40]
for Val in Values:
for I in range(1, Val%9):
print(I,"*",end= "" )
print( )
Ans.
1*
1 *2 *
1 *2 *3 *
Page 8
Q27. Out of the following, find those identifiers, which can not be used for naming Variable or
Functions in a Python program: 2016 AI Comptt. [2]
Days*Rent, For, A_Price, Grand Total, 2Clients, Participants1, _MyCity
Q28. Differentiate between round( ) and floor( ) functions with the help of suitable example.
2016 AI Comptt. [1]
Ans.
round( ):
Syntax:
round(<number>,[<ndigits>])
It returns number rounded to n digits after the decimal points. If ndigits is not given, it returns
nearest integer to its input.
Example:
>>>round(5.555682)
5
>>>round(5.555682,3)
5.556
floor( ):
Syntax:
math.floor(x)
x may be an integer or floating point number
It returns the largest integer not greater than x
Example:
>>> math.floor(4.5)
4
>>> math.floor(-4.5)
-5
Q29. Find and write the output of the following python code : 2015 AI [2]
for Name in ['Jayes', 'Ramya', 'Taruna', 'Suraj']:
print(Name)
if Name[0]== 'T':
break
else :
print('Finished!')
print('Got it!')
Ans.
Jayes
Ramya
Taruna
Got it!
Page 9
Q30. What is the use of split method while using it with String in Python? Give a suitable
example to illustrate its use. 2015 AI Comptt. [2]
Ans. The split( ) method breaks up a string at the specified separator & returns a list of
substrings.
Syntax:
str.split([separator[,maxsplit]])
The split( ) method takes maximum of two parameters:
separator(optional)- The separator is a delimeter. The string splits at the specified
separator. If the separator is not specified, any white space(space,newline,etc.)string is a
separator.
maxsplit(optional)- The maxsplit defines maximum number of splits. The default value
of maxsplit is -1, which means no limit on the number of splits. If an integer value ‘n’ is
given for the second argument, the string is split into ‘n+1’ strings.
Example:
>>> x=ʹblue; red; greenʹ
>>> x.split(ʺ ; ʺ)
[ʹblueʹ, ʹredʹ, ʹgreenʹ]
>>> text=ʹAPS CT DDunʹ
>>> print(text.split( ))
[ʹAPSʹ, ʹCTʹ , ʹDDunʹ]
Here, separator is not specified, by default, space is a string separator.
>>> colour=ʹRedʹ:ʹBlueʹ:ʹOrangeʹ:ʹPinkʹ
>>> print(colour.split(ʹ:ʹ,2))
[ʹRedʹ, ʹBlueʹ, ʹOrange:Pinkʹ]
Q31. Explain the following methods with respect to lists in python. 2015 AI Comptt. [1]
(i) remove
(ii) reverse
Ans.
(i) remove( ): It is used when we know the element to be deleted not the index of the element.
The remove( ) method removes the first occurrence of given item from the list.
Syntax:
List.remove(value)
The remove( ) will report an error if there is no item in the list.
Example:
>>>L1=[10,20,30,20,50]
>>>L1.remove(20)
>>>print(L1)
[10,30,20,50]
(ii) reverse( ): It reverses the elements of the list in place. It does not create a new list.
Syntax:
List.reverse( )
Page
10
>>> L1=[1,2,3,4]
>>> L1.reverse( )
>>> print(L1)
[4,3,2,1]
Q32. Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code. 2015 AI Comptt. [2]
for CITY of [ ʹMumbaiʹ , ʹDelhiʹ , ʹPuneʹ , ʹHyderabadʹ ]:
print(Name)
if Name[0]= =ʹDʹ or Name[0]= =ʹHʹ
BREAK
else:
print(ʹSome State!ʹ)
print(All State)
Ans.
for Name in [ ʹMumbaiʹ , ʹDelhiʹ , ʹPuneʹ , ʹHyderabadʹ ]: # Correction 1 & 2
print(Name)
if Name[0]= =ʹDʹ or Name[0]= =ʹHʹ : # Correction 3
break # Correction 4
else:
print(ʹSome State!ʹ)
print(ʺAll Stateʺ) # Correction 5
Page
11