Tamilnadu State Board New Syllabus Samacheer Kalvi 12th Computer Science Guide Pdf Chapter 5 Python -Variables and Operators Text Book Back Questions and Answers, Notes.

## Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 5 Python -Variables and Operators

### 12th Computer Science Guide Python -Variables and Operators Text Book Questions and Answers

I. Choose the best answer (1 Marks)

Question 1.
Who developed Python ?
a) Ritche
b) Guido Van Rossum
c) Bill Gates
d) Sunder Pitchai
b) Guido Van Rossum

Question 2.
The Python prompt indicates that Interpreter is ready to accept instruction.
a) > > >
b) < < <
c) #
d) < <
a) > > >

Question 3.
Which of the following shortcut is used to create a new Python Program?
a) Ctrl + C
b) Ctrl + F
c) Ctrl + B
d) Ctrl + N
d) Ctrl + N

Question 4.
Which of the following character is used to give comments in Python Program?
a) #
b) &
c) @
d) $Answer: a) # Question 5. This symbol is used to print more than one item on a single line. a) Semicolon(;) b) Dollor($)
c) commaQ
d) Colon(:)
c) commaQ

Question 6.
Which of the following is not a token?
a) Interpreter
b) Identifiers
c) Keyword
d) Operators
a) Interpreter

Question 7.
Which of the following is not a Keyword in Python?
a) break
b) while
c) continue
d) operators
d) operators

Question 8.
Which operator is also called a Comparative operator?
a) Arithmetic
b). Relational
c) Logical
d) Assignment
b) Relational

Question 9.
Which of the following is not a Logical operator?
a) and
b) or
c) not
d) Assignment
d) Assignment

Question 10.
Which operator is also called a Conditional operator?
a) Ternary
b) Relational
c) Logical
d) Assignment
a) Ternary

II. Answer the following questions (2 Marks)

Question 1.
What are the different modes that can be used to test Python Program?
In Python, programs can be written in two ways namely Interactive mode and Script mode. The Interactive mode allows us to write codes in Python command prompt (>>>) whereas in script mode programs can be written and stored as separate file with the extension .py and executed. Script mode is used to create and edit python source file.

Question 2.
Write short notes on Tokens.
Python breaks each logical line into a sequence of elementary lexical components known as Tokens.
The normal token types are

1. Identifiers
2. Keywords
3. Operators
4. Delimiters
5. Literals

Question 3.
What are the different operators that can be used in Python?
In computer programming languages operators are special symbols which represent computations, conditional matching etc. The value of an operator used is called operands. Operators are categorized as Arithmetic, Relational, Logical, Assignment etc.

Question 4.
What is a literal? Explain the types of literals?

• Literal is raw data given in a variable or constant.
• In Python, there are various types of literals.
1. Numeric
2. String
3. Boolean

Question 5.
Write short notes on Exponent data.
An Exponent data contains decimal digit part, decimal point, exponent part followed by one or more digits.
12.E04, 24.e04 # Exponent data

III. Answer the following questions (3 Marks)

Question 1.
Write short notes on the Arithmetic operator with examples.

• An arithmetic operator is a mathematical operator that takes two operands and performs a calculation on them.
• They are used for simple arithmetic.
• Most computer languages contain a set of such operators that can be used within equations to perform different types of sequential calculations.
• Python supports the following Arithmetic operators.
 Operator-Operation Examples Result Assume a=100 and b=10 Evaluate the following expressions + (Addition) > > > a+b 110 – (Subtraction) > > > a-b 90 * (Multiplication) > > > a*b 1000 / (Division) > > > a/b 10.0 % (Modulus) > > > a% 30 10 ** (Exponent) > > > a**2 10000 / / (Floor Division) > > > a// 30 (Integer Division) 3

Question 2.
What are the assignment operators that can be used in Python?

• In Python,= is a simple assignment operator to assign values to variables.
• There are various compound operators in Python like +=, -=, *=, /=,%=, **= and / / = are also available.
• Let = 5 and = 10 assigns the values 5 to and 10 these two assignment statements can also be given a =5 that assigns the values 5 and 10 on the right to the variables a and b respectively.
 Operator Description Example Assume x=10 = Assigns right side operands to left variable »> x=10 »> b=”Computer”
 += Added and assign back the result to left operand i.e. x=30 »> x+=20 # x=x+20 –= Subtracted and assign back the result to left operand i.e. x=25 >>> x-=5 # x=x-5

Question 3.
Explain the Ternary operator with examples.
Conditional operator:
Ternary operator is also known as a conditional operator that evaluates something based on a condition being true or false. It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.
The Syntax conditional operator is,
Variable Name = [on – true] if [Test expression] else [on – false]
Example:
min = 50 if 49 < 50 else 70 # min = 50 min = 50 if 49 > 50 else 70 # min = 70

Question 4.
Write short notes on Escape sequences with examples.

• In Python strings, the backslash “\” is a special character, also called the “escape” character.
• It is used in representing certain whitespace characters: “t” is a tab, “\n\” is a new line, and “\r” is a carriage return.
• For example to print the message “It’s raining”, the Python command is > > > print (“It \ ‘s raining”)
• output:
It’s raining
 Escape sequence character Description Example Output w Backslash >>> print(“\\test”) \test Y Single-quote »> print(“Doesn\’t”) Doesn’t \” Double-quote »> print(“\”Python\””) “Python” \n New line pr in t (” Python “,” \ n”,” Lang..”) Python Lang.. \t Tab print(“Python”,”\t”,”Lang..”) Python Lang..

Question 5.
What are string literals? Explain.
String Literals:
In Python, a string literal is a sequence of characters surrounded by quotes. Python supports single, double and triple quotes for a string. A character literal is a single character surrounded by single or double-quotes. The value with triple – the quote is used to give multi-line string literal.
Strings = “This is Python”
char = “C”
multiline _ str = “This is a multiline string with more than one line code”.

IV. Answer the following questions (5 Marks)

Question 1.
Describe in detail the procedure Script mode programming

• Basically, a script is a text file containing the Python statements.
• Python Scripts are reusable code.
• Once the script is created, it can be executed again and again without retyping.
• The Scripts are editable

Creating Scripts in Python:

• Choose File → New File or press Ctrl + N in the Python shell window.
• An untitled blank script text editor will be displayed on the screen.
• Type the code in Script editor
a=100
b=350
c=a+b
print(“The Sum=”,c)

Saving Python Script:

• Choose File →Save or press Ctrl+S
• Now, Save As dialog box appears on the screen
• In the Save As dialog box, select the location where you want to save your Python code, and type the File name box Python files are by default saved with extension by Thus, while creating Python scripts using Python Script editor, no need to specify the file extension.
• Finally, ‘click Save button to save your Python script.

Executing Python Script:

• Choose Run-Run Module or Press F5
• If code has any error, it will be shown in red color in the IDLE window,, and Python describes the type of error occurred. To correct the errors, go back to Script editor, make corrections, save the file using Ctrl + S or File→Save and execute it again.
• For all error-free code, the output will appear in the IDLE window of Python.

Question 2.
Explain input () and print() functions with examples.
input () function:
In Python input() function is used to accept data as input at run time.
Syntax:
Variable = input (“prompt string”)

• where, prompt string in the syntax is a statement or message to the user, to know what input can be given.
• The input() takes whatever is typed from the keyboard and stores the entered data in the given variable.
• If prompt string is not given in input() no message is displayed on the screen, thus, the user will not know what is to be typed as input

Example:
> > > city=input(“Enter Your City: “)
> > > print(“I am from “, city)

• The input () accepts all data as string or characters but not as numbers.
• If a numerical value is entered, the input values should be explicitly converted into numeric data type.
• The int( ) function is used to convert string data as integer data explicitly.

Example:
x= int (input(“Enter Number 1: “))
y = int (input(“Enter Number 2: “))
print (“The sum =”, x+y)
Output
Enter Number 1: 34
Enter Number 2: 56
The sum = 90
print () function :
In Python, the print() function is used to display result on the screen. The syntax for print() is as follows:

Syntax:
print (“string to be displayed as output” )
print (variable)
print (“String to be displayed as output variable)
print (“String 1 “, variable, “String 2′”,variable, “String 3” )….)

Example
> > > print (“Welcome to Python Programming”)
Welcome to
Python Programming
> > > x= 5
> > > y= 6
> > > z=x+y
> > > print (z)
11
> > > print (“The sum =”,z)
The sum=11
> > > print (“The sum of”,x, “and “, y, “is “,z)
The sum of 5 and 6 is 11

• The print( ) evaluates the expressions before printing it on the monitor
• The print( ) displays an entire statement which specified within print ( )
• Comma (,) is used as a separator in print more than one time

Question 3.
Discuss in detail about Tokens in Python
Python breaks each logical line into a sequence of elementary lexical components known as Token.
The normal token types are

1. Identifiers
2. Keywords
3. Operators
4. Delimiters and
5. Literals.

Identifiers:

• An Identifier is a name used to identify a variable, function, class, module or object.
(A..Z or a..z) or underscore (_). Identifiers may contain digits (0 .. 9). „
• Python identifiers are case sensitive i.e. uppercase and lowercase letters are distinct. Identifiers must not be a python keyword.
• Python does not allow punctuation characters such as %,\$, @, etc., within identifiers.

Keywords:
Keywords are special words used by Python interpreters to recognize the structure of the program. As these words have specific meanings for interpreters, they cannot be used for any other purpose.

Operators:

• In computer programming languages operators are special symbols which represent computations, conditional matching, etc.
• The value of an operator used is called operands.
• Operators are categorized as Arithmetic, Relational, Logical, Assignment, etc. Value and variables when used with the operator are known as operands

Delimiters:
Python uses the symbols and symbol combinations as delimiters in expressions, lists, dictionaries, and strings
Following are the delimiters knew as operands.

Literals:
Literal is raw data given in a variable or constant. In Python, there are various types of literals.

• Numeric
• String
• Boolean

### 12th Computer Science Guide Python -Variables and Operators Additional Questions and Answers

I. Choose the best answer (1 Mark)

Question 1.
Python language was released in the year
a) 1992
b) 1994
c) 1991
d) 2001
c) 1991

Question 2.
CWI means ……………………………
Centrum Wiskunde & Information

Question 3.
In Python, How many ways programs can be written?
a) 4
b) 2
c) 3 ‘
d) many
b) 2

Question 4.
Find the wrong statement from the following.
(a) Python supports procedural approaches
(b) Python supports object-oriented approaches
(c) Python is a DBMS tool
(c) Python is a DBMS tool

Question 5.
Which mode displays the python code result immediately?
a) Compiler
b) Script
c) Interactive
d) program
c) Interactive

Question 6.
Which of the following command is used to execute the Python script?
a) Run → Python Module
b) File → Run Module
c) Run → Module Fun
d) Run → Run Module
d) Run → Run Module

Question 7.
The extension for the python file is ……………………………
(a) .pyt
(b) .pt
(c) .py
(d) .pyth
(c) .py

Question 8.
Which operator replaces multiline if-else in python?
a) Local
b) Conditional
c) Relational
d) Assignment
b) Conditional

Question 9.
Which of the following is a sequence of characters surrounded by quotes?
a) Complex
b) String literal
c) Boolean
d) Octal
b) String literal

Question 10.
What does prompt (>>>) indicator?
(a) Compiler is ready to debug
(c) Waiting for the Input data
(d) Interpreter is ready to accept Instructions
(d) Interpreter is ready to accept Instructions

Question 11.
In Python shell window opened by pressing.
a) Alt + N
b) Shift + N
c) Ctrl + N
d) Ctrl + Shift +N
c) Ctrl + N

Question 12.
In Python, comments begin with …………..
a) /
b) #
c)\
d) //
b) #

Question 13.
Which command is selected from the File menu creates a new script text editor?
(a) New
(b) New file
(c) New editor
(d) New Script file
(b) New file

Question 14.
Python uses the symbols and symbol combinations as ……………. in expressions
a) literals
b) keywords
c) delimiters
d) identifiers
c) delimiters

Question 15.
All data values in Python are ……………
a) class
b) objects
c) type
d) function
b) objects

Question 16.
…………………………… command is used to execute python script?
(a) Run
(b) Compile
(c) Run ? Run Module
(d) Compile ? Compile Run
(c) Run ? Run Module

Question 17.
Octal integer uses …………………………… to denote octal digits
(a) OX
(b) O
(c) OC
(d) Od
(b) O

Question 18.
(a) 0102
(b) 0876
(c) 0432
(d) 0X102
(d) 0X102

Question 19.
How many floating-point values are there is a complex number?
(a) 1
(b) 2
(c) 3
(d) 4
(b) 2

II. Answer the following questions (2 and 3 Marks)

Question 1.
Write a note on keywords. Give examples?
Keywords are special words used by Python interpreters to recognize the structure of the program. As these words have specific meanings for interpreters, they cannot be used for any other purpose. Eg, While, if.

Question 2.
What are keywords? Name any four keywords in Python.
Keywords are special words that are used by Python interpreters to recognize the structure of the program.

Question 3.
Write a note on the relational or comparative operator.

• A Relational operator is also called a Comparative operator which checks the relationship between two operands.
• If the relation is true, it returns True otherwise it returns False.

Question 4.
Write a short note on the comment statement.

• In Python, comments begin with hash symbol (#).
• The lines that begins with # are considered as comments and ignored by the Python interpreter.
• Comments may be single line or no multilines.
• The multiline comments should be enclosed within a set of # as given below.
# It is Single line Comment
# It is multiline comment which contains more than one line #

Question 5.
What are the key features of python?
Key features of Python:
It is a general-purpose programming language which can be used for both scientific and non – scientific programming
It is a platform-independent programming language.
The programs written in Python are easily readable and understandable

Question 6.
What are the uses of the logical operators? Name the operators.

• In python, Logical operators are used to performing logical operations on the given relational expressions.
• There are three logical operators they are and or not.

III. Answer the following questions (5 Marks)

Question 1.
Explain data types in python?
Python Data types:
All data values in Python are objects and each object or value has a type. Python has Built-in or Fundamental data types such as Numbers, String, Boolean, tuples, lists, and dictionaries.

Number Data type:
The built-in number objects in Python supports integers, floating-point numbers, and complex numbers.
Integer Data can be decimal, octal, or hexadecimal. Octal integers use O (both upper and lower case) to denote octal digits and hexadecimal integers use OX (both upper and lower case) and L (only upper case) to denote long integers.
Example:
102, 4567, 567 # Decimal integers
0102, o876, 0432 # Octal integers
0X102, oX876, 0X432 # Hexadecimal integers
34L, 523L # Long decimal integers
A floating-point data is represented by a sequence of decimal digits that includes a decimal point. An Exponent data contains decimal digit part, decimal point, exponent part followed by one or more digits.
Example :
123.34, 456.23, 156.23 # Floating point data
12.E04, 24.e04 # Exponent data
A complex number is made up of two floating-point values, one each for the real and imaginary parts.

Boolean Data type:
A Boolean data can have any of the two values: True or False.

Example:
Bool_varl = True
Bool_var2 = False

String Data type:
String data can be enclosed with a single quote or double quote or triple quote.

Example:
Char_data = ‘A’
String_data = “Computer Science”
Multiline_data= “““String data can be enclosed with single quote or double quote or triple quote.”””