Programming and Problem Solving through Python

Operators Expressions and Python Statements Question & Answer



Question : 1
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 : 2
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 : 3

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 : 4

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 : 5

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 : 6

Which Python statement can be termed as empty statement?



Answer :

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


pass


Question : 7

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 : 8

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 : 9

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 : 10

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 : 11

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 : 12

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 : 13

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 : 14

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.




Question : 15

Define Operator and Operand.



Answer :

An Operator is a particular symbol which is used on some values and produces an output as a result. An operand is an entity on which an operator acts. For example, in the following expression:
c=a+b
the sign + is an operator on the two operands a and b. 



Question : 16

What is the use of type()?



Answer :

To verify the type of any object in Python, use the type() function.



Question : 17

The modulus operator is used to produce the quotient in division. Is it True or False ? 



Answer :

False, the modulus operator is used to produce the remainder.



Question : 18

Describe the use parentheses in expressions.



Answer :

Parentheses can be used to group terms in an expression so as to provide control over the order in which the operations are performed.



Question : 19

Describe how Python evaluates an expression containing parentheses.



Answer :

A Python expression is evaluated by first evaluating each of the sub-expressions in the parentheses, and then using those values to complete the evaluation. If the expression contains nested parentheses, the evaluation is performed by evaluating the innermost parentheses first and working outwards from there.



Question : 20

Explain the use of the assignment operator.



Answer :

The assignment operator causes the value of its right operand to be stored in the memory location identified by its left operand.



Question : 21

What is the difference between Expressions and Operators?



Answer :

Most statements (logical lines) that you write will contain expressions. A simple example of an expression is 2+3. An expression is a combination of operators and operands.
Operators are functionality that do something and can be represented by symbols, such as + or by special keywords. They require some data to operate on and such data is called operands.



Question : 22

Example and (boolean AND) with example.



Answer :

The and operator evaluates to True, if both the expressions are true; False if either or both operands evaluate to False. For example,

x and y returns False if x is False, else it returns evaluation of y

x= False;

y=True;

x and y returns False since x is False.

in this case, Python will not evaluate y since it knows that the left hand side of the 'and' expression is False which implies that the whole expression will be False irrespective of the other values.



Question : 23

Example or (boolean OR) with example.



Answer :

The or operator evaluates to True, if either of operands evaluates to true; False if both operands evaluate to False. For example,

if x is True, it returns True, else it returns evaluation of y

x= True;

y= False;

x or y returns True.



Question : 24

Explain Associativity of operators wit6h example.



Answer :

Associativity means the order in which the two operators with the same precedence should be applied in an expression. It is usually associated from left to right . This means that operators with the same precedence are evaluated from left to right manner. For example, 2+3-4 is evaluated as (2+3)-4.

Some operators like assignment operators have right to left associativity. i.e., a=b=c is treated as a= (b=c).



Question : 25

What is the difference between * and **? Explain with an example.



Answer :

* ( multiply) gives the multiplication of the two numbers or returns the string repeated that many times. For example,

2 * 3 gives 6.

'la' * gives 'lalala'.

** (power) returns x to the power of y. For example,

3 ** 4 gives 81 (i.e., 3 * 3 * 3 * 3 )




Question : 26

What is the difference between << (left shift) and >> (right shift) operators ?



Answer :

<< (left shift) Shift the bits of the number to the left by the number of bits specified. (Each number is represented in memory by bits or binary digits, i.e., 0 and 1)

For example, 2 << 2 gives 8.2 is represented by 10 in bits.

Left shifting by 2 bits gives 1000 which represents the decimal 8.

>> ( right shift) Shift the bits of the number to the right by the number of bits specified.
For example, 11 >> 1 gives 5.
11 is represented in bits by 1011, which when right shifted by 1 bit gives 101 which is the decimal 5.



Question : 27

What is the difference between & (bit-wise AND), and I  (bit-wise OR) operators?



Answer :

Bit-wise AND (&) returns 1 if both the bits are 1, otherwise 0. For example, the numbers 5 & 3 gives 1.

Bit-wise OR (|) returns 1 if any both of the bits is 1. If both the bits are 0, then it returns 0. For example, the numbers 5 | 3 gives 7.




Question : 28

Explain not ( boolean NOT ) with an example



Answer :

The not operator is used to reverse the logical state of its expression. Logical not returns True if the expression is false and False if the expression is true. For example

If x is True, it returns False.

If x is False, it returns True.

x=True;

not x returns False.



Question : 29
What is an expression ?


Answer :

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable. Hence the following are all legal expressions (assuming that the variable x has been assigned a value):
17
x
x + 17



Question : 30

What is a statement ?



Answer :

A statement is a unit of code that the Python interpreter can execute.



Question : 31

What do you mean by floor division ?



Answer :

The operation that divides two numbers and chops off the fractional part. For example, assuming a=15.9, b=3, c=a / / b gives value 5.0, not 5.3.



Question : 32

What is the difference between "=" and "=="  ?



Answer :

"=" is the assignment operator whereas "==" is the equality operator. The assignment operator is used to assign a value to an identifier, whereas the quality operator is used to determine if two expressions have the same value. 



Question : 33

What don you mean by precedence and associativity of operators ?



Answer :

The operators in Python are grouped hierarchically according to their precedence (i.e, order of evaluation ).Operations with a higher precedence are carried out before operations having a lower precedence than any of the other operators.

The order in which consecutive operations within the same precedence group are carried out is know as associativity.



Question : 34

What are Identity and Membership operators ?



Answer :

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.
Membership operators are used to if an object is present in a sequence.



Question : 35

Explain different bitwise operators.



Answer :

& (AND) : Sets each bit to 1 if both bits are 1 
| (OR) : Sets each bit to 1 if one of two bits is 1 
^ (XOR) : Sets each bit to 1 if only one of two bits is 1
~ (NOT) : Inverts all the bits
<< (Left Shift) : Zera fill left shift enables to shift left by pushing zeros in from the right and lets the leftmost bits fall off.
>> (Right Shift) : Signed right shift enable to shift right by pushing copies of the leftmost bit in from the left, and lets the rightmost bits fall off.



Question : 36

What will be the result of 5.0/3 and 5.0 / / 3 ?



Answer :

>>> 5.0 / 3
1.6666666666666667
>>> 5.0 / / 3
1.0



Question : 37
What is the difference between + and * operators of List ?


Answer :

The plus (+) sign is the list concatenation operator, while the asterisk ( * ) is the repetition operator.



Question : 38

What is boolean data type ? Explain with an example.



Answer :

Comparisons in Python can only generate one of the two possible responses: True or False.
These data types are called booleans.
To illustrate, you can create several variables to store Boolean values and print the result:

boo1_1=4==2*3
boo1_2=10<2*2**3
boo1_3=8>2*4+1
print (boo1_1)
print (boo1_2)
print (boo1_3)

RUN
False
True
False



Question : 39

What is the difference between pow() and**?



Answer :

Both pow () and the double star (**) operator perform exponentiation. ** is an operator whereas pow () is a built-in function.



Question : 40

What is the deference between expression and statement ?



Answer :

Statement is an instruction or a unit of code that the Python interpreter can execute. For example, print ("Welcome") is a statement. statement may or may not return a value as outcome.



Question : 41

What is the difference between num = 20 and num == 20?



Answer :

In num = 20, variable num is assigned the value 20
In num == 20, variable num is compared with the value 20



Question : 42

What is conditional branching ?



Answer :

In Conditional branching, a program decides whether or not to execute the next statement (s),based on the resultant value of the current expression. For example, go to second year if you pass first year exams, else repeat first year exam, else repeat first year.



Question : 43

What is looping ?



Answer :

In looping, a program performs a set of actions or operations repeatedly till the given condition is true. Looping, for example, adds 1 to number x till current value of x becomes 100. It is also called iteration. Iteration is a process of executing a statement or a set of statements repeatedly, until the specified condition is true. 



Question : 44

Explain if statement.



Answer :

The ststement if tests a particular condition. Whenever that condition evaluates to true, an action or a set of actions is executed. Otherwise, the actions are ignored.



Question : 45

Explain range() function ?



Answer :

Python rance() function generates a list of numbers, which is generally used to iterate cover with for loops.



Question : 46

Mention the similarity and difference between break and continue statements.



Answer :

Similarity: Both break and continue are jump statement.
Difference : The break statement terminates the entire loop body whereas continue statement forces the next iteration of the loop to take place, skipping any code following continue statement in the current iteration of the loop.



Question : 47

What is the function of break statement in a while or for loop ?



Answer : If the break statement is included in the while, or for, loop then control will immediately be transferred out of the loop when the break statement is encountered. This provides a convenient way to terminate the loop if an error or other irregular condition is detected.

Question : 48

What is the use of Control structures ?



Answer :

Control structures allow us to change the sequence of instructions for execution. They also implement decisions and repetitions in programs.



Question : 49

How many types of control structures are there ? Explain.



Answer :

There are mainly two types of control structures. These are:

  • Conditional Controls : They allow the computer to take decision as to which statement is to be executed next.
  • Looping : It allows the computer to execute a group of statements repeatedly till the condition is satisfied.


Question : 50

Explain Selection logic.



Answer :

Selection logic, also known as decision logic, is used for making decisions. It is used for selecting the proper path out of the two or more alternative paths in the program logic, It is depicted as either an IF...THEN...ELSE or IF...THEN structure.



Question : 51

What do you mean by flow-of-control ?



Answer :

The flow-of-control of a program is the order in which the statements are executed one after the other sequentially.



Question : 52

What do you mean by selection statements ?



Answer :

In some programs, you may need to change the sequence of execution of instructions according to specified conditions. This is done by selection statements.



Question : 53

What is the minimum number of iteration that while loop could make ?



Answer :

while loop could make 0 iteration.



Question : 54

What is the difference between entry controlled loop and exit-controlled loop?



Answer :

In an entry-controlled loop, the loop control condition is evaluated at the entry point of the loop. However, in an exit-controlled loop, the loop control condition is evaluated at the exit point of the loop.
If the loop control condition is false in the beginning, the entry-controlled loop is never executed. However, the exit-controlled loop would be executed at least once.



Question : 55

What are jump statements ?



Answer :

Jump statements are the statements that allow us to transfer control to another part of our program.



Question : 56

What is Iteration ?



Answer :

It is a process of executing a statement or a set of statements repeatedly, until the specified condition is true.



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