Friday 27 December 2019

CBSE Class 11 - Informatics Practices - Python - Type Conversion and Type Casting - Question and Answers(#class11Python)(#eduvictors)

Python Basics - Type Conversion and Type Casting 

Question and Answers
CBSE Class 11 - Informatics Practices

CBSE Class 11 - Informatics Practices - Python - Type Conversion and Type Casting - Question and Answers


Question 1: What is Type Conversion?

Answer: Type conversion means converting one data type to another data type. Python supports two types of type conversion:

1. Implicity Type Conversion
2. Explicit Type Conversion (also called Type Casting)


Question 2: What is implicit type conversion? Give an example.



Answer: In implicit conversion, Python automatically converts one data type to another. In this type conversion, Python takes care there should not be any data loss.

e.g.
a = 10
b = 3
c = a/b
print(c)
type(a)
type(b)
type(c)

In the above snippet, a and b are integer type data while c stores float value i.e. result of a/b. Here  Python promotes conversion of lower datatype (i.e. integer) to higher data type (i.e. float) to avoid data loss.


Question 3: Will implicit conversion take place in the following snippet? Why or Why not?
str1 = "Hello"
num1 = 2020
print(str1+ num1)


Answer: It will give TypeError i.e. it cannot convert num1 (integer) to string implicitly. We need to use explicit type conversion i.e. str(num1)


Question 4: What is type casting?

Answer: Type casting is a user-defined explicit type conversion to convert the data type of an object to required data type.
e.g.
    a = int(1.0)    # converts float to integer
    b = int('123') # converts string to an integer
    c = str(1.20) # converts float to string


Question 5: Write python statements to convert 
(a) integer to string
(b) integer to float
(c) integer to hexdecimal string
(d) integer to binary string
(e) integer to octal string
(f)  integer to bool value
(g) float to integer
(h) float to string
(i) float to bool
(j) string to float
(k) string to bool
(l) character to ASCII code

Answer:
(a) >>> str(16)
(b) >>> float(21)
(c) >>> hex(21)
(d) >>> bin(21)
(e) >>> oct(21)
(f) >>> bool(21)
(g) >>> int (2.75)
(h) >>> str(2.75)
(i) >>> bool(2.75)
(j) >>> float('2.75')
(k) >>> bool('Hello')
(l) >>> ord('A')


Question 6: State one advantage and one disadvantage of type casting.

Answer:
Advantage: More convenient to use e.g. input() returns string value which can be converted to desired data type.

Disadvantage: May lead to data loss e.g. int(2.75) results in 2. The decimal part is lost.



Question 7: What is the output of bool(1) and bool(0)?

Answer: bool() returns True for any non-zero value or non-empty string. It returns False for zero value or empty string.
bool('Hi')  #returns True
bool(20)   #returns True
bool('')      #returns FALSE
bool(0)     #returns FALSE




☞See also:

No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.