Python - Operators Expressions and Python Statements Question & Answer



Qus. What are operators? What is their function? Give examples of some unary and binary operators.

Ans: “Operators are those symbols used with operands, which tells compiler which operation is to be done on operands.‖ in other words – ―operators are tokens that trigger some computation/action when applied to variables and other objects in an expression.‖

Operators are of following types:

  •  Unary operators like (+) Unary Plus, (-) Unary Minus, not etc.
  •  Binary Operators like (+) addition, (*) multiplication, and etc.

Qus. What is block/code block/suit in Python?

Ans: Sometimes a group of statements is part of another statement of function. Such a group of one or more statements is called block or code-block or suit in python. e.g.


Qus. What is the role of indentation in Python?

Ans: Indentation plays a very important role in Python. Python uses indentation to create blocks of code. Statements at same indentation level are part of same block/suit. You cannot unnecessarily indent a statement; python will raise an error for that.

Qus. How many types of strings are supported by Python?

Ans: Python supports two types of strings:

(i) Single-line string That terminates in single line.

(ii) Multi-line String That stores multiple lines of text.

Qus. What will be the sizes of following constants?

(a) "\a‟ (b) “\a” (c) “Kumar\‟s” (d) "\‟ (e) “it‟s”

Ans: (a) 50 (b) 50 (c) 56 (d) 50 (e) 53

This screenshot is the output of the above question.


Qus. How can you create multi-line strings in Python?

Ans: We can create multi-line string by putting a backslash (\) at the end of line which allows you to continue typing in next line in same string.

Qus. Which of the following are syntactically correct strings? State reason.

(a) ”Python is nice Language”

(b) „He called me “Friend!” when he came‟

(c) “Very Good‟

(d) „This is a good book‟

(e) “Namaste

(f) “I liked „Harry Potter‟ very much”

Ans: (a) Correct (b) Correct (c) Incorrect (d) Correct (e) Incorrect (f) Correct


Qus. What is the error in following Python program with one statement?

print(“My name is : “, name)

suggest a solution

Ans: Error is : ―name 'name' is not defined‖. And the solution is to declare the variable-name before this statement.

Qus. Predict the output of the following:

Ans: Output: 17 5


Qus. What are augmented assignment operators? How are they useful?

Ans: An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + (1). Similar constructions are often available for various binary operators. They are helpful in making the source code small.


Qus. What are loops in Python? How many types of loop are there in Python?

Ans: Loops are iteration constructs in Python. Iteration means repetition of a set of statements depending upon a condition test. Loops has three basic elements within it to repeat the statements –

  • Initialization (Start)
  • Check Condition (Stop)
  • Updation (Step)

Python provide two types of loop

(i) Conditional Loop while( (Condition based loop)

(ii) Counting loop for (loop for a given number of times).


Qus. What is the syntax of if-elif statement in Python?

Ans: The syntax of if-elif statement in python is as follows:

If condition1:

#code-block of statements when condition1 is true

elif condion2:

#code-block of statements when condition2 is true

elif condition3:

#code-block of statements when condition3 is true
.

.

.

else:

#code-block of statements when all above conditions are false.


Qus What are jump statements in Python? Name jump statements with example.

Ans: Python offers two jump statements to be used with in loops to jump out of loop-iterations.

These are break and continue statements.