Grade 11 CS ch-7 Data Handling C.W notes
Grade 11 CS ch-7 Data Handling C.W notes
i) Numbers
ii) String
iii) List
iv) Tuple
v) Dictionary
1. Integers (signed)
2. Booleans
3.What are Immutable and Mutable types in Python? List immutable and mutable types of Python?
Mutable types are those whose values can be changed in place whereas Immutable types are those that
can never change their value in place.
1. Lists
2. Dictionaries
3. Sets
1. Integers
2. Floating-Point numbers
3. Booleans
4. Strings
5. Tuples
4. What are three internal key-attributes of a value-variable in Python? Explain with example.
The three internal key-attributes of a value-variable in Python are:
1. Type
2. Value
3. Id
1) type :-
Example = >>>a = 5
>>>type(a)
<class ‘int’>
2)value :-
It is data item contained in the object .it can use by print statement.
>>>print (a)
‘python’
3)id :-
It refers the memory location of the object. It can use by function id()
Example = >>>a = 8
>>>id (a)
308829545
5. Is it true that if two objects return True for is operator, they will also return True for ==
operator?
yes, it is always true.
6.Are these values equal? Why/why not?
1. 20 and 20.0
2. 20 and int(20)
3. str(20) and str(20.0)
4. 'a' and "a"
1.The type of 20 is int whereas the type of 20.0 is float so they are two different objects. Both
have the same value of 20. So, as values are same equality (==) operator return True but as
objects are different is operator returns False.
2.The value and type of both 20 and int(20) are the same and both point to the same object so
both equality (==) and is operator returns True.
1. For str(20) and str(20.0), both equality (==) and is operator returns False as their values
are different and they point to two different objects.
2. For 'a' and "a", both equality (==) and is operator returns True as their values are same
7.What is the difference between implicit type conversion and explicit type conversion?
Example: Example:
a, b = 5, 25.5 a, b = 5, 25.5
c=a+b c = int(a + b)
8. Two objects (say a and b) when compared using == ,return True. But Python gives False when compared
using is operator. Why? (i.e., a == b is True but why is a is b False?)
As equality (==) operator returns True, it means that a and b have the same value but as is operator
returns False, it means that variables a and b point to different objects in memory. For example, consider
the below Python statements:
>>> a = 'abc'
>>> b = input("Enter a string: ")
Enter a string: abc
>>> a == b
True
>>> a is b
False
Here, both a and b have the same value 'abc' but they point to different objects.
10. Evaluate the following for each expression that is successfully evaluated, determine its value and type
for unsuccessful expression, state the reason.
(a) len("hello") == 25/5 or 20/10
(b) 3 < 5 or 50/(5 - (3 + 2))
(c) 50/(5 - (3 + 2)) or 3 < 5
(d) 2 * (2 * (len("01")))
Answer
⇒ 5 == 25/5 or 20/10
(a) len("hello") == 25/5 or 20/10
⇒ 5 == 5 or 2
⇒ True or 2
⇒ True
The type of result is Boolean.
⇒ True or 50/(5 - (3 + 2)) [∵ first operand is True, second operand is not evaluated so no division by 0
(b) 3 < 5 or 50/(5 - (3 + 2))
⇒ True
error happens]
⇒ 50/(5 - 5) or 3 < 5
(c) 50/(5 - (3 + 2)) or 3 < 5
⇒ 50/0 or 3 < 5
⇒ Division by Zero Error
As the denominator of or operator's first operand is 0, Division by Zero error occurs.
⇒ 2 * (2 * 2)
(d) 2 * (2 * (len("01")))
⇒2*4
⇒8
The type of result is Integer.
11. A program runs to completion but gives an incorrect result. What type of error would have caused it?
Answer
Logical errors can make a program run till completion but give incorrect result.
12. In Python, strings are immutable while lists are mutable. What is the difference?
Answer
In Python, strings are immutable means that individual letter assignment for strings is not allowed. For
example:
name='hello'
name[0] = 'p'
The above Python code will cause an error as we are trying to assign some value to an individual letter of a
string.
Lists are mutable in Python means that we can assign values to individual elements of a list. For example:
a = [1, 2, 3, 4, 5]
a[0] = 10
The above Python code will work correctly without any errors as Lists are mutable in Python.
13. How does the // operator differ from the / operator? Give an example of where // would be needed.?
Answer
The Division operator (/) divides its first operand by second operand and always returns the result as a
float value whereas Floor Division operator (//) divides its first operand by second operand and truncates
the fractional part of the result and returns it as an int value. Floor Division operator is useful in situations
where we only need the integer part of the division operation result.
14. What are main error types? Which types are most dangerous and why?
Answer
The types of errors are:
1. Compile Time Errors (Syntax errors and Semantic Errors)
2. Runtime Errors
3. Logical Errors
Logical Errors are the most dangerous as they are hardest to prevent, find and fix.
Syntax Error
Semantics Error
Syntax errors occurs when the rules of the Semantic errors occur when the statement
programming language are violated. are not meaningful.
Example:
Example: x*y=z
x = false
17. Differentiate between a syntax error and a logical error in a python program. When is each type of error
likely to be found?
Syntax Errors occur when we violate the rules of Logical Errors occur due to our mistakes in
writing the statements of the programming programming logic.
language.
Program fails to compile and execute. Program compiles and executes but doesn't
give the desired output.