Programming and Problem Solving through Python

Operators Expressions and Python Statements Question & Answer



Question :
What is the difference between an expression and a statement in Python?


Answer :

A statement is an instruction that the Python interpreter can execute. We have only seen the assignment statement so far. Some other kinds of statements that we‘ll see shortly are while statements, for statements, if statements, and import statements. (There are other kinds too!)

An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to print an expression, the interpreter evaluates the expression and displays the result.



Question :
What are augmented assignment operators? How are they useful?


Answer :

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.



Question :

What is the common structure of Python compound statements?



Answer :

The common structure of a Python compound statement is as shown below:


<compound statement header>:

    <indented body with multiple simple\

     and/or compound statements>


It has the following components:


A header line which begins with a keyword and ends with a colon.

A body containing a sequence of statements at the same level of indentation.



Question :

What is the importance of the three programming constructs?



Answer :

The importance of the three programming constructs is a given below:


Sequence — Statements get executed sequentially.

Selection — Execution of statements depends on a condition test.

Repetition\Iteration — Repetition of a set of statements depends on a condition test.



Question :

What is empty statement in Python? What is its need?



Answer :

In Python, an empty statement is pass statement. Its syntax is:


pass

When pass statement is encountered, Python does nothing and moves to next statement in the flow of control.


It is needed in those instances where the syntax of the language requires the presence of a statement but where the logic of the program does not.



Question :

Which Python statement can be termed as empty statement?



Answer :

In Python, an empty statement is pass statement. Its syntax is:


pass


Question :

What is entry-controlled loop?



Answer :

An entry-controlled loop checks the condition at the time of entry. Only if the condition is true, the program control enters the body of the loop.



Question :

What are the four elements of a while loop in Python?



Answer :

The four elements of a while loop in Python are:


Initialization Expressions — It initializes the loop control variable and it is given outside the while loop before the beginning of the loop.

Test Expression — If its truth value is true then the loop-body gets executed otherwise not.

The Body of the Loop — It is the set of statements that are executed repeatedly in loop.

Update Expressions — It updates the value of loop control variable and it is given inside the while loop.



Question :

What is the difference between else clause of if-else and else clause of Python loops?



Answer :

The else clause of an if-else 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 become false for a while loop or when the for loop has executed for the last value in sequence.



Question :

In which cases, the else clause of a loop does not get executed?



Answer :

The else clause of a loop does not get executed if the loop is terminated due to the execution of a break statement inside the loop.



Question :

What are jump statements? Name them.



Answer :

Jump statements are used to unconditionally transfer program control to other parts within a program. Python provides the below jump statements:


break

continue



Question :

How and when are named conditions useful?



Answer :

Sometimes the conditions being used in the code are complex and repetitive. In such cases, to make the program more readable and maintainable, named conditions can be used.



Question :

What are endless loops ? Why do such loops occur?



Answer :

A loop which continues iterating indefinitely and never stops is termed as an endless or infinite loop. Such loops can occur primarily due to two reasons:


1. Logical errors when the programmer misses updating the value of loop control variable.

2. Purposefully created endless loops that have a break statement within their body to terminate the loop.



Question :

How is break statement different from continue?



Answer :

When the break statement gets executed, it terminates its loop completely and control reaches to the statement immediately following the loop. The continue statement terminates only the current iteration of the loop by skipping rest of the statements in the body of the loop.




CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad