Class 12 CS Chapter 1 Python Revision Tour I Solutions

Contents show

Class 12 CS Chapter 1 Python Revision Tour I

Python Revision Tour I
Class 12 CS Chapter 1 Python Revision Tour I Solutions

Q 1. What is the difference between a keyword and an identifier ?


Ans. Keyword is a special word that has a special meaning and purpose. Keywords are reserved and are a few. For example, if, elif, else etc. are keywords.


Identifier is the user-defined name given to part of a program viz. variable, object, function etc. Identifiers are not reserved. These are defined by the user but they can have letters, digits and a symbol underscore. They must begin with either a letter or underscore. For instance, _chk, chess, trial etc. are identifiers in Python.

Q 2. What are literals in Python ? How many types of literals are allowed in Python ?


Ans. Literals mean constants i.e., the data items that never change their value during a program run. Python allow five types of literals :
(i) string literals
(ii) Numeric literals
(iii) Boolean literals
(iv) Special Literal None
(v) Literal Collections like tuples, lists etc.

Q 3. How many ways are there in Python to represent an integer literal ? (Python Revision Tour I)


Ans. Python allows three types of integer literal :


(a) Decimal (base 10) integer literals


An integer literal consisting of a sequence of digits is taken to be decimal integer literal unless it begins with 0 (digit zero)

For instance, 1234, 41, +97, -17 are decimal integer literals.


(b) Octal (base 10) integer literals


A sequence of digits starting with 0 (digit zero) is taken to be an octal integer.

For instance, decimal integer 8 will be written as 010 as octal integer.

Class 12 CS Chapter 1 Python Revision Tour I Solutions

and decimal integer 12 be written as 014 as octal integer

Class 12 CS Chapter 1 Python Revision Tour I Solutions


(c) Hexadecimal (base 16) integer literals


A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer.
Dor instance, decimal 12 will be written as 0XC as hexadecimal integer.
Thus number 12 will be written either as 12 (as decimal), 014 (as octal) and 0XC (as hexadecimal).

Q 4. How many types of strings are supported in Python ?


Ans. Python allows two string types :
(i) Single line Strings Strings that are terminated in single line
(ii) Multiline Strings Strings storing multiple lines of text.

Q 5. What is None literal in Python ?


Ans. Python has one special literal called None.
The None literal is used to indicate something that has not yet been created. It is a legal empty value in Python.

Q 6. The following code is not giving desired output. We want to input value as 20 and obtain output as 40. Could you pinpoint the problem ?

Number = input("Enter Number")
DoubleTheNumber = Number*2
print(DoubleTheNumber)


Ans. The problem is that input( ) returns value as a sting, so the input value 20 is returned string ’20’ and not as integer 20. So the output is 2020 in place of required output 40.
Also Print is not legal statement of Python; it should be print.

Q 7. Why is following code giving errors ?

name =  "Rehman"
print("Greetings !!!"
       print("Hello", name)
       print("How do you do ?")

Ans. The problem with above code is inconsistent indentation. In Python, we cannot indent a statement unless it is inside a suite and we can indent only as much as required.
Thus, corrected code will be :

name =  "Rehman"
print("Greetings !!!"
print("Hello", name)
print("How do you do ?")

Q 8. What are data types ? What are Python’s built-in core data types ?


Ans. The real life data is of many types. So to represent various types of real-life dat, programming languages provide ways and facilities to handle these, which are known as data types.
Python’s built-in core data types belong to :

  • Numbers (integer, floating-point, complex numbers, Boolean’s)
  • String
  • List
  • Dictionary
  • Tuple

Q 9. Which data types of Python handle Numbers ?


Ans. Python provides following data types of handle numbers (version 3.x) :
(i) Plain integers
(ii) Long integers
(iii) Boolean
(iv) Floating-point numbers
(v) Complex numbers

Q 10. Why is Boolean considered a sub-type of integers ? (Python Revision Tour I)


Ans. Boolean values True and False internally map to integers 1 and 0. That is, internally True is considered equal to 1 and False as equal to 0 (zero). When 1 and 0 are converted to Boolean through bool( ) function, they return True and False. That is why Booleans are treated as a subtype of integers.

Q 11. What is the role of comments and indentation in a program ?


Ans. Comments provide explanatory notes to the readers of the pogram. Compiler or interpreter ignores the comments but they are useful for specifying additional descriptive information regarding the code and logic of the program.
Indentation makes the program more readable and presentable. Its main role is to highlight nesting of groups of control statements.

(Python Revision Tour I)

Q 12. What is a statement ? What is the significance of an empty statement ?


Ans. A statement is an instruction given to the computer to perform any kind of action. An empty statement is useful in situations where the code requires a statement but logic does not. To fill these two requirements simultaneously, empty statement is used.
Python offers pass statement as an empty statement.

Q 13. If you are asked to label the Python loops as determinable or non-determinable, which label would you give to which loop ? Justify your answer.


Ans. The ‘for loop’ can be labelled as determinable loop as number of its iterations can be determined beforehand as the size of the sequence, it is operating upon.
The ‘while loop’ can be labelled as non-determinable loop, as its number of iterations cannot be determined beforehand. Its iterations depend upon upon the result of a test-condition, which cannot be determined beforehand.

Q 14. There are two types of else clauses in Python. What are these two types of else clauses ?


Ans. The two types of Python else clauses are :


(a) else in an if statement (b) else in a loop statement


The else clause of an if statement is executed when the condition of the if statement results into false. The else clause of a loop is executed when the loop is terminating normally i.e., when its test-condition has gone false for a while loop or when the for loop has executed for the last value in sequence.

Q 15. Write a program that asks from users some numbers until string ‘done’ is typed. The program should print the sum of all numbers entered.

Ans.

#Unknown Number of Numbers to Sum
total = 0
s = input('Enter a number of "done": ')
while s != 'done' :
        num = int(s)
        total = total + num
        s = input('Enter a number or "done": ')
print('The sum of entered number is', total)

Read More :

Python Revision Tour II Solutions

Python Revision Tour I