1. What are data types ? How are they important ?
Ans . Data types refer to different types of data that can be used in python language .
They are important because they can be considered as backbone of programming as without them we will not be able to give single command in python.
2 . How many integer types are supported in python ? Name them .
Ans . There are 2 types of integers i.e.
--> Integers (signed)
--> Booleans
3 . How are these numbers different from one another ? 33 , 33.0 , 33j , 33+j
Ans . 33 is an integer , 33.0 is floating number , 33j and 33+j both are complex numbers.
4 . The complex numbers have two parts : real and imaginary . In which data type are real and imaginary parts represented ?
Ans . Real and imaginary parts are represented in a complex number .
5 . How many string types does Python support ? How are they different from one another ?
Ans . There are 2 types of strings i.e. single line string and multi line string which are supported in Python.
Single line string is that string which is written or typed in one single line using single or double quotes while the multi line string is a type of string that are written or typed in multiple lines with the use of triple quotes.
6 . What will following code print ?
str1=' ' ' Hell
o ' ' '
str2= ' ' ' Hell\
o ' ' '
print (len(str1)>len(str2))
Ans . True
7 . What are immutable and mutable data types ? List immutable and mutable types in python .
Ans . Immutable data types are those data types which cannot be changed while the mutable data types are those that can be changed after it's declaration.
Immutable data types - Tuple , strings , etc.
Mutable data types - List , Dictionaries , sets, etc.
8 . What are three internal key attributes of value variable in Python? Explain with example .
Ans . The 3 key attributes are :
---> The type of an object
---> The value of an object
---> The id of an object
Example : >>>a=10
>>>print(a)
>>> 10
>>>print(id(a))
>>>30899132
Here in this the type of an object i.e a is integer type , the value of it is 10 and the id of object a is 30899132.
9 . Is it is it true that if two objects return True for is operator they will also return true for == operator ?
Ans . No, this is not always true because the is operator uses reference point to compare but the == operator uses the value stored in a variable to compare . Thus always it might not happen that for any two variables having same value might be having same reference point.
10 . Are these values equal ? Why / why not ?
Ans .
i) 20 and 20.0 ---> No , because one is integer while the other is float value.
ii) 20 and int(20) ---> Yes,because both these values are of integer type.
iii) str(20) and str(20.0) ---> No , because both string values are different first is '20' while second is '20.0'
iv) 'a' and "a" ---> Yes , because they both are pf string data types and have same value i.e a although represented in two different ways.
11 . What is an atom ? What is an expression ?
Ans . An atom is something that has a value . Identifiers , literals , strings , lists, tuples , sets, dictionary , etc. are all atoms .
An expression in Python is any valid combination of operators and atoms. An expression is composed of one or more operations.
12 . What is difference between implicit type conversion and explicit type conversion ?
Ans . An implicit type conversion is that which is done in backside of coding and is done by python compiler or interpretor while the explicit type conversion is that in which user by using some conversion functions convert the data type by its own.
13 . Two objects (say a and b) when compared using == , return true but Python gives false when compared using is operator why ?
Ans . This might be due to different reference points to which the variables a and b might be pointing although having same value .
14 . Given str1="Hello" , what will be value of ?
Ans .
a) str1[0] ---> "H"
b) str[1] ---> "e"
c) str[-5] ---> "H"
d) str[-4] ---> "e"
e) str[5] ---> error
15 . If you give str1='Hello' , whiy does the below code give error then ?
str1[2]='p'
Ans . This is so because we know that in python strings are immutable data types i.e. after its declaration there value can't be changed . Due to this fact only there occurs error when we write the above code .
16 . What will the result given by following ?
Ans . a) type(6+3) ---> Integer
b) type(6-3) ---> Integer
c) type(6*3) ---> Float
d) type(6/3) ---> Float
e) type(6//3) ---> Float
f) type(6%3)---> Float
17 . What are augmented assignment operators ? How are they useful ?
Ans . They are the operators which enable us to combine any two or more operators and to code them in shorthand .
They are useful as they minimise the code .
18 . Differentiate between (555/222)*2 and (555.0/222)*2 .
Ans . Although the output of the above two commands will be same i.e. a float value but there is slight difference in the input command and it's processing . In the first command all the operands are integer while in the second command one of them i.e. 555.0 is a float value .
19 . Given three Boolean variables a, b, c as : a=False , b= True , c=False.
Evaluate the following below expressions :
Ans . a) b and c ---> False
b) b or c ---> True
c) not a and b ---> True
d) (a and b ) or not c ---> True
e) not b and not(a or) ----> False
f) not((not b or not a) and c) or a --->True
20 . What would the following code fragments result in ? Given X=3 .
Ans . a). 1<X ---> True
b) X>=4 ---> False
c) X==3 ---> True
d) X==3.0 ---> True
e) 'Hello'=="Hello" ---> True
f) "Hello" > "hello" ---> False
g) 4/2==2.0 --> True
h) 4/2==2 ---> True
i) X<7 and 4> 5 ---> False
21 . Write the following expression in python :
Ans . a) ⅓b²h = (1/3)(b*2)*h
b) ฯr²h = (math.pi)(r*2)*h
c) ⅓ฯr²h = (1/3)math.pi(r**2)*h
d) d=√((x2-x1)²+(y2-y1)²) => d=math.sqrt((x2-x1)*2+(y2-y1)*2)
e) (x-h)²+(y-k)²=r² => ((x-h)*2)+((y-k)2) = (r*2)
f) X=(-b+√(b²-4ac))/2a => X=(-b+math.sqrt((b**2)-4*a*c))/(2*a)
g)a³ x a⁶= a^(3+6) => (a*3)+(a6)=a*(3+6)
h) (a³)² = a⁶ => (a*3)2 = a*6
i) a³/a² = a³–² => (a*3)/(a2) = a*(3-2)
j) a–² = 1/a² => (a*(-2)) = 1/(a*2)
22. int('a') produces error . Why ?
Ans . Because we can't convert a string data type to integer unless it has some numeral value stored in it.
23 . int('a') produces error but the following code having int('a') in it , does not return error . Why ?
Len('a')+2 or int('a')
Ans . In the above expression the code is a Boolean expression thus when error occurs in int('a') it give the value as False and proceeds to the addition due to or operator.
24 . Write expression to convert the values 17 , Len('ab') to -
Ans . a) integer ---> by using int( ) function
b) string ---> by using str( ) function
c) Boolean values ---> by using bool( ) function
25 . Evaluate :
Ans . a) 22.0/7.0 - 22/7 = 0
b) 22.0/7.0 - int(22.0/7.0) = 0
c) 22/7 - int(22.0)/7 = 0
Each of the above code will give output --> 0 .This is so because in case a the output of both 22.0/7.0 and 22/7 gives the same output and thus their subtraction result in 0.
Similar is the case in b and c .
26 . Evaluate and justify :
Ans . a) false and None ---> error as false is not defined
b) 0 and None ---> 0
c) True and None ---> None
d) None and None ---> None
27) Evaluate :
Ans . a) 0 or None and "or" = None
b) 1 or None and 'a' or 'b' = 1
c) False and 23 = False
d) 23 and False = False
e) not(1==1 and 0!=1) = False
f) 'abc'=='Abc' and not(2==3 or 3==4) = False
g) (False and 1==1 or not True or 1==1 and False or 0==0) = True
28) Evaluate the following for each expression, that is successfully evaluated determine its value and type for unsuccessful expression state the reason :
Ans . a) Len('hello')==25/5 or 20/10 --> True
b) 3<5 or 50/(5-(3+2)) ---> True
c) 50/(5-(3+2)) or 3<5 ---> Error due to division by 0.
d)2*(2*(Len("O1"))) ---> 8
29) Write an expression that uses exactly 3 arithmetic operators with integer literals and produces result as 99 .
Ans . 9/3*11+33
30) Add parentheses to the following expression to make the order of evaluation more clear .
Ans . (y%4)==0 and ((y%100)!=0) or ((y%400)==0)
Type - B Application Base Questions :
Ans 1 .
a) bool(0) = False. --> It gives the Boolean value of zero which False . (0= False and 1= True)
b) bool (str(0)) =True. --> It gives True because the str( ) function makes a string '0' and thus the Boolean value gives True.
Ans 2 . False
Ans 3 . This is so because we know that the division and multiplication operator always give output in float point form .
Ans 4 . a) 3 3 b) True
False
Ans 5 . True
False
True
False
Ans 6 . -2
81
Ans 7 . Error will be produced when we run this code because in the first line of code value has not been assigned to b and c .
Ans 8 . 4.6 7.4
13.8
21.0
Ans 9 . 4.0
Ans 10 . x , y = 4 , 8
z = (x / y * y ) - x
print ( z )
Ans 11 . Changed expression : 3 + 10/0
Ans 12 . 4 4
1 3
Ans 13 . s=''12345''
sum=0
for k in s :
sum+= int(k)
print(sum)
Ans 14 . True
False
True
True
True
False
Ans 15 . ( underlined red lines are having errors )
a) name = ''HariT''
print ( name )
name [ 2 ] = ''R''
print ( name )
b) a = bool ( 0 )
b = bool ( 1 )
print ( a==false )
print ( b==true )
c) print ( type ( int ( ''123'' ) ) )
print ( type ( int ( '''Hello'' ) ) )
print ( type ( str ( ''123.0'' ) ) )
d) pi = 3.14
print ( type ( pi ) )
print ( type ( ''3.14'' ) )
print ( type ( float ( ''3.14'' ) )
print ( type ( float ( ''three point one four ' ) )
e) print ( ''Hello'' + 2 )
print ( ''Hello'' + ''2'' )
print ( ''Hello'' * 2 )
f) print ( ''Hello'' / 2)
print ( ''Hello'' / 2 )