Students can Download Computer Science Chapter 5 Python -Variables and Operators Questions and Answers, Notes Pdf, Samacheer Kalvi 12th Computer Science Book Solutions Guide Pdf helps you to revise the complete Tamilnadu State Board New Syllabus and score more marks in your examinations.
Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 5 Python – Variables and Operators
Samacheer Kalvi 12th Computer Science Python – Variables and Operators Text Book Back Questions and Answers
PART – 1
I. Choose The Best Answer
Question 1.
Who developed Python?
(a) Ritche
(b) Guido Van Rossum
(c) Bill Gates
(d) Sunder Pitchai
Answer:
(b) Guido Van Rossum
Question 2.
The Python prompt indicates that Interpreter is ready to accept instruction?
(a) >>>
(b) <<<
(c) #
(d) <<
Answer:
(a) >>>
Question 3.
Which of the following shortcut is used to create new Python Program?
(a) Ctrl + C
(b) Ctrl + F
(c) Ctrl + B
(d) Ctrl + N
Answer:
(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) Comma(,)
(d) Colon(;)
Answer:
(c) Comma(,)
Question 6.
Which of the following is not a token?
(a) Interpreter
(b) Identifiers
(c) Keyword
(d) Operators
Answer:
(a) Interpreter
Question 7.
Which of the following is not a Keyword in Python?
(a) Break
(b) While
(c) Continue
(d) Operators
Answer:
(d) Operators
Question 8.
Which operator is also called as Comparative operator?
(a) Arithmetic
(b) Relational
(c) Logical
(d) Assignment
Answer:
(b) Relational
Question 9.
Which of the following is not Logical operator?
(a) And
(b) Or
(c) Not
(d) Assignment
Answer:
(d) Assignment
Question 10.
Which operator is also called as Conditional operator?
(a) Ternary
(b) Relational
(c) Logical
(d) Assignment
Answer:
(a) Ternary
PART – II
II. Answer The Following Questions
Question 1.
What are the different modes that can be used to test Python Program?
Answer:
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?
Answer:
Python breaks each logical line into a sequence of elementary lexical components known as Tokens. The normal token types are;
- Identifiers
- Keywords
- Operators
- Delimiters
- Literals
Whitespace separation is necessary between tokens, identifiers or keywords.
Question 3.
What are the different operators that can be used in Python?
Answer:
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?
Answer:
Literal is a raw data given in a variable or constant. In Python, there are various types of literals.
- Numeric
- String
- Boolean
Question 5.
Write short notes on Exponent data?
Answer:
An Exponent data contains decimal digit part, decimal point, exponent part followed by one or more digits.
12.E04, 24.e04 # Exponent data
PART – III
III. Answer The Following Questions
Question 1.
Write short notes on Arithmetic operator with examples?
Answer:
Arithmetic operators:
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.
Question 2.
What are the assignment operators that can be used in Python?
Answer:
Assignment operators:
In Python, = is a simple assignment operator to assign values to variable. Let a = 5 and b = 10 assigns the value 5 to a and 10 to b these two assignment statement can also be given as a, b = 5, 10 that assigns the value 5 and 10 on the right to the variables a and b respectively. There are various compound operators in Python like + =, – =,* =, / = % =,** = and //= are also available.
Question 3.
Explain Ternary operator with examples?
Answer:
Conditional operator:
Ternary operator is also known as conditional operator that evaluate 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?
Answer:
Escape Sequences:
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 newline, and “\r” is a carriage return. For example to print the message “It’s raining”, the Python command is >>> print (“It\’s rainning”)
It’s rainning
Python supports the following escape sequence characters.
Question 5.
What are string literals? Explain?
Answer:
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 – 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”.
PART – IV
IV. Answer The Following Questions
Question 1.
Describe in detail the procedure Script mode programming?
Answer:
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:
(I) Choose File → New File or press Ctrl + N in Python shell window.
(II) An untitled blank script text editor will be displayed on screen
(III) Type the following code in Script editor
a = 100
b = 350
c = a + b
print (“The Sum = “, c)
Saving Python Script
(I) Choose File → Save or Press Ctrl + S
(II) Now, Save As dialog box appears on the screen
(III) In the Save As dialog box, select the location where you want to save your Python code, and type the file name in File Name box. Python files are by default saved with extension .py. Thus, while creating Python scripts using Python Script editor, no need to specify the file extension.
(IV) Finally, click Save button to save your Python script.
Executing Python Script
(I) Choose Run → Run Module or Press F5
(II) If your 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.
(III) For all error free code, the output will appear in the IDLE window of Python:
Question 2.
Explain input ( ) and print ( ) functions with examples?
Answer:
Input and Output Functions:
A program needs to interact with the user to accomplish the desired task; this can be achieved using Input – Output functions. The input ( ) function helps to enter data at run time by the user and the output function print ( ) is used to display the result of the program on the screen after execution.
The print ( ) function
In Python, the print Q function is used to display result on the screen. The syntax for print Q is as follows:
Example
print (“string to be displayed as output ”)
print (variable)
print (“String to be displayed as output ”, variable)
print (“String1 ”, 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
Th print ( ) evaluates the expression before printing it on the monitor. The print ( ) displays an entire statement which is specified within print ( ). Comma ( , ) is used as a separator in print ( ) to print more than one item.
input ( ) function
In Python, input ( ) function is used to accept data as input at run time. The syntax for input ( ) function is,
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.
If a prompt string is used, it is displayed on the monitor; the user can provide expected data from the input device. 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 1:
input ( ) with prompt string
>>> city = input (“Enter Your City: ”)
Enter Your City: Madurai
>>> print (“I am from “, city)
I am from Madurai
Example 2:
input ( ) without prompt string
>>> city = input ( )
Rajarajan
>>> print (I am from”, city)
I am from Rajarajan
Note that in example – 2, the input ( ) is not having any prompt string, thus the user will not know what is to be typed as input. If the user inputs irrelevant data as given in the above example, then the output will be unexpected. So, to make your program more interactive, provide prompt string with input ( ).
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. We will leam about more such functions in later chapters.
Example 3:
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
Example 4:
Alternate method for the above program
x, y = int (input(“Enter Number 1 :”)), int(input(“Enter Number 2:”))
print (”X = “,x,”Y = “,y)
Output:
Enter Number 1:30
Enter Number 2:50
X = 30 Y= 50
Question 3.
Discuss in detail about Tokens in Python?
Answer:
Tokens:
Python breaks each logical line into a sequence of elementary lexical components known as Tokens. The normal token types are
- Identifiers
- Keywords
- Operators
- Delimiters
- Literals.
Whitespace separation is necessary between tokens, identifiers or keywords.
(I) Identifiers:
An Identifier is a name used to identify a variable, function, class, module or object.
- An identifier must start with an alphabet (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 character such as %, $, @ etc., within identifiers.
Example of valid identifiers
Sum, total _ marks, regno, num 1
Example of invalid identifiers
12 Name, name$, total – mark, continue
(II) Keywords
Keywords are special words used by Python interpreter to recognize the structure of program. As these words have specific meaning for interpreter, they cannot be used for any other purpose.
(III) 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 operator are known as operands.
Arithmetic operators:
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.
Relational or Comparative operators:
A Relational operator is also called as Comparative operator which checks the relationship between two operands. If the relation is true, it returns True; otherwise it returns False.
Logical operators:
In python, Logical operators are used to perform logical operations on the given relational expressions. There are three logical operators they are and, or and not.
Assignment operators:
In Python, = is a simple assignment operator to assign values to variable. Let a = 5 and b = 10 assigns the value 5 to a and 10 to b these two assignment statement can also be given as a, b = 5, 10 that assigns the value 5 and 10 on the right to the variables a and b respectively. There are various compound operators in Python like + =, – =, * =, / =, % =, ** = and //= are also available.
Conditional operator:
Ternary operator is also known as conditional operator that evaluate 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
(IV) Delimiters
Python uses the symbols and symbol combinations as delimiters in expressions, lists, dictionaries and strings. Following are the delimiters.
(V) Literals
Literal is a raw data given in a variable or constant. In Python, there are various types of literals.
- Numeric
- String
- Boolean
1. Numeric Literals:
Numeric Literals consists of digits and are immutable (unchangeable). Numeric literals can belong to 3 different numerical types Integer, Float and Complex.
2. 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-quote is used to give multi-line string literal.
3. Boolean Literals:
A Boolean literal can have any of the two values: True or False.
Escape Sequences:
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 newline, and “\r” is a carriage return. For example to print the message “It’s raining”, the Python command is
>>>print (“It\’s rainning”)
It’s rainning
Python supports the following escape sequence characters.
Samacheer kalvi 12th Computer Science Python – Variables and Operators Additional Questions and Answers
PART – 1
I. Choose The Best Answer:
Question 1.
Python language was released in the year …………………………….
(a) 1991
(b) 1993
(c) 1995
(d) 1997
Answer:
(a) 1991
Question 2.
CWI means ……………………………
Answer:
Centrum Wiskunde & Information
Question 3.
Python got its name from ……………………………
Answer:
Monthly Python’s Flying Circus
Question 4.
Find the wrong statement from the following.
(a) Python supports procedural approaches
(b) Python supports object oriented approaches
(c) Python is DBMS tool
Answer:
(c) Python is DBMS tool
Question 5.
IDLE means ……………………………
Answer:
Integrated Development Learning Environment
Question 6.
How many modes of programming are there in python?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(a) 2
Question 7.
The extension for python file is ……………………………
(a) .pyt
(b) .pt
(c) .py
(d) .pyth
Answer:
(c) .py
Question 8.
…………………………… mode is used to create and edit python source file.
(a) Line
(b) Script
(c) Interactive
(d) Interface
Answer:
(b) Script
Question 9.
Which mode can be used as a simple calculator?
(a) Line
(b) Script
(c) Interactive
(d) Interface
Answer:
(c) Interactive
Question 10.
What does prompt (>>>) indicator?
(a) Compiler is ready to debug
(b) Results are ready
(c) Waiting for the Input data
(d) Interpreter is ready to accept Instructions
Answer:
(d) Interpreter is ready to accept Instructions
Question 11.
Which command is used to get output ……………………………
(a) Cout
(b) Print
(c) Print f
(d) Write
Answer:
(b) Print
Question 12.
Find the correct one from the following.
(a) Scripts are reusable and not editable
(b) Scripts are not reusable and they are editable
(c) Scripts are not reusuable, not editable
(d) Scripts are both reusable and editable
Answer:
(d) Scripts are both reusable and editable
Question 13.
Which command is selected from file menu create new script text editor?
(a) New
(b) New file
(c) New editor
(d) New Script file
Answer:
(b) New file
Question 14.
What is the default name for blank script text editor?
(a) Untitled
(b) Untitled 1
(c) Document 1
(d) Editor 1
Answer:
(b) Untitled 1
Question 15.
What is the keyboard shortcut to Run?
(a) F5
(b) Alt + F5
(c) Shift + F5
(d) Ctrl + F5
Answer:
(a) F5
Question 16.
…………………………… command is used to execute python script?
(a) Run
(b) Compile
(c) Run → Run Module
(d) Compile → Compile Run
Answer:
(c) Run → Run Module
Question 17.
Errors in the python script appears in …………………………… color.
(a) Yellow
(b) Red
(c) Blue
(d) Black
Answer:
(b) Red
Question 18.
…………………………… function helps to enter data at run time by the user.
Answer:
input ( )
Question 19.
If …………………………… is not given in input ( ), no message is displayed on the screen.
Answer:
Prompt sting
Question 20.
Identify the wrong statement from the following. The input ( ) accepts all data as
(a) Strings
(b) Characters
(c) Numbers
(d) All the above
Answer:
(c) Numbers
Question 21.
The …………………………… function is used to convert string data as integer data explicitly.
Answer:
int ( )
Question 22.
…………………………… are ignored by the python interpreter.
(a) Keywords
(b) Tokens
(c) Delimiters
(d) Comments
Answer:
(d) Comments
Question 23.
Comments are of …………………………… or …………………………… lines.
Answer:
Single, multi
Question 24.
…………………………… are used to indicate blocks of codes in python.
(a) Whitespaces
(b) { }
(c) [ ]
(d) < >
Answer:
(a) Whitespaces
Question 25.
Python breaks each logical line into a sequence of elementary lexical components called ……………………………
Answer:
Tokens
Question 26.
How many types of tokens are there?
(a) 1
(b) 2
(c) 5
(d) 10
Answer:
(c) 5
Question 27.
Pick the odd one out.
Identifiers, Keywords, Delimiters, Comments, Literals
Answer:
Comments
Question 28.
An identifier is a name used to identify a ……………………………
(a) Variable
(b) Function
(c) Class
(d) All of these
Answer:
(d) All of these
Question 29.
Pick the odd one out.
(a) Sum, regno, numl, 12Name, – Marks
(b) False, class, is, as, if, end
(c) Relational, while, logical, Assignment,
(d) + * % ** = =
Answer:
(a) 12 Name, (b) end, (c) while, (d) =
Question 30.
Find the correct statement from the following
(a) Continue is an identifier
(b) Sum is a keyword
(c) ** = is a delimiter
(d) = = is an assignment operator
Answer:
(c) ** = is a delimiter
Question 31.
How many types of operators are there?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(d) 5
Question 32.
Match the following
1. // – (i) Modulus
2. # – (ii) Floor division
3. % – (iii) Strings
4. ||| ||| – (iv) Comments
(a) 1 – (ii), 2 – (iv), 3 – (i), 4 – (iii)
(b) 1 – (i), 2 – (ii), 3 – (iii), 4 – (iv)
(c) 1 – (iv), 2 – (ii), 3 – (i), 4 – (iii)
(d) 1 – (iv), 2 – (i), 3 – (iii), 4 – (ii)
Answer:
(a) 1 – (ii), 2 – (iv), 3 – (i), 4 – (iii)
Question 33.
…………………………… are special words used by Python Interpreter
Answer:
Keywords
Question 34.
The value of an operator used is ……………………………
(a) 0
(b) 1
(c) Operands
(d) NULL
Answer:
(c) Operands
Question 37.
Match the following expressions with their equivalent output a = 100, b = 10
(1) >>> a % 30 – (i) 100
(2) >>> a // 30 – (ii) 10.0
(3) >>> a/b – (iii) 3
(4) >>> a * b – (iv) 10
(a) 1 – (iv), 2 – (iii), 3 – (ii), 4 – (i)
(b) 1 – (i), 2 – (ii), 3 – (iii), 4 – (iv)
(c) 1 – (i), 2 – (ii), 3 – (iv), 4 – (iii)
(d) 1 – (iv), 2 – (ii), 3 – (iii), 4 – (i)
Answer:
(a) 1 – (iv), 2 – (iii), 3 – (ii), 4 – (i)
Question 38.
Which operator checks the relationship between two operands.
(a) Relational
(b) Comparative
(c) Both
(d) None of the these
Answer:
(c) Both
Question 39.
Assume a = 100 and b = 35. Find the true statements.
(i) >>> a > b
(ii) >>> a = = b
(iii) >>> a ! = b
(a) (i), (iii) are true
(b) (ii), (iii) are true
(c) (i), (ii) are true
Answer:
(a) (i), (iii) are true
Question 40.
Identify Not equal to operator in python?
(a) < >
(b) ==
(c) NOT EQUAL
(d) ! =
Answer:
(d) ! =
Question 41.
How many logical operators are there?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(b) 3
Question 42.
How many comparative operators are there?
(a) 4
(b) 5
(c) 6
(d) 7
Answer:
(c) 6
Question 43.
…………………………… is the simple assignment operator.
(a) ! =
(b) >
(c) >>
(d) =
Answer:
(d) =
Question 44.
Compound operators comes under the category of …………………………… operators.
Answer:
Assingnment
Question 45.
Find the wrongly matched pair.
(a) ! = – relational operator
(b) not a > b – Comparative Operator
(c) **= – delimitors operator
(d) Assingnment Operator
Answer:
(b) not a > b – Comparative Operator
Question 46.
Identify which is not a delimiter
(a) & =
(b) :
(c) ;
(d) ::
Answer:
(d) ::
Question 47.
How many types of literals are there?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(b) 3
Question 48.
Numeric literals are ……………………………
(a) Integer, Float, Complex
(b) Int, Float, Void
(c) Int, Float, Char
(d) Int, Float, Boolean
Answer:
(a) Integer, Float, Complex
Question 49.
Strings in python are represented using
(a) ”
(b) “”
(c) “‘ “‘
(d) All of these
Answer:
(d) All of these
Question 50.
Multiline string literal is given by ……………………………
Answer:
“‘ “‘ triple quotes
Question 51.
The two values accepted by Boolean literals are …………………………… or ……………………………
Answer:
True, False
Question 52.
…………………………… is the escape character.
(a) +
(b) t
(c) \
(d) %
Answer:
(c) \
Question 53.
Which one of the following is the newline character?
(a) \t
(b) \v
(c) \r
(d) \n
Answer:
(d) \n
Question 54.
…………………………… is the escape character for carriage return.
Answer:
\r
Question 55.
Pick the odd one out.
(a) Tuples, for, list, dictionaries, Number
(b) \n, \”, V, \r, \k
(c) and, or, not, true
(d) >, > =, <, < =, < >
Answer:
(a) for, (b) \k, (c) true, (d) <>
Question 56.
How many Interger data are there?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(b) 3
Question 57.
OX represents …………………………… integer.
Answer:
Hexadecimal
Question 58.
Octal integer uses …………………………… to denote octal digits
(a) OX
(b) O
(c) OC
(d) Od
Answer:
(b) O
Question 59.
Find the hexadecimal Integer.
(a) 0102
(b) 0876
(c) 0432
(d) 0X102
Answer:
(d) 0X102
Question 60.
How many floating point values are there in a complex number?
(a) 1
(b) 2
(c) 3
(d) 4
Answer:
(b) 2
Question 61.
What is the another name for fundamental data type?
(a) Class
(b) Built – in
(c) Typedef
(d) User defined
Answer:
(b) Built – in
Question 62.
Which one of the following statement is wrong?
(a) Octal Integer uses upper and lower case O
(b) Hexadecimal Integer uses upper and lower case OX
(c) Long integer uses upper and lower case 1.
Answer:
(c) Long integer uses upper and lower case 1.
PART – II
II. Answer The Following Questions.
Question 1.
How will you create scripts in python?
Answer:
(I) Choose File → New File or press Ctrl + N in Python shell window.
(II) An untitled blank script text editor will be displayed on screen.
Question 2.
Write note on keywords. Give examples?
Answer:
Keywords are special words used by Python interpreter to recognize the structure of program. As these words have specific meaning for interpreter, they cannot be used for any other purpose. Eg, While, if.
PART – III
III. Answer The Following Questions.
Question 1.
What are the key features of python?
Answer:
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 2.
How will you execute python script?
Executing Python Script
(I) Choose Run → Run Module or Press F5
(II) If your 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.
(III) For all error free code, the output will appear in the IDLE window of Python.
Question 3.
Give the syntax for print ( ) function?
Answer:
The syntax for print ( ) is as follows:
Example:
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”)
Question 4.
Explain the Syntax of input ( ) function?
Answer:
The syntax for inputQ function is,
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.
If a prompt string is used, it is displayed on the monitor; the user can provide expected data from the input device. 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
Question 5.
Give an example for input with and without prompt string?
Example 1: input ( ) with prompt string
>>> city = input (“Enter Your City: ”)
Enter Your City: Madurai
Example 2: input ( ) without prompt string
>>> city = input ( )
Rajarajan
Question 6.
Write a short note on comments?
Answer:
Comments in Python
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 multi – lines. 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 7.
Mention some rules for Identifiers?
Answer:
- An identifier must start with an alphabet (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 character such as %,$, @ etc., within identifiers.
Question 8.
Give any 6 keywords in Python?
Answer:
false, class, none, continue, finally, return, is
Question 9.
Give an example program for Ternary operator?
To test Conditional (Ternary) Operator:
# Program to demonstrate conditional operator
a, b = 30, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b print (“The Minimum of A and B is “,min) # End of the Program Output: The Minimum of A and B is 20.
Question 10.
Write any 6 delimiters in python?
Answer:
PART – IV
IV. Answer The Following Questions
Question 1.
Explain operators in detail?
Answer:
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 operator are known as operands.
(I) Arithematic operators 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.
To test Arithmetic Operators: #Demo Program to test Arithmetic Operators a = 100 b = 10 print (“The Sum = “,a + b) print (“The Difference = “,a – b) print (“The Product = “,a * b) print (“The Quotient = “,a / b) print (“The Remainder = “,a % 30) print (“The Exponent = “, a ** 2) print (“The Floor Division =”, a // 30) #Program End Output: The Sum = 110 The Difference = 90 The Product = 1000 The Quotient = 10.0 The Remainder = 10 The Exponent = 10000 The Floor Division = 3
(II) Relational or Comparative operators A Relational operator is also called as Comparative operator which checks the relationship between two operands. If the relation is true, it returns True; otherwise it returns False.
To test Relational Operators: #Demo Program to test Relational Operators a = int (input(“Enter a Value for A:”)) b = int (input(“Enter a Value for B:”)) print (“A = “,a,” and B = “,b) print (“The a = = b = “,a = = b) print (“The a > b = “,a > b)
print (“The a < b = “,a < b) print (“The a > = b = “, a > = b)
print (“The a! = b = “,a! = 0)
print (“The a! = b = “,a! = b)
#Program End
Output:
Enter a Value for A: 35
Enter a Value for B: 56
A = 35 and B = 56
The a = =b = False
The a > b = False
The a < b = True The a > = b = False
The a < = b = False
The a ! = b = True
(III) Logical operators
In python, Logical operators are used to perform logical operations on the given relational expressions. There are three logical operators they are and, or and not.
To test Logical Operators:
(IV) Assignment operators
In Python, = is a simple assignment operator to assign values to variable. Let a = 5 and b = 10 assigns the value 5 to a and 10 to b these two assignment statement can also be given as a, b = 5, 10 that assigns the value 5 and 10 on the right to the variables a and b respectively. There are various compound operators in Python like + =, – =, * =, /=, % =, ** = and //= are also available.
To test Assingnment Operators:
(V) Conditional operator
Ternary operator is also known as conditional operator that evaluate 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
To test Conditional (Ternary) Operator:
# Program to demonstrate conditional operator
a, b = 30, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b print (“The Minimum of A and B is “,min) # End of the Program Output: The Minimum of A and B is 20.
Question 2.
Write the Output for the given program?
Answer:
Question 3.
Explain literals and its types?
Answer: Literal is a raw data given in a variable or constant. In Python, there are various types of literals.
(I) Numeric Literals Numeric Literals consists of digits and are immutable (unchangeable). Numeric literals can belong to 3 different numerical types Integer, Float and Complex. To demonstrate Numeric literals # Program to demonstrate Numeric Literals a = Ob 1010 #Binary Literals b = 100 #Decimal Literal c = 0o310 #Octal Literal d = Ox 12c #Hexadecimal Literal print (“Integer Literals :”,a, b, c, d) #Float Literal float_1 = 10.5 float_2 = 1.5e2 print (“Float Literals :”,float_1, float_2) #Complex Literal x = 1 + 3.14 j print (“Complex Literals:”, x) Print (“x = “, x , “Imaginary part of x = “, x.imag, “Real part of x = “, x.real) #End of the Program Output: Integer Literals: 10 100 200 300 Float Literals: 10.5 150.0 Complex Literals: x = (1.3.14) Imaginary part of x = 3.14 Real part of 9 x = 1.0
(II) 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-quote is used to give multi – line string literal. To test String Literals # Demo Program to test String Literals strings = “This is Python” char = “C” multiline_str = ‘”This is a multiline string with more than one line code.'” print (strings) print (char) print (multiline_str) # End of the Program Output: This is Python C C This is a multiline string with more than one line code.
(III) Boolean Literals A Boolean literal can have any of the two values: True or False. # Demo Program to test String Literals boolean _ 1 = True boolean _ 2 = False print (“Demo Program for Boolean Literals”) print (“Boolean Value 1 :”,boolean_1) print (“Boolean Value2 :”,boolean_2) # End of the Program Output: Demo Program for Boolean Literals Boolean Value 1: True Boolean Value2: False (iv) Escape Sequences 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 newline, and “\r” is a carriage return. For example to print the message “It’s raining”, the Python command is >>> print (“ItVs rainning”)
It’s rainning
Python supports the following escape sequence characters.
Question 4.
Explain data types in python?
Answer:
Python Data types:
All data values in Python are objects and each object or value has type. Python has Built-in or Fundamental data types such as Number, 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 integer use O (both upper and lower case) to denote octal digits and hexadecimal integer use OX (both upper and lower case) and L (only upper case) to denote long integer.
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
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 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.”””