Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Applications, Class 10, CBSE
Aashna has written following code :
a = 5
b = '5'
c = "5"
d = 5.0
(a) What are the data types of a, b, c, d ?
(b) Are a, b, c, d storing exactly the same values ? Why/why not ?
(c) What will be the output of following code based on values of a, b, c and d ?
print a == b
print b == c
print a == d
(d) Discuss the reasons behind the output of part (c).
(a) The data types are as follows:
a | int |
b | String |
c | String |
d | float |
(b) No, a, b, c, and d are not storing exactly the same values. While the values of b and c look like integers (numerical 5), they are enclosed within quotes, making them strings. On the other hand, a is an integer number, and d is a floating-point number with a decimal point.
So, although they all represent the number 5, they are stored as different data types.
(c) The output of the following code will be :
False
True
True
(d) The reasons behind the output of part c are as follows:
1. print a == b
This comparison checks if the value of a is equal to the value of b. Even though the values of a and b are both 5, they are not of the same data type (int vs. str). Python strictly checks for both value and type equality in this case, so the comparison evaluates to False.
2. print b == c
This comparison checks if the value of b is equal to the value of c. Since both b and c are strings with the same content ("5"), the comparison evaluates to True.
3. print a == d
This comparison checks if the value of a is equal to the value of d. Although they have different data types (int vs. float), their numerical values are the same (both represent 5). Python performs type coercion in this case and considers them equal, so the comparison evaluates to True.
Anant has written following expressions in Python 2.7 :
a = - 4 % 1.5
b = - 4 // 1.5
c = 4.0 / 5
d = 4 / 5
(a) Find out the values stored by a, b, c and d.
(b) What are the datatypes of a, b, c and d ?
(c) Why are the values of c and d not the same.
(a) The values stored in a, b, c, d are as follows:
Variable | Value | Reason |
---|---|---|
a | -0.5 | Remainder of -4 divided by 1.5 is -0.5 |
b | -3.0 | Floor division of -4 by 1.5 is -3.0 as floor division divides and truncates the fractional part from the result. |
c | 0.8 | Division of 4.0 by 5 is 0.8, which is a floating-point value |
d | 0 | Division of 4 by 5 is 0, and the fractional part is truncated |
(b) The data types of a, b, c and d are as follows:
Variables | Datatypes |
---|---|
a | float |
b | float |
c | float |
d | int |
(c) The values of 'c' and 'd' are not the same because of the difference in the division operation performed in each case:
'c' is the result of dividing two floating-point numbers, which preserves the decimal point and gives a floating-point result. 'd' is the result of dividing two integers, which truncates the fractional part and gives an integer result.
Which statement/function will you use to display something on screen ?
Reason — The print statement is used to display something on screen.
Which statement/function will you use to get value for a variable from keyboard ?
raw_input
Reason — The raw_input function is used to get value for a variable from keyboard.
Which of these is not a Python data type ?
NUMBER
Reason — NUMBER is not a Python data type. Numbers are stored using int, float and long data types.
In the following code a = '5'
. a is a ............... :
string
Reason — A string literal is always enclosed in single quotes or double quotes.
What is the output of following code ?
print 8 >= 8
True
Reason — a >= b
compares a and b and returns true if a is greater than or equal to b, else returns false. Since 8 is equal to 8, the given expression results in 'true'.
What is the output of following code ?
print "8 >= 8"
8 >= 8
Reason — Since 8 >= 8
is enclosed in quotes, it is treated as a String and printed on the output terminal as it is.
What will be the output of following code ?
print(8 >= 8)
True
Reason — The given expression (8 >= 8)
results in true as 8 is equal to 8. Thus, 'true' is printed on the output terminal.
What will be the output of
print 3*2**2
12
Reason — The given expression 3*2**2
is evaluated as follows:
= 3 * 2 ** 2
= 3 * 4 (Since exponentiation (**) has higher precedence than multiplication (*))
= 12
Thus, 12 is printed as output.
What will be the output of
print (3*2)**2
36
Reason — The given expression (3*2)**2
is evaluated as follows:
= (3 * 2) ** 2
= 6 ** 2 (Since parentheses has higher precedence than exponentiation (**))
= 36
Thus, 36 is printed as output.
Which of the following has the highest precedence in an expression ?
Parenthesis (())
Reason — Parenthesis (()) has the highest precedence in an expression.
What are tokens in Python ? How many types of tokens are allowed in Python ? Examplify your answer.
The smallest individual unit in a program is known as a Token. Python has following tokens:
How are keywords different from identifiers?
Keywords are reserved words carrying special meaning and purpose to the language compiler/interpreter. For example, if, elif, etc. are keywords. Identifiers are user defined names for different parts of the program like variables, objects, classes, functions, etc. Identifiers are not reserved. They can have letters, digits and underscore. They must begin with either a letter or underscore. For example, _chk, chess, trail, etc.
What are literals in Python ? How many types of literals are allowed in Python ?
Literals are data items that have a fixed value. The different types of literals allowed in Python are:
What are operators ? What is their function ? Give examples of some unary and binary operators.
Operators are tokens that trigger some computation/action when applied to variables and other objects in an expression.
Unary plus (+), Unary minus (-), Bitwise complement (~), Logical negation (not) are a few examples of unary operators.
Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division (/).
What all components can a Python program contain ?
A Python program can contain various components like expressions, statements, comments, functions, blocks and indentation.
What are data types ? How are they important ?
Data types are used to identify the type of data a memory location can hold and the associated operations of handling it.
The data that we deal with in our programs can be of many types like character, integer, real number, string, boolean, etc. hence programming languages including Python provide ways and facilities to handle all these different types of data through data types. The data types define the capabilities to handle a specific type of data such as memory space it allocates to hold a certain type of data and the range of values supported for a given data type, etc.
How many integer types are supported by Python ? Name them.
Two integer types are supported by Python. They are:
Write a program to input a number n and print n, n2, n3.
n = int(input("Enter n: "))
n2, n3 = n ** 2, n ** 3
print("n =", n)
print("n^2 =", n2)
print("n^3 =", n3)
Enter n: 2
n = 2
n^2 = 4
n^3 = 8
What is meant by token ? Name the tokens available in Python.
The smallest individual unit in a program is known as a Token. Python has following tokens:
What are keywords ? Can keywords be used as identifiers ?
Keywords are words having special meaning reserved by programming language.
No, keywords cannot be used as identifiers.
What is an identifier ? What are the identifier forming rules of Python ?
Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program viz variables, objects, classes, functions, lists, dictionaries etc. For example, Myfile, _DS, _name_01, H34CAP etc.
The identifier forming rules of Python are:
What are literals ? How many types of literals are available in Python ?
Literals are data items that have a fixed value. The different types of literals allowed in Python are:
How many types of integer literals are allowed in Python ? How are they written ?
The types of integer literals allowed in Python are as follows:
What kind of literals are these : 011, 0X2A, 17, 014, 0XBC1 ?
011 | Octal Literal |
0X2A | Hexadecimal Literal |
17 | Decimal Literal |
014 | Octal Literal |
0XBC1 | Hexadecimal Literal |
How are nongraphic characters represented in Python ?
Python allows us to have certain non-graphic characters in String values. These non-graphic characters can be represented by using escape sequences — represented by a backslash ( \ ) followed by one or more characters.
Some examples of non-graphic characters and their escape sequences are as follows:
Escape Sequence | Non-Graphic Character |
---|---|
\a | ASCII Bell (BEL) |
\n | New line character |
\t | Horizontal tab |
What are string-literals in Python ? How many ways, can you create String literals in Python ?
We can form string literals by enclosing text in both forms of quotes — single quotes or double quotes. For example, 'Astha', "Rizwan", 'Hello World', "112FB0291", etc.
Python allows us to have two string types :
What is meant by a floating-point literal in Python ? How many ways can a floating literal be represented into ?
Floating point literals represent real numbers and are written with a decimal point dividing the integer and fractional parts.
Floating literals are represented in Python in two forms —
What are the two Boolean literals in Python ?
The two Boolean literals in Python are true and false.
What kind of program elements are these: 'a', 4.38925, "a", main( ) ?
'a' | String Literal |
4.38925 | Floating point Literal |
"a" | String Literal |
main( ) | Function |