Python Variables and Operators: book back answers
22 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Python Variables and OperatorsChoose the best answer (1 Marks)
Who developed Python ?
- A) Ritche
- B) Guido Van Rossum
- C) Bill Gates
- D) Sunder Pitchai
Try it, then show the answerAnswer
B) Guido Van Rossum
The chapter identifies Guido Van Rossum as the creator of Python.
From the book: “Python is a general purpose programming language created by Guido Van Rossum”
The Python prompt indicates that Interpreter is ready to accept instruction.
- A) >>>
- B) <<<
- C) #
- D) <<
Try it, then show the answerAnswer
A) >>>
The Python prompt shown in the chapter is >>>.
From the book: “The prompt (>>>) indicates that Interpreter is ready to accept instructions.”
Which of the following shortcut is used to create new Python Program ?
- A) Ctrl + C
- B) Ctrl + F
- C) Ctrl + B
- D) Ctrl + N
Try it, then show the answerAnswer
D) Ctrl + N
The chapter says to choose File → New File or press Ctrl + N to create a script.
From the book: “Choose File → New File or press Ctrl + N in Python shell window.”
Which of the following character is used to give comments in Python Program ?
- A) #
- B) &
- C) @
- D) $
Try it, then show the answerAnswer
A) #
Python comments begin with the hash symbol.
From the book: “In Python, comments begin with hash symbol (#).”
This symbol is used to print more than one item on a single line.
- A) Semicolon(;)
- B) Dollor($)
- C) comma(,)
- D) Colon(:)
Try it, then show the answerAnswer
C) comma(,)
The comma is used as a separator in print() to print more than one item.
From the book: “Comma ( , ) is used as a separator in print ( ) to print more than one item.”
Which of the following is not a token ?
- A) Interpreter
- B) Identifiers
- C) Keyword
- D) Operators
Try it, then show the answerAnswer
A) Interpreter
The chapter lists identifiers, keywords, operators, delimiters and literals as token types; interpreter is not one of them.
From the book: “The normal token types are 1) Identifiers, 2) Keywords, 3) Operators, 4) Delimiters and 5) Literals.”
Which of the following is not a Keyword in Python ?
- A) break
- B) while
- C) continue
- D) operators
Try it, then show the answerAnswer
D) operators
break, while and continue appear in the chapter’s keyword list. Operators are a token category, not a keyword listed there.
From the book: “Keywords are special words used by Python interpreter to recognize the structure of program.”
Which operator is also called as Comparative operator?
- A) Arithmetic
- B) Relational
- C) Logical
- D) Assignment
Try it, then show the answerAnswer
B) Relational
The chapter states that a relational operator is also called a comparative operator.
From the book: “A Relational operator is also called as Comparative operator which checks the relationship between two operands.”
Which of the following is not Logical operator?
- A) and
- B) or
- C) not
- D) Assignment
Try it, then show the answerAnswer
D) Assignment
The logical operators are and, or and not. Assignment is a separate operator category.
From the book: “There are three logical operators they are and, or and not.”
Which operator is also called as Conditional operator?
- A) Ternary
- B) Relational
- C) Logical
- D) Assignment
Try it, then show the answerAnswer
A) Ternary
The chapter names the ternary operator as the conditional operator.
From the book: “Ternary operator is also known as conditional operator”
Part - II — Answer the following questions : (2 Marks)
What are the different modes that can be used to test Python Program ?
Try it, then show the answerAnswer
Interactive mode and Script mode.
In interactive mode, code is typed at the Python prompt and results appear immediately. In script mode, programs are written and stored as separate .py files for execution.
From the book: “In Python, programs can be written in two ways namely Interactive mode and Script mode.”
Write short notes on Tokens.
Try it, then show the answerAnswer
Tokens are elementary lexical components into which Python breaks each logical line. The token types are identifiers, keywords, operators, delimiters and literals.
Whitespace separation is necessary between tokens.
From the book: “Python breaks each logical line into a sequence of elementary lexical components known as Tokens.”
What are the different operators that can be used in Python ?
Try it, then show the answerAnswer
Arithmetic, Relational, Logical, Assignment and Conditional (Ternary) operators.
These are the operator types described in the chapter.
From the book: “Operators are categorized as Arithmetic, Relational, Logical, Assignment etc.”
What is a literal? Explain the types of literals ?
Try it, then show the answerAnswer
A literal is raw data given to a variable or constant. The types of literals are Numeric, String and Boolean.
Numeric literals include integer, float and complex forms. String literals are character sequences in quotes, while Boolean literals are True or False.
From the book: “Literal is a raw data given to a variable or constant in Python, there are various types of literals.”
Write short notes on Exponent data?
Try it, then show the answerAnswer
Exponent data contains a decimal digit part, a decimal point and an exponent part followed by one or more digits. Examples are 12.E04 and 24.e04.
These forms are given as examples of exponent data in the chapter.
From the book: “An Exponent data contains decimal digit part, decimal point, exponent part followed by one or more digits.”
Part - III — Answer the following questions : (3 Marks)
Write short notes on Arithmetic operator with examples.
Try it, then show the answerAnswer
An arithmetic operator takes two operands and performs a calculation. Python arithmetic operators include +, -, *, /, %, ** and //. For example, if a=100 and b=10, a+b gives 110, a/b gives 10.0 and a//30 gives 3.
These operators perform calculations such as addition, subtraction, multiplication, division, modulus, exponentiation and floor division.
From the book: “An arithmetic operator is a mathematical operator that takes two operands and performs a calculation on them.”
What are the assignment operators that can be used in Python?
Try it, then show the answerAnswer
The assignment operators are =, +=, -=, *=, /=, %=, **= and //=.
The simple assignment operator assigns a value to a variable; the compound operators perform an operation and assign the result back to the variable.
From the book: “There are various compound operators in Python like +=, -=, *=, /=, %=, **= and //= are also available.”
Explain Ternary operator with examples.
Try it, then show the answerAnswer
The ternary operator evaluates a condition in one line and chooses a value depending on whether the condition is true or false. Its syntax is Variable Name = [on_true] if [Test expression] else [on_false]. Example: min = a if a < b else b.
For a=30 and b=20, the example selects b because a < b is false, so min becomes 20.
From the book: “Ternary operator is also known as conditional operator that evaluate something based on a condition being true or false.”
Write short notes on Escape sequences with examples.
Try it, then show the answerAnswer
The backslash is an escape character used to represent certain characters in Python strings. For example, \t represents a tab, \n a newline, and \r a carriage return; \\ represents a backslash and \' a single quote.
Escape sequences let strings represent whitespace and special quote or backslash characters.
From the book: “It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.”
What are string literals? Explain.
Try it, then show the answerAnswer
A string literal is a sequence of characters surrounded by quotes. Python supports single, double and triple quotes; a single character in single or double quotes is also a string literal, and triple quotes can be used for multiline strings.
The chapter treats character literals as string literals and shows triple-quoted strings for multiline text.
From the book: “In Python a string literal is a sequence of characters surrounded by quotes.”
Part - IV — Answer the following questions : (5 Marks)
Describe in detail the procedure Script mode programming.
Try it, then show the answerAnswer
Script mode is used to create and edit a Python source file. A script is a text file containing Python statements, which can be saved and executed whenever required.
- Create a new script: In the Python shell window, choose File → New File or press Ctrl + N. An untitled, blank script text editor will appear.
- Enter the program: Type the required Python statements in the script editor. The script can be edited, and its code can be reused without retyping.
- Save the script: Choose File → Save or press Ctrl + S. In the Save As dialog box, select a location and enter a file name; Python files are saved with the .py extension by default.
- Execute the script: Choose Run → Run Module or press F5 to run the saved program.
- Correct errors: If the code contains an error, it is shown in red in the IDLE window, along with its type. Return to the script editor, make the corrections, save the file, and execute it again.
- View the output: When the code has no errors, its output appears in the Python IDLE window.
Example: The chapter’s sample script assigns values to a and b, calculates c = a+b, and displays the sum using print ("The Sum=", c).
Thus, Script mode lets us write, save, edit, and run Python programs as reusable .py files.
From the book: “Python Scripts are reusable code. Once the script is created, it can be executed again and again without retyping.”
Discuss in detail about Tokens in Python
Try it, then show the answerAnswer
In Python, each logical line is broken into elementary lexical components called tokens. Tokens are the basic components used to form Python statements.
- Types of tokens: The normal token types are identifiers, keywords, operators, delimiters and literals.
- Identifiers: An identifier names a variable, function, class, module or object. It must begin with an alphabet or underscore; it may contain digits, is case sensitive, and must not be a keyword.
- Keywords: Keywords are special words recognised by the Python interpreter to identify program structure. Since they have specific meanings, they cannot be used for another purpose.
- Operators: Operators are special symbols that represent computations or conditional matching. They act on operands and include arithmetic, relational, logical and assignment operators.
- Delimiters: Delimiters mark boundaries between separate regions in expressions and other data. Python uses symbols such as parentheses, brackets, braces, commas, colons and semicolons.
- Literals: A literal is raw data given to a variable or constant. Python has numeric, string and Boolean literals.
- Whitespace between tokens: Whitespace separation is necessary between tokens. Spaces or tabs help separate tokens in Python code.
Example: The chapter gives valid identifiers such as Sum, total_marks, regno and num1; invalid identifiers include 12Name, name$, total-mark and continue.
Thus, tokens are the lexical building blocks of Python programs, with each type serving a specific purpose.
From the book: “The normal token types are 1) Identifiers, 2) Keywords, 3) Operators, 4) Delimiters and 5) Literals.”