Type A : Short Answer Questions/Conceptual
Questions
Question 1
What are tokens in Python ? How many types of tokens are allowed in
Python ? Examplify your answer.
Answer
The smallest individual unit in a program is known as a Token. Python
has following tokens:
1. Keywords — Examples are import, for, in, while, etc.
2. Identifiers — Examples are MyFile, _DS, DATE_9_7_77, etc.
3. Literals — Examples are "abc", 5, 28.5, etc.
4. Operators — Examples are +, -, >, or, etc.
5. Punctuators — ' " # () etc.
Question 2
How are keywords different from identifiers ?
Answer
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.
Question 3
What are literals in Python ? How many types of literals are allowed in
Python ?
Answer
Literals are data items that have a fixed value. The different types of
literals allowed in Python are:
6. String literals
7. Numeric literals
8. Boolean literals
9. Special literal None
10. Literal collections
Question 4
Can nongraphic characters be used in Python ? How ? Give examples to
support your answer.
Answer
Yes, nongraphic characters can be used in Python with the help of
escape sequences. For example, backspace is represented as \b, tab is
represented as \t, carriage return is represented as \r.
Question 5
How are floating constants represented in Python ? Give examples to
support your answer.
Answer
Floating constants are represented in Python in two forms — Fractional
Form and Exponent form. Examples:
11. Fractional Form — 2.0, 17.5, -13.0, -0.00625
12. Exponent form — 152E05, 1.52E07, 0.152E08, -0.172E-3
Question 6
How are string-literals represented and implemented in Python ?
Answer
A string-literal is represented as a sequence of characters surrounded
by quotes (single, double or triple quotes). String-literals in Python are
implemented using Unicode.
Question 7
Which of these is not a legal numeric type in Python ? (a) int (b) float (c)
decimal.
Answer
decimal is not a legal numeric type in Python.
Question 8
Which argument of print( ) would you set for:
(i) changing the default separator (space) ?
(ii) printing the following line in current line ?
Answer
(i) sep
(ii) end
Question 9
What are operators ? What is their function ? Give examples of some
unary and binary operators.
Answer
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 (/).
Question 10
What is an expression and a statement ?
Answer
An expression is any legal combination of symbols that represents a
value. For example, 2.9, a + 5, (3 + 5) / 4.
A statement is a programming instruction that does something i.e.
some action takes place. For example:
print("Hello")
a = 15
b = a - 10
Question 11
What all components can a Python program contain ?
Answer
A Python program can contain various components like expressions,
statements, comments, functions, blocks and indentation.
Question 12
What do you understand by block/code block/suite in Python ?
Answer
A block/code block/suite is a group of statements that are part of
another statement. For example:
if b > 5:
print("Value of 'b' is less than 5.")
print("Thank you.")
Question 13
What is the role of indentation in Python ?
Answer
Python uses indentation to create blocks of code. Statements at same
indentation level are part of same block/suite.
Question 14
What are variables ? How are they important for a program ?
Answer
Variables are named labels whose values can be used and processed
during program run. Variables are important for a program because
they enable a program to process different sets of data.
Question 15
What do you understand by undefined variable in Python ?
Answer
In Python, a variable is not created until some value is assigned to it. A
variable is created when a value is assigned to it for the first time. If we
try to use a variable before assigning a value to it then it will result in an
undefined variable. For example:
print(x) #This statement will cause an error for undefined variable
x
x = 20
print(x)
The first line of the above code snippet will cause an undefined variable
error as we are trying to use x before assigning a value to it.
Question 16
What is Dynamic Typing feature of Python ?
Answer
A variable pointing to a value of a certain type can be made to point to
a value/object of different type.This is called Dynamic Typing. For
example:
x = 10
print(x)
x = "Hello World"
print(x)
Question 17
What would the following code do : X = Y = 7 ?
Answer
It will assign a value of 7 to the variables X and Y.
Question 18
What is the error in following code : X, Y = 7 ?
Answer
The error in the above code is that we have mentioned two variables X,
Y as Lvalues but only give a single numeric literal 7 as the Rvalue. We
need to specify one more value like this to correct the error:
X, Y = 7, 8
Question 19
Following variable definition is creating problem X = 0281, find reasons.
Answer
Python doesn't allow decimal numbers to have leading zeros. That is
the reason why this line is creating problem.
Question 20
"Comments are useful and easy way to enhance readability and
understandability of a program." Elaborate with examples.
Answer
Comments can be used to explain the purpose of the program,
document the logic of a piece of code, describe the behaviour of a
program, etc. This enhances the readability and understandability of a
program. For example:
# This program shows a program's components
# Definition of function SeeYou() follows
def SeeYou():
print("Time to say Good Bye!!")
# Main program-code follows now
a = 15
b = a - 10
print (a + 3)
if b > 5: # colon means it's a block
print("Value of 'a' was more than 15 initially.")
else:
print("Value of 'a' was 15 or less initially.")
SeeYou() # calling above defined function SeeYou()